Skip to content

Commit 0df757e

Browse files
authored
Merge pull request #474 from ttsugriy/uniq-ptr
Use unique_ptr to manage visited_list_pool_
2 parents e7c66c8 + 1ee95db commit 0df757e

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

hnswlib/hnswalg.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <assert.h>
99
#include <unordered_set>
1010
#include <list>
11+
#include <memory>
1112

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

36-
VisitedListPool *visited_list_pool_{nullptr};
37+
std::unique_ptr<VisitedListPool> visited_list_pool_{nullptr};
3738

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

123124
cur_element_count = 0;
124125

125-
visited_list_pool_ = new VisitedListPool(1, max_elements);
126+
visited_list_pool_ = std::unique_ptr<VisitedListPool>(new VisitedListPool(1, max_elements));
126127

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

150150

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

576-
delete visited_list_pool_;
577-
visited_list_pool_ = new VisitedListPool(1, new_max_elements);
576+
visited_list_pool_.reset(new VisitedListPool(1, new_max_elements));
578577

579578
element_levels_.resize(new_max_elements);
580579

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

727-
visited_list_pool_ = new VisitedListPool(1, max_elements);
726+
visited_list_pool_.reset(new VisitedListPool(1, max_elements));
728727

729728
linkLists_ = (char **) malloc(sizeof(void *) * max_elements);
730729
if (linkLists_ == nullptr)

0 commit comments

Comments
 (0)