From d3197c5f06ea3bfc0f303d2fe610f085d57a16ca Mon Sep 17 00:00:00 2001 From: James Melville Date: Sat, 30 Jul 2022 11:41:19 -0700 Subject: [PATCH 1/2] initialize fields in constructor --- hnswlib/bruteforce.h | 8 ++++++-- hnswlib/hnswalg.h | 25 ++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/hnswlib/bruteforce.h b/hnswlib/bruteforce.h index 24260400..691424bc 100644 --- a/hnswlib/bruteforce.h +++ b/hnswlib/bruteforce.h @@ -8,10 +8,14 @@ namespace hnswlib { template class BruteforceSearch : public AlgorithmInterface { public: - BruteforceSearch(SpaceInterface *s) { + BruteforceSearch(SpaceInterface *s) : data_(nullptr), maxelements_(0), + cur_element_count(0), size_per_element_(0), data_size_(0), + dist_func_param_(nullptr) { } - BruteforceSearch(SpaceInterface *s, const std::string &location) { + BruteforceSearch(SpaceInterface *s, const std::string &location) : + data_(nullptr), maxelements_(0), cur_element_count(0), size_per_element_(0), + data_size_(0), dist_func_param_(nullptr) { loadIndex(location, s); } diff --git a/hnswlib/hnswalg.h b/hnswlib/hnswalg.h index e95e0b52..7054839b 100644 --- a/hnswlib/hnswalg.h +++ b/hnswlib/hnswalg.h @@ -17,15 +17,34 @@ namespace hnswlib { class HierarchicalNSW : public AlgorithmInterface { public: static const tableint max_update_element_locks = 65536; - HierarchicalNSW(SpaceInterface *s) { + HierarchicalNSW(SpaceInterface *s) : + max_elements_(0), cur_element_count(0), size_data_per_element_(0), + size_links_per_element_(0), num_deleted_(0), M_(0), maxM_(0), maxM0_(0), + ef_construction_(0), mult_(0.0), revSize_(0.0), maxlevel_(0), + visited_list_pool_(nullptr), enterpoint_node_(0), size_links_level0_(0), + offsetData_(0), offsetLevel0_(0), + data_level0_memory_(nullptr), + linkLists_(nullptr), data_size_(0), label_offset_(0), + dist_func_param_(nullptr), metric_distance_computations(0), metric_hops(0), + ef_(0){ } - HierarchicalNSW(SpaceInterface *s, const std::string &location, bool nmslib = false, size_t max_elements=0) { + HierarchicalNSW(SpaceInterface *s, const std::string &location, bool nmslib = false, size_t max_elements=0) : + max_elements_(0), cur_element_count(0), size_data_per_element_(0), + size_links_per_element_(0), num_deleted_(0), M_(0), maxM_(0), maxM0_(0), + ef_construction_(0), mult_(0.0), revSize_(0.0), maxlevel_(0), + visited_list_pool_(nullptr), enterpoint_node_(0), size_links_level0_(0), + offsetData_(0), offsetLevel0_(0), + data_level0_memory_(nullptr), + linkLists_(nullptr), data_size_(0), label_offset_(0), + dist_func_param_(nullptr), metric_distance_computations(0), metric_hops(0), + ef_(0) { loadIndex(location, s, max_elements); } HierarchicalNSW(SpaceInterface *s, size_t max_elements, size_t M = 16, size_t ef_construction = 200, size_t random_seed = 100) : - link_list_locks_(max_elements), link_list_update_locks_(max_update_element_locks), element_levels_(max_elements) { + link_list_locks_(max_elements), link_list_update_locks_(max_update_element_locks), element_levels_(max_elements), + metric_distance_computations(0), metric_hops(0) { max_elements_ = max_elements; num_deleted_ = 0; From 25c738386e14d03134e480dbc0d5b334d3078a0a Mon Sep 17 00:00:00 2001 From: James Melville Date: Wed, 3 Aug 2022 22:58:33 -0700 Subject: [PATCH 2/2] direct member initialize fields --- hnswlib/hnswalg.h | 72 +++++++++++++++++------------------------------ 1 file changed, 26 insertions(+), 46 deletions(-) diff --git a/hnswlib/hnswalg.h b/hnswlib/hnswalg.h index 7054839b..8060683c 100644 --- a/hnswlib/hnswalg.h +++ b/hnswlib/hnswalg.h @@ -17,34 +17,15 @@ namespace hnswlib { class HierarchicalNSW : public AlgorithmInterface { public: static const tableint max_update_element_locks = 65536; - HierarchicalNSW(SpaceInterface *s) : - max_elements_(0), cur_element_count(0), size_data_per_element_(0), - size_links_per_element_(0), num_deleted_(0), M_(0), maxM_(0), maxM0_(0), - ef_construction_(0), mult_(0.0), revSize_(0.0), maxlevel_(0), - visited_list_pool_(nullptr), enterpoint_node_(0), size_links_level0_(0), - offsetData_(0), offsetLevel0_(0), - data_level0_memory_(nullptr), - linkLists_(nullptr), data_size_(0), label_offset_(0), - dist_func_param_(nullptr), metric_distance_computations(0), metric_hops(0), - ef_(0){ + HierarchicalNSW(SpaceInterface *s) { } - HierarchicalNSW(SpaceInterface *s, const std::string &location, bool nmslib = false, size_t max_elements=0) : - max_elements_(0), cur_element_count(0), size_data_per_element_(0), - size_links_per_element_(0), num_deleted_(0), M_(0), maxM_(0), maxM0_(0), - ef_construction_(0), mult_(0.0), revSize_(0.0), maxlevel_(0), - visited_list_pool_(nullptr), enterpoint_node_(0), size_links_level0_(0), - offsetData_(0), offsetLevel0_(0), - data_level0_memory_(nullptr), - linkLists_(nullptr), data_size_(0), label_offset_(0), - dist_func_param_(nullptr), metric_distance_computations(0), metric_hops(0), - ef_(0) { + HierarchicalNSW(SpaceInterface *s, const std::string &location, bool nmslib = false, size_t max_elements=0) { loadIndex(location, s, max_elements); } HierarchicalNSW(SpaceInterface *s, size_t max_elements, size_t M = 16, size_t ef_construction = 200, size_t random_seed = 100) : - link_list_locks_(max_elements), link_list_update_locks_(max_update_element_locks), element_levels_(max_elements), - metric_distance_computations(0), metric_hops(0) { + link_list_locks_(max_elements), link_list_update_locks_(max_update_element_locks), element_levels_(max_elements) { max_elements_ = max_elements; num_deleted_ = 0; @@ -104,22 +85,21 @@ namespace hnswlib { delete visited_list_pool_; } - size_t max_elements_; - size_t cur_element_count; - size_t size_data_per_element_; - size_t size_links_per_element_; - size_t num_deleted_; + size_t max_elements_{0}; + size_t cur_element_count{0}; + size_t size_data_per_element_{0}; + size_t size_links_per_element_{0}; + size_t num_deleted_{0}; + size_t M_{0}; + size_t maxM_{0}; + size_t maxM0_{0}; + size_t ef_construction_{0}; - size_t M_; - size_t maxM_; - size_t maxM0_; - size_t ef_construction_; + double mult_{0.0}, revSize_{0.0}; + int maxlevel_{0}; - double mult_, revSize_; - int maxlevel_; - - VisitedListPool *visited_list_pool_; + VisitedListPool *visited_list_pool_{nullptr}; std::mutex cur_element_count_guard_; std::vector link_list_locks_; @@ -127,20 +107,20 @@ namespace hnswlib { // Locks to prevent race condition during update/insert of an element at same time. // Note: Locks for additions can also be used to prevent this race condition if the querying of KNN is not exposed along with update/inserts i.e multithread insert/update/query in parallel. std::vector link_list_update_locks_; - tableint enterpoint_node_; + tableint enterpoint_node_{0}; - size_t size_links_level0_; - size_t offsetData_, offsetLevel0_; + size_t size_links_level0_{0}; + size_t offsetData_{0}, offsetLevel0_{0}; - char *data_level0_memory_; - char **linkLists_; + char *data_level0_memory_{nullptr}; + char **linkLists_{nullptr}; std::vector element_levels_; - size_t data_size_; + size_t data_size_{0}; - size_t label_offset_; + size_t label_offset_{0}; DISTFUNC fstdistfunc_; - void *dist_func_param_; + void *dist_func_param_{nullptr}; std::unordered_map label_lookup_; std::default_random_engine level_generator_; @@ -253,8 +233,8 @@ namespace hnswlib { return top_candidates; } - mutable std::atomic metric_distance_computations; - mutable std::atomic metric_hops; + mutable std::atomic metric_distance_computations{0}; + mutable std::atomic metric_hops{0}; template std::priority_queue, std::vector>, CompareByFirst> @@ -523,7 +503,7 @@ namespace hnswlib { } std::mutex global; - size_t ef_; + size_t ef_{0}; void setEf(size_t ef) { ef_ = ef;