Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/VecSim/algorithms/brute_force/brute_force.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <cstring>
#include <queue>
#include <cassert>
#include <cmath>

using namespace std;

Expand Down Expand Up @@ -76,7 +77,7 @@ int BruteForceIndex::addVector(const void *vector_data, size_t label) {

// See if new id is bigger than current vector count. Needs to resize the index.
if (id >= this->idToVectorBlockMemberMapping.size()) {
this->idToVectorBlockMemberMapping.resize(this->count * 2);
this->idToVectorBlockMemberMapping.resize(std::ceil(this->count * 1.1));
}

// Get vector block to store the vector in.
Expand Down
3 changes: 2 additions & 1 deletion src/VecSim/algorithms/brute_force/brute_force.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ class BruteForceIndex : public VecSimIndex {
DISTFUNC<float> dist_func;
VecSearchMode last_mode;
#ifdef BUILD_TESTS
// Allow the following tests to access the index size private member.
// Allow the following tests to access the index private members.
friend class BruteForceTest_preferAdHocOptimization_Test;
friend class BruteForceTest_test_dynamic_bf_info_iterator_Test;
friend class BruteForceTest_resizeIndex_Test;
#endif
};
3 changes: 2 additions & 1 deletion src/VecSim/algorithms/hnsw/hnsw_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ int HNSWIndex::addVector(const void *vector_data, size_t id) {
vector_data = normalized_data;
}
if (hnsw->getIndexSize() == this->hnsw->getIndexCapacity()) {
this->hnsw->resizeIndex(std::max<size_t>(this->hnsw->getIndexCapacity() * 2, 2));
this->hnsw->resizeIndex(
std::max<size_t>(std::ceil(this->hnsw->getIndexCapacity() * 1.1), 2));
}
this->hnsw->addPoint(vector_data, id);
return true;
Expand Down
29 changes: 29 additions & 0 deletions tests/unit/test_bruteforce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,35 @@ TEST_F(BruteForceTest, brute_force_vector_add_test) {
VecSimIndex_Free(index);
}

TEST_F(BruteForceTest, resizeIndex) {
size_t dim = 4;
size_t n = 15;
VecSimParams params{.algo = VecSimAlgo_BF,
.bfParams = BFParams{.type = VecSimType_FLOAT32,
.dim = dim,
.metric = VecSimMetric_L2,
.initialCapacity = n}};
VecSimIndex *index = VecSimIndex_New(&params);
ASSERT_EQ(VecSimIndex_IndexSize(index), 0);

float a[dim];
for (size_t i = 0; i < n; i++) {
for (size_t j = 0; j < dim; j++) {
a[j] = (float)i;
}
VecSimIndex_AddVector(index, (const void *)a, i);
}
ASSERT_EQ(reinterpret_cast<BruteForceIndex *>(index)->idToVectorBlockMemberMapping.size(), n);

// Add another vector, since index size equals to the capacity, this should cause resizing
// (by 10% factor from the new index size).
VecSimIndex_AddVector(index, (const void *)a, n + 1);
ASSERT_EQ(VecSimIndex_IndexSize(index), n + 1);
ASSERT_EQ(reinterpret_cast<BruteForceIndex *>(index)->idToVectorBlockMemberMapping.size(),
std::ceil(1.1 * (n + 1)));
VecSimIndex_Free(index);
}

TEST_F(BruteForceTest, brute_force_vector_search_test_ip) {
size_t dim = 4;
size_t n = 100;
Expand Down
29 changes: 29 additions & 0 deletions tests/unit/test_hnswlib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,35 @@ TEST_F(HNSWLibTest, hnswlib_vector_add_test) {
VecSimIndex_Free(index);
}

TEST_F(HNSWLibTest, resizeIndex) {
size_t dim = 4;
size_t n = 15;
VecSimParams params{.algo = VecSimAlgo_HNSWLIB,
.hnswParams = HNSWParams{.type = VecSimType_FLOAT32,
.dim = dim,
.metric = VecSimMetric_L2,
.initialCapacity = n}};
VecSimIndex *index = VecSimIndex_New(&params);
ASSERT_EQ(VecSimIndex_IndexSize(index), 0);

float a[dim];
for (size_t i = 0; i < n; i++) {
for (size_t j = 0; j < dim; j++) {
a[j] = (float)i;
}
VecSimIndex_AddVector(index, (const void *)a, i);
}
ASSERT_EQ(reinterpret_cast<HNSWIndex *>(index)->getHNSWIndex()->getIndexCapacity(), n);

// Add another vector, since index size equals to the capacity, this should cause resizing
// (by 10% factor from the index size).
VecSimIndex_AddVector(index, (const void *)a, n + 1);
ASSERT_EQ(VecSimIndex_IndexSize(index), n + 1);
ASSERT_EQ(reinterpret_cast<HNSWIndex *>(index)->getHNSWIndex()->getIndexCapacity(),
std::ceil(1.1 * n));
VecSimIndex_Free(index);
}

TEST_F(HNSWLibTest, hnswlib_vector_search_test) {
size_t n = 100;
size_t k = 11;
Expand Down