mirror of
https://github.com/pgvector/pgvector.git
synced 2026-07-11 15:16:54 +08:00
Replaced pairing heap with array in SelectNeighbors - closes #447
Co-authored-by: Heikki Linnakangas <heikki.linnakangas@iki.fi>
This commit is contained in:
@@ -952,7 +952,9 @@ SelectNeighbors(char *base, List *c, int lm, int lc, FmgrInfo *procinfo, Oid col
|
|||||||
{
|
{
|
||||||
List *r = NIL;
|
List *r = NIL;
|
||||||
List *w = list_copy(c);
|
List *w = list_copy(c);
|
||||||
pairingheap *wd;
|
HnswCandidate **wd;
|
||||||
|
int wdlen = 0;
|
||||||
|
int wdoff = 0;
|
||||||
HnswNeighborArray *neighbors = HnswGetNeighbors(base, e2, lc);
|
HnswNeighborArray *neighbors = HnswGetNeighbors(base, e2, lc);
|
||||||
bool mustCalculate = !neighbors->closerSet;
|
bool mustCalculate = !neighbors->closerSet;
|
||||||
List *added = NIL;
|
List *added = NIL;
|
||||||
@@ -961,7 +963,7 @@ SelectNeighbors(char *base, List *c, int lm, int lc, FmgrInfo *procinfo, Oid col
|
|||||||
if (list_length(w) <= lm)
|
if (list_length(w) <= lm)
|
||||||
return w;
|
return w;
|
||||||
|
|
||||||
wd = pairingheap_allocate(CompareNearestCandidates, NULL);
|
wd = palloc(sizeof(HnswCandidate *) * list_length(w));
|
||||||
|
|
||||||
/* Ensure order of candidates is deterministic for closer caching */
|
/* Ensure order of candidates is deterministic for closer caching */
|
||||||
if (sortCandidates)
|
if (sortCandidates)
|
||||||
@@ -1027,21 +1029,21 @@ SelectNeighbors(char *base, List *c, int lm, int lc, FmgrInfo *procinfo, Oid col
|
|||||||
if (e->closer)
|
if (e->closer)
|
||||||
r = lappend(r, e);
|
r = lappend(r, e);
|
||||||
else
|
else
|
||||||
pairingheap_add(wd, &(CreatePairingHeapNode(e)->ph_node));
|
wd[wdlen++] = e;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Cached value can only be used in future if sorted deterministically */
|
/* Cached value can only be used in future if sorted deterministically */
|
||||||
neighbors->closerSet = sortCandidates;
|
neighbors->closerSet = sortCandidates;
|
||||||
|
|
||||||
/* Keep pruned connections */
|
/* Keep pruned connections */
|
||||||
while (!pairingheap_is_empty(wd) && list_length(r) < lm)
|
while (wdoff < wdlen && list_length(r) < lm)
|
||||||
r = lappend(r, ((HnswPairingHeapNode *) pairingheap_remove_first(wd))->inner);
|
r = lappend(r, wd[wdoff++]);
|
||||||
|
|
||||||
/* Return pruned for update connections */
|
/* Return pruned for update connections */
|
||||||
if (pruned != NULL)
|
if (pruned != NULL)
|
||||||
{
|
{
|
||||||
if (!pairingheap_is_empty(wd))
|
if (wdoff < wdlen)
|
||||||
*pruned = ((HnswPairingHeapNode *) pairingheap_first(wd))->inner;
|
*pruned = wd[wdoff];
|
||||||
else
|
else
|
||||||
*pruned = linitial(w);
|
*pruned = linitial(w);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user