mirror of
https://github.com/pgvector/pgvector.git
synced 2026-07-10 14:46:56 +08:00
Skip more work
This commit is contained in:
@@ -755,6 +755,7 @@ SelectNeighbors(List *c, int m, int lc, FmgrInfo *procinfo, Oid collation, HnswE
|
|||||||
List *w = list_copy(c);
|
List *w = list_copy(c);
|
||||||
pairingheap *wd;
|
pairingheap *wd;
|
||||||
bool mustCalculate = !e2->neighbors[lc].closerSet;
|
bool mustCalculate = !e2->neighbors[lc].closerSet;
|
||||||
|
bool foundNew = false;
|
||||||
|
|
||||||
if (list_length(w) <= m)
|
if (list_length(w) <= m)
|
||||||
return w;
|
return w;
|
||||||
@@ -768,16 +769,27 @@ SelectNeighbors(List *c, int m, int lc, FmgrInfo *procinfo, Oid collation, HnswE
|
|||||||
|
|
||||||
w = list_delete_last(w);
|
w = list_delete_last(w);
|
||||||
|
|
||||||
/*
|
/* Use previous state of r and wd to skip work when possible */
|
||||||
* r and wd will be the same as previous calls until the new
|
|
||||||
* candidate, so can skip distance calculations
|
|
||||||
*/
|
|
||||||
if (mustCalculate)
|
if (mustCalculate)
|
||||||
e->closer = CheckElementCloser(e, r, lc, procinfo, collation);
|
e->closer = CheckElementCloser(e, r, lc, procinfo, collation);
|
||||||
|
else if (foundNew)
|
||||||
|
{
|
||||||
|
/* If new or current candidate is not closer, no change in state */
|
||||||
|
if (newCandidate->closer && e->closer)
|
||||||
|
{
|
||||||
|
/* Only need to compare with new candidate */
|
||||||
|
float distance = HnswGetDistance(e->element, newCandidate->element, lc, procinfo, collation);
|
||||||
|
|
||||||
|
e->closer = e->distance < distance;
|
||||||
|
|
||||||
|
if (!e->closer)
|
||||||
|
mustCalculate = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
else if (e == newCandidate)
|
else if (e == newCandidate)
|
||||||
{
|
{
|
||||||
e->closer = CheckElementCloser(e, r, lc, procinfo, collation);
|
e->closer = CheckElementCloser(e, r, lc, procinfo, collation);
|
||||||
mustCalculate = true;
|
foundNew = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (e->closer)
|
if (e->closer)
|
||||||
|
|||||||
Reference in New Issue
Block a user