@@ -608,10 +608,10 @@ GetElementDistance(char *base, HnswElement element, Datum q, FmgrInfo *procinfo,
608
608
/*
609
609
* Create a candidate for the entry point
610
610
*/
611
- HnswCandidate *
611
+ HnswSearchCandidate *
612
612
HnswEntryCandidate (char * base , HnswElement entryPoint , Datum q , Relation index , FmgrInfo * procinfo , Oid collation , bool loadVec )
613
613
{
614
- HnswCandidate * hc = palloc (sizeof (HnswCandidate ));
614
+ HnswSearchCandidate * hc = palloc (sizeof (HnswSearchCandidate ));
615
615
616
616
HnswPtrStore (base , hc -> element , entryPoint );
617
617
if (index == NULL )
@@ -621,19 +621,19 @@ HnswEntryCandidate(char *base, HnswElement entryPoint, Datum q, Relation index,
621
621
return hc ;
622
622
}
623
623
624
- #define HnswGetPairingHeapCandidate (membername , ptr ) ( pairingheap_container(HnswPairingHeapNode , membername, ptr)->inner )
625
- #define HnswGetPairingHeapCandidateConst (membername , ptr ) ( pairingheap_const_container(HnswPairingHeapNode , membername, ptr)->inner )
624
+ #define HnswGetSearchCandidate (membername , ptr ) pairingheap_container(HnswSearchCandidate , membername, ptr)
625
+ #define HnswGetSearchCandidateConst (membername , ptr ) pairingheap_const_container(HnswSearchCandidate , membername, ptr)
626
626
627
627
/*
628
628
* Compare candidate distances
629
629
*/
630
630
static int
631
631
CompareNearestCandidates (const pairingheap_node * a , const pairingheap_node * b , void * arg )
632
632
{
633
- if (HnswGetPairingHeapCandidateConst (c_node , a )-> distance < HnswGetPairingHeapCandidateConst (c_node , b )-> distance )
633
+ if (HnswGetSearchCandidateConst (c_node , a )-> distance < HnswGetSearchCandidateConst (c_node , b )-> distance )
634
634
return 1 ;
635
635
636
- if (HnswGetPairingHeapCandidateConst (c_node , a )-> distance > HnswGetPairingHeapCandidateConst (c_node , b )-> distance )
636
+ if (HnswGetSearchCandidateConst (c_node , a )-> distance > HnswGetSearchCandidateConst (c_node , b )-> distance )
637
637
return -1 ;
638
638
639
639
return 0 ;
@@ -645,27 +645,15 @@ CompareNearestCandidates(const pairingheap_node *a, const pairingheap_node *b, v
645
645
static int
646
646
CompareFurthestCandidates (const pairingheap_node * a , const pairingheap_node * b , void * arg )
647
647
{
648
- if (HnswGetPairingHeapCandidateConst (w_node , a )-> distance < HnswGetPairingHeapCandidateConst (w_node , b )-> distance )
648
+ if (HnswGetSearchCandidateConst (w_node , a )-> distance < HnswGetSearchCandidateConst (w_node , b )-> distance )
649
649
return -1 ;
650
650
651
- if (HnswGetPairingHeapCandidateConst (w_node , a )-> distance > HnswGetPairingHeapCandidateConst (w_node , b )-> distance )
651
+ if (HnswGetSearchCandidateConst (w_node , a )-> distance > HnswGetSearchCandidateConst (w_node , b )-> distance )
652
652
return 1 ;
653
653
654
654
return 0 ;
655
655
}
656
656
657
- /*
658
- * Create a pairing heap node for a candidate
659
- */
660
- static HnswPairingHeapNode *
661
- CreatePairingHeapNode (HnswCandidate * c )
662
- {
663
- HnswPairingHeapNode * node = palloc (sizeof (HnswPairingHeapNode ));
664
-
665
- node -> inner = c ;
666
- return node ;
667
- }
668
-
669
657
/*
670
658
* Init visited
671
659
*/
@@ -825,15 +813,13 @@ HnswSearchLayer(char *base, Datum q, List *ep, int ef, int lc, Relation index, F
825
813
/* Add entry points to v, C, and W */
826
814
foreach (lc2 , ep )
827
815
{
828
- HnswCandidate * hc = (HnswCandidate * ) lfirst (lc2 );
816
+ HnswSearchCandidate * hc = (HnswSearchCandidate * ) lfirst (lc2 );
829
817
bool found ;
830
- HnswPairingHeapNode * node ;
831
818
832
819
AddToVisited (base , & v , hc -> element , index , & found );
833
820
834
- node = CreatePairingHeapNode (hc );
835
- pairingheap_add (C , & node -> c_node );
836
- pairingheap_add (W , & node -> w_node );
821
+ pairingheap_add (C , & hc -> c_node );
822
+ pairingheap_add (W , & hc -> w_node );
837
823
838
824
/*
839
825
* Do not count elements being deleted towards ef when vacuuming. It
@@ -846,8 +832,8 @@ HnswSearchLayer(char *base, Datum q, List *ep, int ef, int lc, Relation index, F
846
832
847
833
while (!pairingheap_is_empty (C ))
848
834
{
849
- HnswCandidate * c = HnswGetPairingHeapCandidate (c_node , pairingheap_remove_first (C ));
850
- HnswCandidate * f = HnswGetPairingHeapCandidate (w_node , pairingheap_first (W ));
835
+ HnswSearchCandidate * c = HnswGetSearchCandidate (c_node , pairingheap_remove_first (C ));
836
+ HnswSearchCandidate * f = HnswGetSearchCandidate (w_node , pairingheap_first (W ));
851
837
HnswElement cElement ;
852
838
853
839
if (c -> distance > f -> distance )
@@ -863,12 +849,11 @@ HnswSearchLayer(char *base, Datum q, List *ep, int ef, int lc, Relation index, F
863
849
for (int i = 0 ; i < unvisitedLength ; i ++ )
864
850
{
865
851
HnswElement eElement ;
866
- HnswCandidate * e ;
867
- HnswPairingHeapNode * node ;
852
+ HnswSearchCandidate * e ;
868
853
float eDistance ;
869
854
bool alwaysAdd = wlen < ef ;
870
855
871
- f = HnswGetPairingHeapCandidate (w_node , pairingheap_first (W ));
856
+ f = HnswGetSearchCandidate (w_node , pairingheap_first (W ));
872
857
873
858
if (index == NULL )
874
859
{
@@ -899,13 +884,11 @@ HnswSearchLayer(char *base, Datum q, List *ep, int ef, int lc, Relation index, F
899
884
continue ;
900
885
901
886
/* Create a new candidate */
902
- e = palloc (sizeof (HnswCandidate ));
887
+ e = palloc (sizeof (HnswSearchCandidate ));
903
888
HnswPtrStore (base , e -> element , eElement );
904
889
e -> distance = eDistance ;
905
-
906
- node = CreatePairingHeapNode (e );
907
- pairingheap_add (C , & node -> c_node );
908
- pairingheap_add (W , & node -> w_node );
890
+ pairingheap_add (C , & e -> c_node );
891
+ pairingheap_add (W , & e -> w_node );
909
892
910
893
/*
911
894
* Do not count elements being deleted towards ef when vacuuming.
@@ -926,7 +909,7 @@ HnswSearchLayer(char *base, Datum q, List *ep, int ef, int lc, Relation index, F
926
909
/* Add each element of W to w */
927
910
while (!pairingheap_is_empty (W ))
928
911
{
929
- HnswCandidate * hc = HnswGetPairingHeapCandidate (w_node , pairingheap_remove_first (W ));
912
+ HnswSearchCandidate * hc = HnswGetSearchCandidate (w_node , pairingheap_remove_first (W ));
930
913
931
914
w = lappend (w , hc );
932
915
}
@@ -1307,16 +1290,27 @@ HnswFindElementNeighbors(char *base, HnswElement element, HnswElement entryPoint
1307
1290
{
1308
1291
int lm = HnswGetLayerM (m , lc );
1309
1292
List * neighbors ;
1310
- List * lw ;
1293
+ List * lw = NIL ;
1294
+ ListCell * lc2 ;
1311
1295
1312
1296
w = HnswSearchLayer (base , q , ep , efConstruction , lc , index , procinfo , collation , m , true, skipElement );
1313
1297
1298
+ /* Convert search candidates to candidates */
1299
+ foreach (lc2 , w )
1300
+ {
1301
+ HnswSearchCandidate * sc = lfirst (lc2 );
1302
+ HnswCandidate * hc = palloc (sizeof (HnswCandidate ));
1303
+
1304
+ hc -> element = sc -> element ;
1305
+ hc -> distance = sc -> distance ;
1306
+
1307
+ lw = lappend (lw , hc );
1308
+ }
1309
+
1314
1310
/* Elements being deleted or skipped can help with search */
1315
1311
/* but should be removed before selecting neighbors */
1316
1312
if (index != NULL )
1317
- lw = RemoveElements (base , w , skipElement );
1318
- else
1319
- lw = w ;
1313
+ lw = RemoveElements (base , lw , skipElement );
1320
1314
1321
1315
/*
1322
1316
* Candidates are sorted, but not deterministically. Could set
0 commit comments