pgvector.git / summary / log / commit / refs
commit 124018b8dd24b24ae43e2cc67b591f193b9d2cab
Author: Andrew Kane <andrew@ankane.org>
Date: Fri Oct 11 02:30:47 2024 +0000
Added HnswInitSearchCandidate function
src/hnswutils.c | 31 ++++++++++++++++++++-----------
1 file changed, 20 insertions(+), 11 deletions(-)
diff --git a/src/hnswutils.c b/src/hnswutils.c
index 732fcd72..e84561ea 100644
--- a/src/hnswutils.c
+++ b/src/hnswutils.c
@@ -581,21 +581,34 @@ GetElementDistance(char *base, HnswElement element, HnswQuery * q, HnswSupport *
return HnswGetDistance(q->value, value, support);
}
+/*
+ * Allocate a search candidate
+ */
+static HnswSearchCandidate *
+HnswInitSearchCandidate(char *base, HnswElement element, double distance)
+{
+ HnswSearchCandidate *sc = palloc(sizeof(HnswSearchCandidate));
+
+ HnswPtrStore(base, sc->element, element);
+ sc->distance = distance;
+ return sc;
+}
+
/*
* Create a candidate for the entry point
*/
HnswSearchCandidate *
HnswEntryCandidate(char *base, HnswElement entryPoint, HnswQuery * q, Relation index, HnswSupport * support, bool loadVec)
{
- HnswSearchCandidate *sc = palloc(sizeof(HnswSearchCandidate));
bool inMemory = index == NULL;
+ double distance;
- HnswPtrStore(base, sc->element, entryPoint);
if (inMemory)
- sc->distance = GetElementDistance(base, entryPoint, q, support);
+ distance = GetElementDistance(base, entryPoint, q, support);
else
- HnswLoadElement(entryPoint, &sc->distance, q, index, support, loadVec, NULL);
- return sc;
+ HnswLoadElement(entryPoint, &distance, q, index, support, loadVec, NULL);
+
+ return HnswInitSearchCandidate(base, entryPoint, distance);
}
/*
@@ -912,9 +925,7 @@ HnswSearchLayer(char *base, HnswQuery * q, List *ep, int ef, int lc, Relation in
if (discarded != NULL)
{
/* Create a new candidate */
- e = palloc(sizeof(HnswSearchCandidate));
- HnswPtrStore(base, e->element, eElement);
- e->distance = eDistance;
+ e = HnswInitSearchCandidate(base, eElement, eDistance);
pairingheap_add(*discarded, &e->w_node);
}
@@ -926,9 +937,7 @@ HnswSearchLayer(char *base, HnswQuery * q, List *ep, int ef, int lc, Relation in
continue;
/* Create a new candidate */
- e = palloc(sizeof(HnswSearchCandidate));
- HnswPtrStore(base, e->element, eElement);
- e->distance = eDistance;
+ e = HnswInitSearchCandidate(base, eElement, eDistance);
pairingheap_add(C, &e->c_node);
pairingheap_add(W, &e->w_node);
[parent: 35b252a3e371]