HNSW: use node max link property.

This is both more correct in formal terms, and in practical
terms as well, as we could over-allocate nodes sometimes.
This commit is contained in:
antirez 2025-03-15 10:30:14 +01:00
parent 68d3067125
commit 6e434bcaaf
1 changed files with 1 additions and 3 deletions

4
hnsw.c
View File

@ -938,8 +938,6 @@ void hnsw_update_worst_neighbor_on_remove(HNSW *index, hnswNode *node, uint32_t
void select_neighbors(HNSW *index, pqueue *candidates, hnswNode *new_node,
uint32_t layer, uint32_t required_links, int aggressive)
{
uint32_t max_links = (layer == 0) ? index->M*2 : index->M;
for (uint32_t i = 0; i < candidates->count; i++) {
hnswNode *neighbor = pq_get_node(candidates,i);
if (neighbor == new_node) continue; // Don't link node with itself.
@ -990,7 +988,7 @@ void select_neighbors(HNSW *index, pqueue *candidates, hnswNode *new_node,
/* If potential neighbor node has space, simply add the new link.
* We will have space as well. */
uint32_t n = neighbor->layers[layer].num_links;
if (n < max_links) {
if (n < neighbor->layers[layer].max_links) {
/* Link candidate to new node. */
neighbor->layers[layer].links[n] = new_node;
neighbor->layers[layer].num_links++;