Skip to content
Open
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
26 changes: 26 additions & 0 deletions testing/vector_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,32 @@ TEST_F(VectorIndexTest, HnswAddPointReplaceDeletedDoesNotDuplicateLabel) {
EXPECT_FALSE(algo.isMarkedDeleted(1));
}

TEST_F(VectorIndexTest, HnswHandlesEmptyNeighborLists) {
hnswlib::L2Space space{1};
VectorHNSW<float>::HNSWIndex algo(
&space, /*max_elements=*/2, /*normalized=*/false, /*m_value=*/2,
/*ef_construction=*/10, /*allow_replace_deleted=*/false);
auto query = [](float value) {
absl::string_view bytes(reinterpret_cast<const char *>(&value),
sizeof(value));
return QueryVector(VectorRecord::Construct(bytes, kDefaultMagnitude),
bytes.size(), false);
};
auto entry = query(0.0f);
auto point = query(1.0f);
auto update = query(2.0f);
algo.addPoint(std::move(entry), 0, 2);
auto *links = algo.get_linklist_at_level(0, 0);
algo.setListCount(links, 0);
reinterpret_cast<hnswlib::tableint *>(links + 1)[0] = algo.max_elements_;
EXPECT_NO_THROW(algo.searchBaseLayer(0, point, 0));
algo.addPoint(std::move(point), 1, 1);
links = algo.get_linklist_at_level(0, 2);
algo.setListCount(links, 0);
reinterpret_cast<hnswlib::tableint *>(links + 1)[0] = algo.max_elements_;
EXPECT_NO_THROW(algo.updatePoint(std::move(update), 1, 1.0));
}

// ---- Happy path ----------------------------------------------------------
TEST_F(VectorIndexTest, LoadValidatesEmptyIndex) {
auto golden = BuildGoldenChunks({}, kGoldenMax);
Expand Down
5 changes: 3 additions & 2 deletions third_party/hnswlib/hnswalg.h
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,9 @@ class HierarchicalNSW
size_t size = getListCount((linklistsizeint *)data);
tableint *datal = (tableint *)(data + 1);
#ifdef USE_PREFETCH
__builtin_prefetch((char *)(visited_array + *(data + 1)), 0, 3);
__builtin_prefetch((char *)(visited_array + *(data + 1) + 64), 0, 3);
if (size > 0) {
__builtin_prefetch((char *)(visited_array + *datal), 0, 3);
__builtin_prefetch((char *)(visited_array + *datal + 64), 0, 3);
__builtin_prefetch(GetDataByInternalId(*datal)->GetRawVector(), 0, 3);
}
if (size > 1) {
Expand Down Expand Up @@ -1516,6 +1516,7 @@ class HierarchicalNSW
std::unique_lock<std::mutex> lock(link_list_locks_[currObj]);
data = get_linklist_at_level(currObj, level);
int size = getListCount(data);
if (size == 0) break;
tableint *datal = (tableint *)(data + 1);
#ifdef USE_PREFETCH
__builtin_prefetch(GetDataByInternalId(*datal)->GetRawVector(), 0, 3);
Expand Down
Loading