Skip to content

Commit c9a1785

Browse files
committed
Code refine
1 parent d3d444b commit c9a1785

File tree

5 files changed

+14
-13
lines changed

5 files changed

+14
-13
lines changed

src/VecSim/algorithms/brute_force/brute_force.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,6 @@ class BruteForceIndex : public VecSimIndexAbstract<DataType, DistType> {
5959

6060
const RawDataContainer *getVectorsContainer() const { return this->vectors; }
6161

62-
const labelType getLabelByInternalId(idType internal_id) const {
63-
return idToLabelMapping.at(internal_id);
64-
}
6562
// Remove a specific vector that is stored under a label from the index by its internal id.
6663
virtual int deleteVectorById(labelType label, idType id) = 0;
6764
// Remove a vector and return a map between internal ids and the original internal ids of the

src/VecSim/algorithms/brute_force/brute_force_multi.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,15 @@ class BruteForceIndex_Multi : public BruteForceIndex<DataType, DistType> {
9595
}
9696
return keys;
9797
};
98+
9899
inline vecsim_stl::vector<idType> getElementIds(size_t label) const override {
99100
auto it = labelToIdsLookup.find(label);
100101
if (it == labelToIdsLookup.end()) {
101102
return vecsim_stl::vector<idType>{this->allocator}; // return an empty collection
102103
}
103104
return it->second;
104105
}
106+
105107
inline vecsim_stl::abstract_priority_queue<DistType, labelType> *
106108
getNewMaxPriorityQueue() const override {
107109
return new (this->allocator)

src/VecSim/algorithms/hnsw/hnsw_tiered.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ void TieredHNSWIndex<DataType, DistType>::executeInsertJob(HNSWInsertJob *job) {
571571
// corresponding job id. Note that after calling deleteVectorById, the last id's label
572572
// shouldn't be available, since it is removed from the lookup.
573573
labelType last_vec_label =
574-
this->frontendIndex->getLabelByInternalId(this->frontendIndex->indexSize() - 1);
574+
this->frontendIndex->getVectorLabel(this->frontendIndex->indexSize() - 1);
575575
int deleted = this->frontendIndex->deleteVectorById(job->label, job->id);
576576
if (deleted && job->id != this->frontendIndex->indexSize()) {
577577
// If the vector removal caused a swap with the last id, update the relevant insert job.

src/VecSim/algorithms/svs/svs_tiered.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ class TieredSVSIndex : public VecSimTieredIndex<DataType, float> {
371371
// calculating all the distances and access the BF index. We take the lock on this
372372
// call.
373373
auto cur_flat_results = [this, n_res]() {
374-
std::shared_lock<std::shared_mutex> flat_lock{index->flatIndexGuard};
374+
std::shared_lock flat_lock{index->flatIndexGuard};
375375
return flat_iterator->getNextResults(n_res, BY_SCORE_THEN_ID);
376376
}();
377377
// This is also the only time `getNextResults` on the BF iterator can fail.
@@ -538,20 +538,20 @@ class TieredSVSIndex : public VecSimTieredIndex<DataType, float> {
538538
std::vector<DataType> vectors_to_move;
539539

540540
{ // lock frontendIndex from modifications
541-
std::shared_lock<std::shared_mutex> frontend_lock{this->flatIndexGuard};
541+
std::shared_lock flat_lock{this->flatIndexGuard};
542542

543543
auto flat_index = this->GetFlatIndex();
544-
auto frontend_index_size = this->frontendIndex->indexSize();
544+
const auto frontend_index_size = this->frontendIndex->indexSize();
545545
const size_t dim = flat_index->getDim();
546546
labels_to_move.reserve(frontend_index_size);
547547
vectors_to_move.reserve(frontend_index_size * dim);
548548

549-
for (idType i = 0; i < frontend_index_size; ++i) {
549+
for (idType i = 0; i < frontend_index_size; i++) {
550550
labels_to_move.push_back(flat_index->getVectorLabel(i));
551551
auto data = flat_index->getDataByInternalId(i);
552552
vectors_to_move.insert(vectors_to_move.end(), data, data + dim);
553553
}
554-
// restart journal from current frontend index state
554+
// reset journal to the current frontend index state
555555
swaps_journal.clear();
556556
} // release frontend index
557557

@@ -590,6 +590,8 @@ class TieredSVSIndex : public VecSimTieredIndex<DataType, float> {
590590
}
591591
}
592592
// delete vectors from the frontend index in reverse order
593+
// it increases the chance of avoiding swaps in the frontend index and performance
594+
// improvement
593595
size_t deleted = 0;
594596
for (idType i = labels_to_move.size(); i > 0; --i) {
595597
auto label = labels_to_move[i - 1];
@@ -679,13 +681,13 @@ class TieredSVSIndex : public VecSimTieredIndex<DataType, float> {
679681
// Async mode - add vector to the frontend index and schedule an update job if needed.
680682
if (!this->backendIndex->isMultiValue()) {
681683
// Remove vector from the backend index if it exists in case of non-MULTI.
682-
std::scoped_lock lock(this->mainIndexGuard);
684+
std::lock_guard lock(this->mainIndexGuard);
683685
ret -= svs_index->deleteVectors(&label, 1);
684686
// If main index is empty then update_threshold is trainingTriggerThreshold,
685687
// overwise it is 1.
686688
}
687689
{
688-
std::shared_lock<std::shared_mutex> lock(this->mainIndexGuard);
690+
std::shared_lock lock(this->mainIndexGuard);
689691
update_threshold = this->backendIndex->indexSize() == 0 ? this->trainingTriggerThreshold
690692
: this->updateTriggerThreshold;
691693
}
@@ -866,7 +868,7 @@ class TieredSVSIndex : public VecSimTieredIndex<DataType, float> {
866868
void runGC() override {
867869
TIERED_LOG(VecSimCommonStrings::LOG_VERBOSE_STRING,
868870
"running asynchronous GC for tiered SVS index");
869-
std::unique_lock<std::shared_mutex> backend_lock{this->mainIndexGuard};
871+
std::lock_guard lock{this->mainIndexGuard};
870872
// VecSimIndexAbstract::runGC() is protected
871873
static_cast<VecSimIndexInterface *>(this->backendIndex)->runGC();
872874
}

tests/unit/test_svs_tiered.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1095,7 +1095,7 @@ TYPED_TEST(SVSTieredIndexTest, parallelInsertSearch) {
10951095
auto sz_f = tiered_index->GetFlatIndex()->indexSize();
10961096
auto sz_b = tiered_index->GetBackendIndex()->indexSize();
10971097
EXPECT_LE(sz_f, this->getUpdateThreshold());
1098-
EXPECT_EQ(sz_f + sz_b, n) << "Flat index size: " << sz_f << ", Backend index size: " << sz_b;
1098+
EXPECT_EQ(sz_f + sz_b, n);
10991099
EXPECT_EQ(successful_searches, n);
11001100
EXPECT_EQ(mock_thread_pool.jobQ.size(), 0);
11011101
}

0 commit comments

Comments
 (0)