Compare commits

..

5 Commits

Author SHA1 Message Date
Andrew Kane
b5ae5fe9e7 Updated comments [skip ci] 2023-10-04 18:14:08 -07:00
Andrew Kane
c28f4683f7 Updated comment [skip ci] 2023-10-04 18:03:04 -07:00
Andrew Kane
0e5338676d Added comments [skip ci] 2023-10-04 17:58:41 -07:00
Andrew Kane
8d09de7467 Improved logic 2023-10-04 16:47:47 -07:00
Andrew Kane
920de9edde Speed up HNSW index build 2023-10-04 15:02:48 -07:00
2 changed files with 26 additions and 22 deletions

View File

@@ -110,14 +110,13 @@ typedef struct HnswCandidate
{
HnswElement element;
float distance;
bool closer;
} HnswCandidate;
typedef struct HnswNeighborArray
{
int length;
bool closerSet;
HnswCandidate *items;
HnswElement firstPruned;
} HnswNeighborArray;
typedef struct HnswPairingHeapNode

View File

@@ -139,7 +139,7 @@ HnswInitNeighbors(HnswElement element, int m)
a = &element->neighbors[lc];
a->length = 0;
a->items = palloc(sizeof(HnswCandidate) * lm);
a->closerSet = false;
a->firstPruned = NULL;
}
}
@@ -754,8 +754,7 @@ SelectNeighbors(List *c, int m, int lc, FmgrInfo *procinfo, Oid collation, HnswE
List *r = NIL;
List *w = list_copy(c);
pairingheap *wd;
bool mustCalculate = !e2->neighbors[lc].closerSet;
bool foundNew = false;
bool mustCalculate = e2->neighbors[lc].firstPruned == NULL;
if (list_length(w) <= m)
return w;
@@ -766,39 +765,45 @@ SelectNeighbors(List *c, int m, int lc, FmgrInfo *procinfo, Oid collation, HnswE
{
/* Assumes w is already ordered desc */
HnswCandidate *e = llast(w);
bool closer;
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 for as many candidates
* as there is state for
*/
if (mustCalculate)
e->closer = CheckElementCloser(e, r, lc, procinfo, collation);
else if (foundNew)
closer = CheckElementCloser(e, r, lc, procinfo, collation);
else if (e->element == e2->neighbors[lc].firstPruned)
{
/* 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);
closer = false;
e->closer = e->distance < distance;
if (!e->closer)
mustCalculate = true;
}
/*
* Could store multiple pruned and only calculate when exhausted
* (or store full state) at the expense of memory
*/
mustCalculate = true;
}
else if (e == newCandidate)
{
e->closer = CheckElementCloser(e, r, lc, procinfo, collation);
foundNew = true;
closer = CheckElementCloser(e, r, lc, procinfo, collation);
mustCalculate = true;
}
else
closer = true;
if (e->closer)
if (closer)
r = lappend(r, e);
else
pairingheap_add(wd, &(CreatePairingHeapNode(e)->ph_node));
}
e2->neighbors[lc].closerSet = true;
/* Save first pruned */
/* OK to leave previous element if empty */
if (!pairingheap_is_empty(wd))
e2->neighbors[lc].firstPruned = ((HnswPairingHeapNode *) pairingheap_first(wd))->inner->element;
/* Keep pruned connections */
while (!pairingheap_is_empty(wd) && list_length(r) < m)