Skip to content

Commit 9abd6b4

Browse files
alonre24meiravgri
authored andcommitted
let the capacity of idToLabel in flat index be tight to the size. This requires changing a bit the logic of updating jobs id after removing a vector from flat buffer and swap ids. (#367)
1 parent f315f70 commit 9abd6b4

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

src/VecSim/algorithms/brute_force/brute_force.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ void BruteForceIndex<DataType, DistType>::appendVector(const void *vector_data,
161161
size_t last_block_vectors_count = id % this->blockSize;
162162
this->idToLabelMapping.resize(
163163
idToLabelMapping_size + this->blockSize - last_block_vectors_count, 0);
164+
this->idToLabelMapping.shrink_to_fit();
164165
}
165166

166167
// add label to idToLabelMapping
@@ -215,6 +216,7 @@ void BruteForceIndex<DataType, DistType>::removeVector(idType id_to_delete) {
215216
if (this->count + this->blockSize <= idToLabel_size) {
216217
size_t vector_to_align_count = idToLabel_size % this->blockSize;
217218
this->idToLabelMapping.resize(idToLabel_size - this->blockSize - vector_to_align_count);
219+
this->idToLabelMapping.shrink_to_fit();
218220
}
219221
}
220222
}

tests/unit/test_allocator.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ TYPED_TEST(IndexAllocatorTest, test_bf_index_block_size_1) {
164164
// collection allocate additional structures for their internal implementation.
165165
ASSERT_EQ(allocator->getAllocationSize(),
166166
expectedAllocationSize + deleteCommandAllocationDelta);
167-
ASSERT_LE(expectedAllocationSize + expectedAllocationDelta, allocator->getAllocationSize());
168-
ASSERT_LE(expectedAllocationDelta, deleteCommandAllocationDelta);
167+
ASSERT_GE(expectedAllocationSize + expectedAllocationDelta, allocator->getAllocationSize());
168+
ASSERT_GE(expectedAllocationDelta, deleteCommandAllocationDelta);
169169

170170
info = bfIndex->info();
171171
ASSERT_EQ(allocator->getAllocationSize(), info.commonInfo.memory);

tests/unit/test_bruteforce.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ TYPED_TEST(BruteForceTest, brute_force_vector_update_test) {
108108

109109
TYPED_TEST(BruteForceTest, resize_and_align_index) {
110110
size_t dim = 4;
111-
size_t n = 15;
111+
size_t n = 14;
112112
size_t blockSize = 10;
113113

114114
BFParams params = {
@@ -134,24 +134,26 @@ TYPED_TEST(BruteForceTest, resize_and_align_index) {
134134

135135
// Add another vector, since index size equals to the capacity, this should cause resizing
136136
// (to fit a multiplication of block_size).
137-
GenerateAndAddVector<TEST_DATA_T>(index, dim, n + 1);
137+
GenerateAndAddVector<TEST_DATA_T>(index, dim, n);
138138
ASSERT_EQ(VecSimIndex_IndexSize(index), n + 1);
139139
// Check new capacity size, should be blockSize * 2.
140140
ASSERT_EQ(bf_index->idToLabelMapping.size(), 2 * blockSize);
141+
ASSERT_EQ(bf_index->idToLabelMapping.capacity(), 2 * blockSize);
141142

142-
// Now size = n + 1 = 16, capacity = 2* bs = 20. Test capacity overflow again
143-
// to check that it stays aligned with blocksize.
143+
// Now size = n + 1 (= 15), capacity = 2 * bs (= 20). Test capacity overflow again
144+
// to check that it stays aligned with block size.
144145

145146
size_t add_vectors_count = 8;
146147
for (size_t i = 0; i < add_vectors_count; i++) {
147148
GenerateAndAddVector<TEST_DATA_T>(index, dim, n + 2 + i, i);
148149
}
149150

150-
// Size should be n + 1 + 8 = 24.
151+
// Size should be n + 1 + 8 (= 25).
151152
ASSERT_EQ(VecSimIndex_IndexSize(index), n + 1 + add_vectors_count);
152153

153154
// Check new capacity size, should be blockSize * 3.
154155
ASSERT_EQ(bf_index->idToLabelMapping.size(), 3 * blockSize);
156+
ASSERT_EQ(bf_index->idToLabelMapping.capacity(), 3 * blockSize);
155157

156158
VecSimIndex_Free(index);
157159
}
@@ -170,7 +172,7 @@ TYPED_TEST(BruteForceTest, resize_and_align_index_largeInitialCapacity) {
170172
BruteForceIndex<TEST_DATA_T, TEST_DIST_T> *bf_index = this->CastToBF(index);
171173
ASSERT_EQ(VecSimIndex_IndexSize(index), 0);
172174

173-
// add up to blocksize + 1 = 3 + 1 = 4
175+
// Add up to block size + 1 = 3 + 1 = 4
174176
for (size_t i = 0; i < bs + 1; i++) {
175177
GenerateAndAddVector<TEST_DATA_T>(index, dim, i, i);
176178
}
@@ -190,6 +192,7 @@ TYPED_TEST(BruteForceTest, resize_and_align_index_largeInitialCapacity) {
190192
// 10 - 3 - 10 % 3 (1) = 6
191193
idToLabelMapping_size = bf_index->idToLabelMapping.size();
192194
ASSERT_EQ(idToLabelMapping_size, n - bs - n % bs);
195+
ASSERT_EQ(idToLabelMapping_size, bf_index->idToLabelMapping.capacity());
193196

194197
// Delete all the vectors to decrease idToLabelMapping size by another bs.
195198
size_t i = 0;
@@ -198,19 +201,24 @@ TYPED_TEST(BruteForceTest, resize_and_align_index_largeInitialCapacity) {
198201
++i;
199202
}
200203
ASSERT_EQ(bf_index->idToLabelMapping.size(), bs);
204+
ASSERT_EQ(bf_index->idToLabelMapping.capacity(), bs);
205+
201206
// Add and delete a vector to achieve:
202207
// size % block_size == 0 && size + bs <= idToLabelMapping_size(3).
203208
// idToLabelMapping_size should be resized to zero.
204209
GenerateAndAddVector<TEST_DATA_T>(index, dim, 0);
205210
VecSimIndex_DeleteVector(index, 0);
206211
ASSERT_EQ(bf_index->idToLabelMapping.size(), 0);
212+
ASSERT_EQ(bf_index->idToLabelMapping.capacity(), 0);
207213

208214
// Do it again. This time after adding a vector idToLabelMapping_size is increased by bs.
209215
// Upon deletion it will be resized to zero again.
210216
GenerateAndAddVector<TEST_DATA_T>(index, dim, 0);
211217
ASSERT_EQ(bf_index->idToLabelMapping.size(), bs);
218+
ASSERT_EQ(bf_index->idToLabelMapping.capacity(), bs);
212219
VecSimIndex_DeleteVector(index, 0);
213220
ASSERT_EQ(bf_index->idToLabelMapping.size(), 0);
221+
ASSERT_EQ(bf_index->idToLabelMapping.capacity(), 0);
214222

215223
VecSimIndex_Free(index);
216224
}

0 commit comments

Comments
 (0)