Skip to content

Commit 124018b

Browse files
committed
Added HnswInitSearchCandidate function
1 parent 35b252a commit 124018b

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

src/hnswutils.c

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -581,21 +581,34 @@ GetElementDistance(char *base, HnswElement element, HnswQuery * q, HnswSupport *
581581
return HnswGetDistance(q->value, value, support);
582582
}
583583

584+
/*
585+
* Allocate a search candidate
586+
*/
587+
static HnswSearchCandidate *
588+
HnswInitSearchCandidate(char *base, HnswElement element, double distance)
589+
{
590+
HnswSearchCandidate *sc = palloc(sizeof(HnswSearchCandidate));
591+
592+
HnswPtrStore(base, sc->element, element);
593+
sc->distance = distance;
594+
return sc;
595+
}
596+
584597
/*
585598
* Create a candidate for the entry point
586599
*/
587600
HnswSearchCandidate *
588601
HnswEntryCandidate(char *base, HnswElement entryPoint, HnswQuery * q, Relation index, HnswSupport * support, bool loadVec)
589602
{
590-
HnswSearchCandidate *sc = palloc(sizeof(HnswSearchCandidate));
591603
bool inMemory = index == NULL;
604+
double distance;
592605

593-
HnswPtrStore(base, sc->element, entryPoint);
594606
if (inMemory)
595-
sc->distance = GetElementDistance(base, entryPoint, q, support);
607+
distance = GetElementDistance(base, entryPoint, q, support);
596608
else
597-
HnswLoadElement(entryPoint, &sc->distance, q, index, support, loadVec, NULL);
598-
return sc;
609+
HnswLoadElement(entryPoint, &distance, q, index, support, loadVec, NULL);
610+
611+
return HnswInitSearchCandidate(base, entryPoint, distance);
599612
}
600613

601614
/*
@@ -912,9 +925,7 @@ HnswSearchLayer(char *base, HnswQuery * q, List *ep, int ef, int lc, Relation in
912925
if (discarded != NULL)
913926
{
914927
/* Create a new candidate */
915-
e = palloc(sizeof(HnswSearchCandidate));
916-
HnswPtrStore(base, e->element, eElement);
917-
e->distance = eDistance;
928+
e = HnswInitSearchCandidate(base, eElement, eDistance);
918929
pairingheap_add(*discarded, &e->w_node);
919930
}
920931

@@ -926,9 +937,7 @@ HnswSearchLayer(char *base, HnswQuery * q, List *ep, int ef, int lc, Relation in
926937
continue;
927938

928939
/* Create a new candidate */
929-
e = palloc(sizeof(HnswSearchCandidate));
930-
HnswPtrStore(base, e->element, eElement);
931-
e->distance = eDistance;
940+
e = HnswInitSearchCandidate(base, eElement, eDistance);
932941
pairingheap_add(C, &e->c_node);
933942
pairingheap_add(W, &e->w_node);
934943

0 commit comments

Comments
 (0)