diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2024-04-23 14:54:46 +0000 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2024-04-23 14:54:46 +0000 |
commit | 1a8350f867910dc49bf29bad893730372a4f6c25 (patch) | |
tree | 65b996c40b92515e812a08645aab1586b45657cf /searchlib | |
parent | c6685d0d0c8c25f6790cadea4e4c6daf791c57b6 (diff) |
SelectResult does not need templates. Also move instead of copy
Diffstat (limited to 'searchlib')
6 files changed, 54 insertions, 36 deletions
diff --git a/searchlib/src/tests/tensor/hnsw_index/hnsw_index_test.cpp b/searchlib/src/tests/tensor/hnsw_index/hnsw_index_test.cpp index fcce2f5eb17..e2fc80f6de6 100644 --- a/searchlib/src/tests/tensor/hnsw_index/hnsw_index_test.cpp +++ b/searchlib/src/tests/tensor/hnsw_index/hnsw_index_test.cpp @@ -547,11 +547,9 @@ TYPED_TEST(HnswIndexTest, manual_insert) this->init(false); EXPECT_EQ(0, this->get_active_nodes()); - std::vector<uint32_t> nbl; - HnswTestNode empty{nbl}; - this->index->set_node(1, empty); + this->index->set_node(1, std::vector<uint32_t>()); EXPECT_EQ(1, this->get_active_nodes()); - this->index->set_node(2, empty); + this->index->set_node(2, std::vector<uint32_t>()); EXPECT_EQ(2, this->get_active_nodes()); HnswTestNode three{{1,2}}; @@ -563,7 +561,7 @@ TYPED_TEST(HnswIndexTest, manual_insert) this->expect_entry_point(1, 0); - HnswTestNode twolevels{{{1},nbl}}; + HnswTestNode twolevels{{{1},std::vector<uint32_t>()}}; this->index->set_node(4, twolevels); this->expect_entry_point(4, 1); @@ -641,11 +639,9 @@ TYPED_TEST(HnswIndexTest, memory_is_put_on_hold_while_read_guard_is_held) TYPED_TEST(HnswIndexTest, shrink_called_simple) { this->init(false); - std::vector<uint32_t> nbl; - HnswTestNode empty{nbl}; + HnswTestNode empty{std::vector<uint32_t>()}; this->index->set_node(1, empty); - nbl.push_back(1); - HnswTestNode nb1{nbl}; + HnswTestNode nb1{std::vector<uint32_t>(1, 1)}; this->index->set_node(2, nb1); this->index->set_node(3, nb1); this->index->set_node(4, nb1); @@ -681,11 +677,9 @@ TYPED_TEST(HnswIndexTest, shrink_called_simple) TYPED_TEST(HnswIndexTest, shrink_called_heuristic) { this->init(true); - std::vector<uint32_t> nbl; - HnswTestNode empty{nbl}; + HnswTestNode empty{std::vector<uint32_t>()}; this->index->set_node(1, empty); - nbl.push_back(1); - HnswTestNode nb1{nbl}; + HnswTestNode nb1{std::vector<uint32_t>(1, 1)}; this->index->set_node(2, nb1); this->index->set_node(3, nb1); this->index->set_node(4, nb1); diff --git a/searchlib/src/vespa/searchlib/tensor/CMakeLists.txt b/searchlib/src/vespa/searchlib/tensor/CMakeLists.txt index 83a1968f4a1..e34c5791ede 100644 --- a/searchlib/src/vespa/searchlib/tensor/CMakeLists.txt +++ b/searchlib/src/vespa/searchlib/tensor/CMakeLists.txt @@ -24,6 +24,7 @@ vespa_add_library(searchlib_tensor OBJECT hnsw_multi_best_neighbors.cpp hnsw_nodeid_mapping.cpp hnsw_single_best_neighbors.cpp + hnsw_test_node.cpp imported_tensor_attribute_vector.cpp imported_tensor_attribute_vector_read_guard.cpp inv_log_level_generator.cpp diff --git a/searchlib/src/vespa/searchlib/tensor/hnsw_index.cpp b/searchlib/src/vespa/searchlib/tensor/hnsw_index.cpp index 7b42fd9a0e1..738541bdd53 100644 --- a/searchlib/src/vespa/searchlib/tensor/hnsw_index.cpp +++ b/searchlib/src/vespa/searchlib/tensor/hnsw_index.cpp @@ -2,7 +2,6 @@ #include "hnsw_index.h" #include "bitvector_visited_tracker.h" -#include "distance_function.h" #include "hash_set_visited_tracker.h" #include "hnsw_index_loader.hpp" #include "hnsw_index_saver.h" @@ -145,6 +144,9 @@ PreparedAddDoc::~PreparedAddDoc() = default; PreparedAddDoc::PreparedAddDoc(PreparedAddDoc&& other) noexcept = default; } +SelectResult::SelectResult() noexcept = default; +SelectResult::~SelectResult() = default; + template <HnswIndexType type> ArrayStoreConfig HnswIndex<type>::make_default_level_array_store_config() @@ -192,7 +194,7 @@ HnswIndex<type>::have_closer_distance(HnswTraversalCandidate candidate, const Hn template <HnswIndexType type> template <typename HnswCandidateVectorT> -typename HnswIndex<type>::SelectResult +SelectResult HnswIndex<type>::select_neighbors_simple(const HnswCandidateVectorT& neighbors, uint32_t max_links) const { HnswCandidateVectorT sorted(neighbors); @@ -210,7 +212,7 @@ HnswIndex<type>::select_neighbors_simple(const HnswCandidateVectorT& neighbors, template <HnswIndexType type> template <typename HnswCandidateVectorT> -typename HnswIndex<type>::SelectResult +SelectResult HnswIndex<type>::select_neighbors_heuristic(const HnswCandidateVectorT& neighbors, uint32_t max_links) const { SelectResult result; @@ -239,7 +241,7 @@ HnswIndex<type>::select_neighbors_heuristic(const HnswCandidateVectorT& neighbor template <HnswIndexType type> template <typename HnswCandidateVectorT> -typename HnswIndex<type>::SelectResult +SelectResult HnswIndex<type>::select_neighbors(const HnswCandidateVectorT& neighbors, uint32_t max_links) const { if (_cfg.heuristic_select_neighbors()) { @@ -573,7 +575,7 @@ HnswIndex<type>::internal_prepare_add_node(PreparedAddDoc& op, TypedCells input_ } template <HnswIndexType type> -typename HnswIndex<type>::LinkArray +LinkArray HnswIndex<type>::filter_valid_nodeids(uint32_t level, const typename PreparedAddNode::Links &neighbors, uint32_t self_nodeid) { LinkArray valid; @@ -986,7 +988,7 @@ HnswIndex<type>::get_node(uint32_t nodeid) const { auto levels_ref = _graph.acquire_levels_ref(nodeid); if (!levels_ref.valid()) { - return HnswTestNode(); + return {}; } auto levels = _graph.levels_store.get(levels_ref); HnswTestNode::LevelArray result; @@ -996,7 +998,7 @@ HnswIndex<type>::get_node(uint32_t nodeid) const std::sort(result_links.begin(), result_links.end()); result.push_back(result_links); } - return HnswTestNode(result); + return {std::move(result)}; } template <HnswIndexType type> diff --git a/searchlib/src/vespa/searchlib/tensor/hnsw_index.h b/searchlib/src/vespa/searchlib/tensor/hnsw_index.h index d94b6584c35..616140f426f 100644 --- a/searchlib/src/vespa/searchlib/tensor/hnsw_index.h +++ b/searchlib/src/vespa/searchlib/tensor/hnsw_index.h @@ -53,16 +53,28 @@ struct PreparedAddNode { struct PreparedFirstAddDoc : public PrepareResult {}; -struct PreparedAddDoc : public PrepareResult { +struct PreparedAddDoc final : public PrepareResult { using ReadGuard = vespalib::GenerationHandler::Guard; uint32_t docid; ReadGuard read_guard; std::vector<PreparedAddNode> nodes; PreparedAddDoc(uint32_t docid_in, ReadGuard read_guard_in) noexcept; - ~PreparedAddDoc(); + ~PreparedAddDoc() override; PreparedAddDoc(PreparedAddDoc&& other) noexcept; }; } + +using LinkArray = std::vector<uint32_t, vespalib::allocator_large<uint32_t>>; +struct SelectResult { + HnswTraversalCandidateVector used; + LinkArray unused; + SelectResult() noexcept; + SelectResult(const SelectResult &) = delete; + SelectResult & operator=(const SelectResult &) = delete; + SelectResult(SelectResult &&) noexcept = default; + ~SelectResult(); +}; + template <HnswIndexType type> class HnswIndex : public NearestNeighborIndex { public: @@ -85,7 +97,6 @@ protected: using LinkArrayStore = typename GraphType::LinkArrayStore; using LinkArrayRef = typename GraphType::LinkArrayRef; - using LinkArray = std::vector<uint32_t, vespalib::allocator_large<uint32_t>>; using LevelArrayRef = typename GraphType::LevelArrayRef; @@ -125,11 +136,6 @@ protected: * Used by select_neighbors_heuristic(). */ bool have_closer_distance(HnswTraversalCandidate candidate, const HnswTraversalCandidateVector& curr_result) const; - struct SelectResult { - HnswTraversalCandidateVector used; - LinkArray unused; - ~SelectResult() {} - }; template <typename HnswCandidateVectorT> SelectResult select_neighbors_heuristic(const HnswCandidateVectorT& neighbors, uint32_t max_links) const; template <typename HnswCandidateVectorT> diff --git a/searchlib/src/vespa/searchlib/tensor/hnsw_test_node.cpp b/searchlib/src/vespa/searchlib/tensor/hnsw_test_node.cpp new file mode 100644 index 00000000000..5f104c18cf7 --- /dev/null +++ b/searchlib/src/vespa/searchlib/tensor/hnsw_test_node.cpp @@ -0,0 +1,10 @@ +// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#include "hnsw_test_node.h" + +namespace search::tensor { + +HnswTestNode::HnswTestNode() noexcept = default; +HnswTestNode::~HnswTestNode() = default; + +} diff --git a/searchlib/src/vespa/searchlib/tensor/hnsw_test_node.h b/searchlib/src/vespa/searchlib/tensor/hnsw_test_node.h index dd0e61ec9e8..ff86a65967c 100644 --- a/searchlib/src/vespa/searchlib/tensor/hnsw_test_node.h +++ b/searchlib/src/vespa/searchlib/tensor/hnsw_test_node.h @@ -2,6 +2,7 @@ #pragma once +#include <cstdint> #include <vector> namespace search::tensor { @@ -19,14 +20,18 @@ private: LevelArray _levels; public: - HnswTestNode() : _levels() {} - HnswTestNode(const LinkArray& level_0) : _levels() { _levels.push_back(level_0); } - HnswTestNode(const LevelArray& levels_in) : _levels(levels_in) {} - bool empty() const { return _levels.empty(); } - size_t size() const { return _levels.size(); } - const LevelArray& levels() const { return _levels; } - const LinkArray& level(size_t idx) const { return _levels[idx]; } - bool operator==(const HnswTestNode& rhs) { + HnswTestNode() noexcept; + HnswTestNode(const HnswTestNode &) = delete; + HnswTestNode & operator=(const HnswTestNode &) = delete; + HnswTestNode(HnswTestNode &&) noexcept = default; + ~HnswTestNode(); + HnswTestNode(LinkArray&& level_0) : _levels() { _levels.push_back(std::move(level_0)); } + HnswTestNode(LevelArray&& levels_in) : _levels(std::move(levels_in)) {} + bool empty() const noexcept { return _levels.empty(); } + size_t size() const noexcept { return _levels.size(); } + const LevelArray& levels() const noexcept { return _levels; } + const LinkArray& level(size_t idx) const noexcept { return _levels[idx]; } + bool operator==(const HnswTestNode& rhs) noexcept { return _levels == rhs._levels; } }; |