Added HnswGetNeighbors macro

This commit is contained in:
Andrew Kane
2024-01-13 20:00:34 -08:00
parent cbf3eb4fa5
commit 597bfdc76b
4 changed files with 22 additions and 19 deletions

View File

@@ -96,6 +96,8 @@
/* Ensure fits on page and in uint8 */ /* Ensure fits on page and in uint8 */
#define HnswGetMaxLevel(m) Min(((BLCKSZ - MAXALIGN(SizeOfPageHeaderData) - MAXALIGN(sizeof(HnswPageOpaqueData)) - offsetof(HnswNeighborTupleData, indextids) - sizeof(ItemIdData)) / (sizeof(ItemPointerData)) / m) - 2, 255) #define HnswGetMaxLevel(m) Min(((BLCKSZ - MAXALIGN(SizeOfPageHeaderData) - MAXALIGN(sizeof(HnswPageOpaqueData)) - offsetof(HnswNeighborTupleData, indextids) - sizeof(ItemIdData)) / (sizeof(ItemPointerData)) / m) - 2, 255)
#define HnswGetNeighbors(element, lc) (AssertMacro((element)->level >= (lc)), &(element)->neighbors[lc])
/* Variables */ /* Variables */
extern int hnsw_ef_search; extern int hnsw_ef_search;
extern bool hnsw_enable_parallel_build; extern bool hnsw_enable_parallel_build;

View File

