@@ -598,9 +598,10 @@ HnswSearchCandidate *
598
598
HnswEntryCandidate (char * base , HnswElement entryPoint , Datum q , Relation index , FmgrInfo * procinfo , Oid collation , bool loadVec )
599
599
{
600
600
HnswSearchCandidate * sc = palloc (sizeof (HnswSearchCandidate ));
601
+ bool inMemory = index == NULL ;
601
602
602
603
HnswPtrStore (base , sc -> element , entryPoint );
603
- if (index == NULL )
604
+ if (inMemory )
604
605
sc -> distance = GetElementDistance (base , entryPoint , q , procinfo , collation );
605
606
else
606
607
HnswLoadElement (entryPoint , & sc -> distance , & q , index , procinfo , collation , loadVec , NULL );
@@ -644,9 +645,9 @@ CompareFurthestCandidates(const pairingheap_node *a, const pairingheap_node *b,
644
645
* Init visited
645
646
*/
646
647
static inline void
647
- InitVisited (char * base , visited_hash * v , Relation index , int ef , int m )
648
+ InitVisited (char * base , visited_hash * v , bool inMemory , int ef , int m )
648
649
{
649
- if (index != NULL )
650
+ if (! inMemory )
650
651
v -> tids = tidhash_create (CurrentMemoryContext , ef * m * 2 , NULL );
651
652
else if (base != NULL )
652
653
v -> offsets = offsethash_create (CurrentMemoryContext , ef * m * 2 , NULL );
@@ -658,9 +659,9 @@ InitVisited(char *base, visited_hash * v, Relation index, int ef, int m)
658
659
* Add to visited
659
660
*/
660
661
static inline void
661
- AddToVisited (char * base , visited_hash * v , HnswElementPtr elementPtr , Relation index , bool * found )
662
+ AddToVisited (char * base , visited_hash * v , HnswElementPtr elementPtr , bool inMemory , bool * found )
662
663
{
663
- if (index != NULL )
664
+ if (! inMemory )
664
665
{
665
666
HnswElement element = HnswPtrAccess (base , elementPtr );
666
667
ItemPointerData indextid ;
@@ -721,7 +722,7 @@ HnswLoadUnvisitedFromMemory(char *base, HnswElement element, HnswUnvisited * unv
721
722
HnswCandidate * hc = & localNeighborhood -> items [i ];
722
723
bool found ;
723
724
724
- AddToVisited (base , v , hc -> element , NULL , & found );
725
+ AddToVisited (base , v , hc -> element , true , & found );
725
726
726
727
if (!found )
727
728
unvisited [(* unvisitedLength )++ ].element = HnswPtrAccess (base, hc -> element );
@@ -805,11 +806,12 @@ HnswSearchLayer(char *base, Datum q, List *ep, int ef, int lc, Relation index, F
805
806
int lm = HnswGetLayerM (m , lc );
806
807
HnswUnvisited * unvisited = palloc (lm * sizeof (HnswUnvisited ));
807
808
int unvisitedLength ;
809
+ bool inMemory = index == NULL ;
808
810
809
- InitVisited (base , & v , index , ef , m );
811
+ InitVisited (base , & v , inMemory , ef , m );
810
812
811
813
/* Create local memory for neighborhood if needed */
812
- if (index == NULL )
814
+ if (inMemory )
813
815
{
814
816
neighborhoodSize = HNSW_NEIGHBOR_ARRAY_SIZE (lm );
815
817
localNeighborhood = palloc (neighborhoodSize );
@@ -821,7 +823,7 @@ HnswSearchLayer(char *base, Datum q, List *ep, int ef, int lc, Relation index, F
821
823
HnswSearchCandidate * sc = (HnswSearchCandidate * ) lfirst (lc2 );
822
824
bool found ;
823
825
824
- AddToVisited (base , & v , sc -> element , index , & found );
826
+ AddToVisited (base , & v , sc -> element , inMemory , & found );
825
827
826
828
pairingheap_add (C , & sc -> c_node );
827
829
pairingheap_add (W , & sc -> w_node );
@@ -846,7 +848,7 @@ HnswSearchLayer(char *base, Datum q, List *ep, int ef, int lc, Relation index, F
846
848
847
849
cElement = HnswPtrAccess (base , c -> element );
848
850
849
- if (index == NULL )
851
+ if (inMemory )
850
852
HnswLoadUnvisitedFromMemory (base , cElement , unvisited , & unvisitedLength , & v , lc , localNeighborhood , neighborhoodSize );
851
853
else
852
854
HnswLoadUnvisitedFromDisk (cElement , unvisited , & unvisitedLength , & v , index , m , lm , lc );
@@ -860,7 +862,7 @@ HnswSearchLayer(char *base, Datum q, List *ep, int ef, int lc, Relation index, F
860
862
861
863
f = HnswGetSearchCandidate (w_node , pairingheap_first (W ));
862
864
863
- if (index == NULL )
865
+ if (inMemory )
864
866
{
865
867
eElement = unvisited [i ].element ;
866
868
eDistance = GetElementDistance (base , eElement , q , procinfo , collation );
@@ -1222,9 +1224,10 @@ HnswFindElementNeighbors(char *base, HnswElement element, HnswElement entryPoint
1222
1224
int entryLevel ;
1223
1225
Datum q = HnswGetValue (base , element );
1224
1226
HnswElement skipElement = existing ? element : NULL ;
1227
+ bool inMemory = index == NULL ;
1225
1228
1226
1229
/* Precompute hash */
1227
- if (index == NULL )
1230
+ if (inMemory )
1228
1231
PrecomputeHash (base , element );
1229
1232
1230
1233
/* No neighbors if no entry point */
@@ -1273,7 +1276,7 @@ HnswFindElementNeighbors(char *base, HnswElement element, HnswElement entryPoint
1273
1276
1274
1277
/* Elements being deleted or skipped can help with search */
1275
1278
/* but should be removed before selecting neighbors */
1276
- if (index != NULL )
1279
+ if (! inMemory )
1277
1280
lw = RemoveElements (base , lw , skipElement );
1278
1281
1279
1282
/*
0 commit comments