Skip to content

Commit

Permalink
Use unique_ptr to manage visited_list_pool_.
Browse files Browse the repository at this point in the history
  • Loading branch information
ttsugriy committed Jun 19, 2023
1 parent 013def5 commit 1ee95db
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions hnswlib/hnswalg.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <assert.h>
#include <unordered_set>
#include <list>
#include <memory>

namespace hnswlib {
typedef unsigned int tableint;
Expand All @@ -33,7 +34,7 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> {
double mult_{0.0}, revSize_{0.0};
int maxlevel_{0};

VisitedListPool *visited_list_pool_{nullptr};
std::unique_ptr<VisitedListPool> visited_list_pool_{nullptr};

// Locks operations with element by label value
mutable std::vector<std::mutex> label_op_locks_;
Expand Down Expand Up @@ -122,7 +123,7 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> {

cur_element_count = 0;

visited_list_pool_ = new VisitedListPool(1, max_elements);
visited_list_pool_ = std::unique_ptr<VisitedListPool>(new VisitedListPool(1, max_elements));

// initializations for special treatment of the first node
enterpoint_node_ = -1;
Expand All @@ -144,7 +145,6 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> {
free(linkLists_[i]);
}
free(linkLists_);
delete visited_list_pool_;
}


Expand Down Expand Up @@ -573,8 +573,7 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> {
if (new_max_elements < cur_element_count)
throw std::runtime_error("Cannot resize, max element is less than the current number of elements");

delete visited_list_pool_;
visited_list_pool_ = new VisitedListPool(1, new_max_elements);
visited_list_pool_.reset(new VisitedListPool(1, new_max_elements));

element_levels_.resize(new_max_elements);

Expand Down Expand Up @@ -724,7 +723,7 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> {
std::vector<std::mutex>(max_elements).swap(link_list_locks_);
std::vector<std::mutex>(MAX_LABEL_OPERATION_LOCKS).swap(label_op_locks_);

visited_list_pool_ = new VisitedListPool(1, max_elements);
visited_list_pool_.reset(new VisitedListPool(1, max_elements));

linkLists_ = (char **) malloc(sizeof(void *) * max_elements);
if (linkLists_ == nullptr)
Expand Down

0 comments on commit 1ee95db

Please sign in to comment.