@@ -581,21 +581,34 @@ GetElementDistance(char *base, HnswElement element, HnswQuery * q, HnswSupport *
581
581
return HnswGetDistance (q -> value ,value ,support );
582
582
}
583
583
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
+
584
597
/*
585
598
* Create a candidate for the entry point
586
599
*/
587
600
HnswSearchCandidate *
588
601
HnswEntryCandidate (char * base ,HnswElement entryPoint ,HnswQuery * q ,Relation index ,HnswSupport * support ,bool loadVec )
589
602
{
590
- HnswSearchCandidate * sc = palloc (sizeof (HnswSearchCandidate ));
591
603
bool inMemory = index == NULL ;
604
+ double distance ;
592
605
593
- HnswPtrStore (base ,sc -> element ,entryPoint );
594
606
if (inMemory )
595
- sc -> distance = GetElementDistance (base ,entryPoint ,q ,support );
607
+ distance = GetElementDistance (base ,entryPoint ,q ,support );
596
608
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 );
599
612
}
600
613
601
614
/*
@@ -912,9 +925,7 @@ HnswSearchLayer(char *base, HnswQuery * q, List *ep, int ef, int lc, Relation in
912
925
if (discarded != NULL )
913
926
{
914
927
/* 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 );
918
929
pairingheap_add (* discarded ,& e -> w_node );
919
930
}
920
931
@@ -926,9 +937,7 @@ HnswSearchLayer(char *base, HnswQuery * q, List *ep, int ef, int lc, Relation in
926
937
continue ;
927
938
928
939
/* 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 );
932
941
pairingheap_add (C ,& e -> c_node );
933
942
pairingheap_add (W ,& e -> w_node );
934
943