Skip to content

Commit 22cdb46

Browse files
author
Dmitry Yashunin
committed
Refactoring
1 parent 34fe7f1 commit 22cdb46

File tree

1 file changed

+9
-17
lines changed

1 file changed

+9
-17
lines changed

hnswlib/hnswalg.h

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -721,8 +721,6 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> {
721721
}
722722
tableint internalId = search->second;
723723
lock_table.unlock();
724-
// wait for element addition or update
725-
std::unique_lock <std::mutex> lock_el_update(link_list_update_locks_[(internalId & (max_update_element_locks - 1))]);
726724
char* data_ptrv = getDataByInternalId(internalId);
727725
size_t dim = *((size_t *) dist_func_param_);
728726
std::vector<data_t> data;
@@ -746,8 +744,6 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> {
746744
}
747745
tableint internalId = search->second;
748746
lock_table.unlock();
749-
// wait for element addition or update
750-
std::unique_lock <std::mutex> lock_el_update(link_list_update_locks_[(internalId & (max_update_element_locks - 1))]);
751747
markDeletedInternal(internalId);
752748
}
753749

@@ -773,8 +769,11 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> {
773769

774770

775771
/**
776-
* Remove the deleted mark of the node, does NOT really change the current graph.
777-
*/
772+
* Remove the deleted mark of the node, does NOT really change the current graph.
773+
*
774+
* Note: the method is not safe to use when replacement of deleted elements is enabled
775+
* bacause elements marked as deleted can be completely removed from the index
776+
*/
778777
void unmarkDelete(labeltype label) {
779778
std::unique_lock <std::mutex> lock_table(label_lookup_lock);
780779
auto search = label_lookup_.find(label);
@@ -783,19 +782,14 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> {
783782
}
784783
tableint internalId = search->second;
785784
lock_table.unlock();
786-
// wait for element addition or update
787-
std::unique_lock <std::mutex> lock_el_update(link_list_update_locks_[(internalId & (max_update_element_locks - 1))]);
788785
unmarkDeletedInternal(internalId);
789786
}
790787

791788

792789

793790
/**
794-
* Remove the deleted mark of the node.
795-
*
796-
* Note: the method is not safe to use when replacement of deleted elements is enabled
797-
* bacause elements marked as deleted can be completely removed from the index
798-
*/
791+
* Remove the deleted mark of the node.
792+
*/
799793
void unmarkDeletedInternal(tableint internalId) {
800794
assert(internalId < cur_element_count);
801795
if (isMarkedDeleted(internalId)) {
@@ -860,18 +854,16 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> {
860854
addPoint(data_point, label);
861855
return label;
862856
} else {
863-
// wait for element addition or update
864-
std::unique_lock <std::mutex> lock_el_update(link_list_update_locks_[(internal_id_replaced & (max_update_element_locks - 1))]);
857+
// no need to protect element from additions and updates as
858+
// we assume that there are no concurrent operations on deleted element
865859
labeltype label_replaced = getExternalLabel(internal_id_replaced);
866860
setExternalLabel(internal_id_replaced, label);
867-
lock_el_update.unlock();
868861

869862
std::unique_lock <std::mutex> lock_table(label_lookup_lock);
870863
label_lookup_.erase(label_replaced);
871864
label_lookup_[label] = internal_id_replaced;
872865
lock_table.unlock();
873866

874-
lock_el_update.lock();
875867
unmarkDeletedInternal(internal_id_replaced);
876868
updatePoint(data_point, internal_id_replaced, 1.0);
877869

0 commit comments

Comments
 (0)