From cae162ffc67aefa139176d2a6f0f0f53cf1f80ab Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Fri, 6 Oct 2023 12:26:53 -0700 Subject: [PATCH] Ensure order is deterministic for SelectNeighbors closer caching --- src/hnswutils.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/hnswutils.c b/src/hnswutils.c index f1465d0..f9610f0 100644 --- a/src/hnswutils.c +++ b/src/hnswutils.c @@ -887,6 +887,12 @@ CompareCandidateDistances(const void *a, const void *b) if (hca->distance > hcb->distance) return -1; + if (hca->element < hcb->element) + return 1; + + if (hca->element > hcb->element) + return -1; + return 0; } @@ -1048,6 +1054,10 @@ HnswInsertElement(HnswElement element, HnswElement entryPoint, Relation index, F else lw = w; + /* Ensure order is deterministic for SelectNeighbors closer caching */ + if (index == NULL) + list_sort(lw, CompareCandidateDistances); + neighbors = SelectNeighbors(lw, lm, lc, procinfo, collation, element, NULL, NULL); AddConnections(element, neighbors, lm, lc);