Skip to content

Commit e03ac92

Browse files
authored
change the resize index factor to 1.1 instead of 2 (#148)
1 parent 497d228 commit e03ac92

File tree

5 files changed

+64
-3
lines changed

5 files changed

+64
-3
lines changed

src/VecSim/algorithms/brute_force/brute_force.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <cstring>
1111
#include <queue>
1212
#include <cassert>
13+
#include <cmath>
1314

1415
using namespace std;
1516

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

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

8283
// Get vector block to store the vector in.

src/VecSim/algorithms/brute_force/brute_force.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ class BruteForceIndex : public VecSimIndex {
4444
DISTFUNC<float> dist_func;
4545
VecSearchMode last_mode;
4646
#ifdef BUILD_TESTS
47-
// Allow the following tests to access the index size private member.
47+
// Allow the following tests to access the index private members.
4848
friend class BruteForceTest_preferAdHocOptimization_Test;
4949
friend class BruteForceTest_test_dynamic_bf_info_iterator_Test;
50+
friend class BruteForceTest_resizeIndex_Test;
5051
#endif
5152
};

src/VecSim/algorithms/hnsw/hnsw_wrapper.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ int HNSWIndex::addVector(const void *vector_data, size_t id) {
4040
vector_data = normalized_data;
4141
}
4242
if (hnsw->getIndexSize() == this->hnsw->getIndexCapacity()) {
43-
this->hnsw->resizeIndex(std::max<size_t>(this->hnsw->getIndexCapacity() * 2, 2));
43+
this->hnsw->resizeIndex(
44+
std::max<size_t>(std::ceil(this->hnsw->getIndexCapacity() * 1.1), 2));
4445
}
4546
this->hnsw->addPoint(vector_data, id);
4647
return true;

tests/unit/test_bruteforce.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,35 @@ TEST_F(BruteForceTest, brute_force_vector_add_test) {
3535
VecSimIndex_Free(index);
3636
}
3737

38+
TEST_F(BruteForceTest, resizeIndex) {
39+
size_t dim = 4;
40+
size_t n = 15;
41+
VecSimParams params{.algo = VecSimAlgo_BF,
42+
.bfParams = BFParams{.type = VecSimType_FLOAT32,
43+
.dim = dim,
44+
.metric = VecSimMetric_L2,
45+
.initialCapacity = n}};
46+
VecSimIndex *index = VecSimIndex_New(&params);
47+
ASSERT_EQ(VecSimIndex_IndexSize(index), 0);
48+
49+
float a[dim];
50+
for (size_t i = 0; i < n; i++) {
51+
for (size_t j = 0; j < dim; j++) {
52+
a[j] = (float)i;
53+
}
54+
VecSimIndex_AddVector(index, (const void *)a, i);
55+
}
56+
ASSERT_EQ(reinterpret_cast<BruteForceIndex *>(index)->idToVectorBlockMemberMapping.size(), n);
57+
58+
// Add another vector, since index size equals to the capacity, this should cause resizing
59+
// (by 10% factor from the new index size).
60+
VecSimIndex_AddVector(index, (const void *)a, n + 1);
61+
ASSERT_EQ(VecSimIndex_IndexSize(index), n + 1);
62+
ASSERT_EQ(reinterpret_cast<BruteForceIndex *>(index)->idToVectorBlockMemberMapping.size(),
63+
std::ceil(1.1 * (n + 1)));
64+
VecSimIndex_Free(index);
65+
}
66+
3867
TEST_F(BruteForceTest, brute_force_vector_search_test_ip) {
3968
size_t dim = 4;
4069
size_t n = 100;

tests/unit/test_hnswlib.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,35 @@ TEST_F(HNSWLibTest, hnswlib_vector_add_test) {
4040
VecSimIndex_Free(index);
4141
}
4242

43+
TEST_F(HNSWLibTest, resizeIndex) {
44+
size_t dim = 4;
45+
size_t n = 15;
46+
VecSimParams params{.algo = VecSimAlgo_HNSWLIB,
47+
.hnswParams = HNSWParams{.type = VecSimType_FLOAT32,
48+
.dim = dim,
49+
.metric = VecSimMetric_L2,
50+
.initialCapacity = n}};
51+
VecSimIndex *index = VecSimIndex_New(&params);
52+
ASSERT_EQ(VecSimIndex_IndexSize(index), 0);
53+
54+
float a[dim];
55+
for (size_t i = 0; i < n; i++) {
56+
for (size_t j = 0; j < dim; j++) {
57+
a[j] = (float)i;
58+
}
59+
VecSimIndex_AddVector(index, (const void *)a, i);
60+
}
61+
ASSERT_EQ(reinterpret_cast<HNSWIndex *>(index)->getHNSWIndex()->getIndexCapacity(), n);
62+
63+
// Add another vector, since index size equals to the capacity, this should cause resizing
64+
// (by 10% factor from the index size).
65+
VecSimIndex_AddVector(index, (const void *)a, n + 1);
66+
ASSERT_EQ(VecSimIndex_IndexSize(index), n + 1);
67+
ASSERT_EQ(reinterpret_cast<HNSWIndex *>(index)->getHNSWIndex()->getIndexCapacity(),
68+
std::ceil(1.1 * n));
69+
VecSimIndex_Free(index);
70+
}
71+
4372
TEST_F(HNSWLibTest, hnswlib_vector_search_test) {
4473
size_t n = 100;
4574
size_t k = 11;

0 commit comments

Comments
 (0)