8000 Use inMemory for conditionals · postgrespro/pgvector@17266ed · GitHub
[go: up one dir, main page]

Skip to content

Commit 17266ed

Browse files
committed
Use inMemory for conditionals
1 parent a98534e commit 17266ed

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

src/hnswutils.c

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -598,9 +598,10 @@ HnswSearchCandidate *
598598
HnswEntryCandidate(char *base, HnswElement entryPoint, Datum q, Relation index, FmgrInfo *procinfo, Oid collation, bool loadVec)
599599
{
600600
HnswSearchCandidate *sc = palloc(sizeof(HnswSearchCandidate));
601+
bool inMemory = index == NULL;
601602

602603
HnswPtrStore(base, sc->element, entryPoint);
603-
if (index == NULL)
604+
if (inMemory)
604605
sc->distance = GetElementDistance(base, entryPoint, q, procinfo, collation);
605606
else
606607
HnswLoadElement(entryPoint, &sc->distance, &q, index, procinfo, collation, loadVec, NULL);
@@ -644,9 +645,9 @@ CompareFurthestCandidates(const pairingheap_node *a, const pairingheap_node *b,
644645
* Init visited
645646
*/
646647
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)
648649
{
649-
if (index != NULL)
650+
if (!inMemory)
650651
v->tids = tidhash_create(CurrentMemoryContext, ef * m * 2, NULL);
651652
else if (base != NULL)
652653
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)
658659
* Add to visited
659660
*/
660661
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)
662663
{
663-
if (index != NULL)
664+
if (!inMemory)
664665
{
665666
HnswElement element = HnswPtrAccess(base, elementPtr);
666667
ItemPointerData indextid;
@@ -721,7 +722,7 @@ HnswLoadUnvisitedFromMemory(char *base, HnswElement element, HnswUnvisited * unv
721722
HnswCandidate *hc = &localNeighborhood->items[i];
722723
bool found;
723724

724-
AddToVisited(base, v, hc->element, NULL, &found);
725+
AddToVisited(base, v, hc->element, true, &found);
725726

726727
if (!found)
727728
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
805806
int lm = HnswGetLayerM(m, lc);
806807
HnswUnvisited *unvisited = palloc(lm * sizeof(HnswUnvisited));
807808
int unvisitedLength;
809+
bool inMemory = index == NULL;
808810

809-
InitVisited(base, &v, index, ef, m);
811+
InitVisited(base, &v, inMemory, ef, m);
810812

811813
/* Create local memory for neighborhood if needed */
812-
if (index == NULL)
814+
if (inMemory)
813815
{
814816
neighborhoodSize = HNSW_NEIGHBOR_ARRAY_SIZE(lm);
815817
localNeighborhood = palloc(neighborhoodSize);
@@ -821,7 +823,7 @@ HnswSearchLayer(char *base, Datum q, List *ep, int ef, int lc, Relation index, F
821823
HnswSearchCandidate *sc = (HnswSearchCandidate *) lfirst(lc2);
822824
bool found;
823825

824-
AddToVisited(base, &v, sc->element, index, &found);
826+
AddToVisited(base, &v, sc->element, inMemory, &found);
825827

826828
pairingheap_add(C, &sc->c_node);
827829
pairingheap_add(W, &sc->w_node);
@@ -846,7 +848,7 @@ HnswSearchLayer(char *base, Datum q, List *ep, int ef, int lc, Relation index, F
846848

847849
cElement = HnswPtrAccess(base, c->element);
848850

849-
if (index == NULL)
851+
if (inMemory)
850852
HnswLoadUnvisitedFromMemory(base, cElement, unvisited, &unvisitedLength, &v, lc, localNeighborhood, neighborhoodSize);
851853
else
852854
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
860862

861863
f = HnswGetSearchCandidate(w_node, pairingheap_first(W));
862864

863-
if (index == NULL)
865+
if (inMemory)
864866
{
865867
eElement = unvisited[i].element;
866868
eDistance = GetElementDistance(base, eElement, q, procinfo, collation);
@@ -1222,9 +1224,10 @@ HnswFindElementNeighbors(char *base, HnswElement element, HnswElement entryPoint
12221224
int entryLevel;
12231225
Datum q = HnswGetValue(base, element);
12241226
HnswElement skipElement = existing ? element : NULL;
1227+
bool inMemory = index == NULL;
12251228

12261229
/* Precompute hash */
1227-
if (index == NULL)
1230+
if (inMemory)
12281231
PrecomputeHash(base, element);
12291232

12301233
/* No neighbors if no entry point */
@@ -1273,7 +1276,7 @@ HnswFindElementNeighbors(char *base, HnswElement element, HnswElement entryPoint
12731276

12741277
/* Elements being deleted or skipped can help with search */
12751278
/* but should be removed before selecting neighbors */
1276-
if (index != NULL)
1279+
if (!inMemory)
12771280
lw = RemoveElements(base, lw, skipElement);
12781281

12791282
/*

0 commit comments

Comments
 (0)
0