@@ -322,7 +322,7 @@ HnswElementMemory(HnswElement e, int m)
static bool static bool
HnswFindDuplicateInMemory(HnswElement element) HnswFindDuplicateInMemory(HnswElement element)
{ {
HnswNeighborArray *neighbors = &element->neighbors[0]; HnswNeighborArray *neighbors = HnswGetNeighbors(element, 0);
for (int i = 0; i < neighbors->length; i++) for (int i = 0; i < neighbors->length; i++)
{ {
@@ -398,7 +398,7 @@ InsertTupleInMemory(Relation index, Datum *values, ItemPointer heaptid, HnswBuil
for (int lc = element->level; lc >= 0; lc--) for (int lc = element->level; lc >= 0; lc--)
{ {
int lm = HnswGetLayerM(m, lc); int lm = HnswGetLayerM(m, lc);
HnswNeighborArray *neighbors = &element->neighbors[lc]; HnswNeighborArray *neighbors = HnswGetNeighbors(element, lc);
for (int i = 0; i < neighbors->length; i++) for (int i = 0; i < neighbors->length; i++)
HnswUpdateConnection(element, &neighbors->items[i], lm, lc, NULL, NULL, procinfo, collation); HnswUpdateConnection(element, &neighbors->items[i], lm, lc, NULL, NULL, procinfo, collation);

View File

@@ -343,7 +343,7 @@ HnswUpdateNeighborPages(Relation index, FmgrInfo *procinfo, Oid collation, HnswE
for (int lc = e->level; lc >= 0; lc--) for (int lc = e->level; lc >= 0; lc--)
{ {
int lm = HnswGetLayerM(m, lc); int lm = HnswGetLayerM(m, lc);
HnswNeighborArray *neighbors = &e->neighbors[lc]; HnswNeighborArray *neighbors = HnswGetNeighbors(e, lc);
for (int i = 0; i < neighbors->length; i++) for (int i = 0; i < neighbors->length; i++)
{ {
@@ -513,7 +513,7 @@ HnswAddDuplicate(Relation index, HnswElement element, HnswElement dup, bool buil
static bool static bool
HnswFindDuplicate(Relation index, HnswElement element, bool building) HnswFindDuplicate(Relation index, HnswElement element, bool building)
{ {
HnswNeighborArray *neighbors = &element->neighbors[0]; HnswNeighborArray *neighbors = HnswGetNeighbors(element, 0);
for (int i = 0; i < neighbors->length; i++) for (int i = 0; i < neighbors->length; i++)
{ {

View File

@@ -390,7 +390,7 @@ HnswSetNeighborTuple(HnswNeighborTuple ntup, HnswElement e, int m)
for (int lc = e->level; lc >= 0; lc--) for (int lc = e->level; lc >= 0; lc--)
{ {
HnswNeighborArray *neighbors = &e->neighbors[lc]; HnswNeighborArray *neighbors = HnswGetNeighbors(e, lc);
int lm = HnswGetLayerM(m, lc); int lm = HnswGetLayerM(m, lc);
for (int i = 0; i < lm; i++) for (int i = 0; i < lm; i++)
@@ -448,7 +448,7 @@ LoadNeighborsFromPage(HnswElement element, Relation index, Page page, int m)
if (level < 0) if (level < 0)
level = 0; level = 0;
neighbors = &element->neighbors[level]; neighbors = HnswGetNeighbors(element, level);
hc = &neighbors->items[neighbors->length++]; hc = &neighbors->items[neighbors->length++];
hc->element = e; hc->element = e;
} }
@@ -671,7 +671,7 @@ HnswSearchLayer(Datum q, List *ep, int ef, int lc, Relation index, FmgrInfo *pro
HnswLoadNeighbors(c->element, index, m); HnswLoadNeighbors(c->element, index, m);
/* Get the neighborhood at layer lc */ /* Get the neighborhood at layer lc */
neighborhood = &c->element->neighbors[lc]; neighborhood = HnswGetNeighbors(c->element, lc);
for (int i = 0; i < neighborhood->length; i++) for (int i = 0; i < neighborhood->length; i++)
{ {
@@ -774,23 +774,23 @@ HnswGetDistance(HnswElement a, HnswElement b, int lc, FmgrInfo *procinfo, Oid co
/* Look for cached distance */ /* Look for cached distance */
if (a->neighbors != NULL) if (a->neighbors != NULL)
{ {
Assert(a->level >= lc); HnswNeighborArray *neighbors = HnswGetNeighbors(a, lc);
for (int i = 0; i < a->neighbors[lc].length; i++) for (int i = 0; i < neighbors->length; i++)
{ {
if (a->neighbors[lc].items[i].element == b) if (neighbors->items[i].element == b)
return a->neighbors[lc].items[i].distance; return neighbors->items[i].distance;
} }
} }
if (b->neighbors != NULL) if (b->neighbors != NULL)
{ {
Assert(b->level >= lc); HnswNeighborArray *neighbors = HnswGetNeighbors(b, lc);
for (int i = 0; i < b->neighbors[lc].length; i++) for (int i = 0; i < neighbors->length; i++)
{ {
if (b->neighbors[lc].items[i].element == a) if (neighbors->items[i].element == a)
return b->neighbors[lc].items[i].distance; return neighbors->items[i].distance;
} }
} }
@@ -826,7 +826,8 @@ SelectNeighbors(List *c, int m, int lc, FmgrInfo *procinfo, Oid collation, HnswE
List *r = NIL; List *r = NIL;
List *w = list_copy(c); List *w = list_copy(c);
pairingheap *wd; pairingheap *wd;
bool mustCalculate = !e2->neighbors[lc].closerSet; HnswNeighborArray *neighbors = HnswGetNeighbors(e2, lc);
bool mustCalculate = !neighbors->closerSet;
List *added = NIL; List *added = NIL;
bool removedAny = false; bool removedAny = false;
@@ -890,7 +891,7 @@ SelectNeighbors(List *c, int m, int lc, FmgrInfo *procinfo, Oid collation, HnswE
} }
/* Cached value can only be used in future if sorted deterministically */ /* Cached value can only be used in future if sorted deterministically */
e2->neighbors[lc].closerSet = sortCandidates; neighbors->closerSet = sortCandidates;
/* Keep pruned connections */ /* Keep pruned connections */
while (!pairingheap_is_empty(wd) && list_length(r) < m) while (!pairingheap_is_empty(wd) && list_length(r) < m)
@@ -915,7 +916,7 @@ static void
AddConnections(HnswElement element, List *neighbors, int m, int lc) AddConnections(HnswElement element, List *neighbors, int m, int lc)
{ {
ListCell *lc2; ListCell *lc2;
HnswNeighborArray *a = &element->neighbors[lc]; HnswNeighborArray *a = HnswGetNeighbors(element, lc);
foreach(lc2, neighbors) foreach(lc2, neighbors)
a->items[a->length++] = *((HnswCandidate *) lfirst(lc2)); a->items[a->length++] = *((HnswCandidate *) lfirst(lc2));
@@ -927,7 +928,7 @@ AddConnections(HnswElement element, List *neighbors, int m, int lc)
void void
HnswUpdateConnection(HnswElement element, HnswCandidate * hc, int m, int lc, int *updateIdx, Relation index, FmgrInfo *procinfo, Oid collation) HnswUpdateConnection(HnswElement element, HnswCandidate * hc, int m, int lc, int *updateIdx, Relation index, FmgrInfo *procinfo, Oid collation)
{ {
HnswNeighborArray *currentNeighbors = &hc->element->neighbors[lc]; HnswNeighborArray *currentNeighbors = HnswGetNeighbors(hc->element, lc);
HnswCandidate hc2; HnswCandidate hc2;