Skip to content

Commit 1ff0032

Browse files
committed
Code refine
1 parent c52a57f commit 1ff0032

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
@@ -572,7 +572,7 @@ void TieredHNSWIndex<DataType, DistType>::executeInsertJob(HNSWInsertJob *job) {
572572
// corresponding job id. Note that after calling deleteVectorById, the last id's label
573573
// shouldn't be available, since it is removed from the lookup.
574574
labelType last_vec_label =
575-
this->frontendIndex->getLabelByInternalId(this->frontendIndex->indexSize() - 1);
575+
this->frontendIndex->getVectorLabel(this->frontendIndex->indexSize() - 1);
576576
int deleted = this->frontendIndex->deleteVectorById(job->label, job->id);
577577
if (deleted && job->id != this->frontendIndex->indexSize()) {
578578
// 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
@@ -370,7 +370,7 @@ class TieredSVSIndex : public VecSimTieredIndex<DataType, float> {
370370
// calculating all the distances and access the BF index. We take the lock on this
371371
// call.
372372
auto cur_flat_results = [this, n_res]() {
373-
std::shared_lock<std::shared_mutex> flat_lock{index->flatIndexGuard};
373+
std::shared_lock flat_lock{index->flatIndexGuard};
374374
return flat_iterator->getNextResults(n_res, BY_SCORE_THEN_ID);
375375
}();
376376
// This is also the only time `getNextResults` on the BF iterator can fail.
@@ -537,20 +537,20 @@ class TieredSVSIndex : public VecSimTieredIndex<DataType, float> {
537537
std::vector<DataType> vectors_to_move;
538538

539539
{ // lock frontendIndex from modifications
540-
std::shared_lock<std::shared_mutex> frontend_lock{this->flatIndexGuard};
540+
std::shared_lock flat_lock{this->flatIndexGuard};
541541

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

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

@@ -589,6 +589,8 @@ class TieredSVSIndex : public VecSimTieredIndex<DataType, float> {
589589
}
590590
}
591591
// delete vectors from the frontend index in reverse order
592+
// it increases the chance of avoiding swaps in the frontend index and performance
593+
// improvement
592594
size_t deleted = 0;
593595
for (idType i = labels_to_move.size(); i > 0; --i) {
594596
auto label = labels_to_move[i - 1];
@@ -678,13 +680,13 @@ class TieredSVSIndex : public VecSimTieredIndex<DataType, float> {
678680
// Async mode - add vector to the frontend index and schedule an update job if needed.
679681
if (!this->backendIndex->isMultiValue()) {
680682
// Remove vector from the backend index if it exists in case of non-MULTI.
681-
std::scoped_lock lock(this->mainIndexGuard);
683+
std::lock_guard lock(this->mainIndexGuard);
682684
ret -= svs_index->deleteVectors(&label, 1);
683685
// If main index is empty then update_threshold is trainingTriggerThreshold,
684686
// overwise it is 1.
685687
}
686688
{
687-
std::shared_lock<std::shared_mutex> lock(this->mainIndexGuard);
689+
std::shared_lock lock(this->mainIndexGuard);
688690
update_threshold = this->backendIndex->indexSize() == 0 ? this->trainingTriggerThreshold
689691
: this->updateTriggerThreshold;
690692
}
@@ -875,7 +877,7 @@ class TieredSVSIndex : public VecSimTieredIndex<DataType, float> {
875877
void runGC() override {
876878
TIERED_LOG(VecSimCommonStrings::LOG_VERBOSE_STRING,
877879
"running asynchronous GC for tiered SVS index");
878-
std::unique_lock<std::shared_mutex> backend_lock{this->mainIndexGuard};
880+
std::lock_guard lock{this->mainIndexGuard};
879881
// VecSimIndexAbstract::runGC() is protected
880882
static_cast<VecSimIndexInterface *>(this->backendIndex)->runGC();
881883
}

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)