mirror of
https://github.com/pgvector/pgvector.git
synced 2026-06-30 01:31:15 +08:00
Improved code [skip ci]
This commit is contained in:
@@ -68,7 +68,7 @@
|
||||
#define HnswIsElementTuple(tup) ((tup)->type == HNSW_ELEMENT_TUPLE_TYPE)
|
||||
#define HnswIsNeighborTuple(tup) ((tup)->type == HNSW_NEIGHBOR_TUPLE_TYPE)
|
||||
|
||||
#define GetLayerM(m, layer) (layer == 0 ? m * 2 : m)
|
||||
#define HnswGetLayerM(m, layer) (layer == 0 ? m * 2 : m)
|
||||
#define HnswGetMl(m) (1 / log(m))
|
||||
|
||||
/* Variables */
|
||||
@@ -84,8 +84,8 @@ typedef struct HnswElementData
|
||||
HnswNeighborArray *neighbors;
|
||||
BlockNumber blkno;
|
||||
OffsetNumber offno;
|
||||
BlockNumber neighborPage;
|
||||
OffsetNumber neighborOffno;
|
||||
BlockNumber neighborPage;
|
||||
Vector *vec;
|
||||
} HnswElementData;
|
||||
|
||||
|
||||
@@ -345,7 +345,7 @@ HnswAddDuplicate(Relation index, HnswElement element, HnswElement dup)
|
||||
Buffer buf;
|
||||
Page page;
|
||||
GenericXLogState *state;
|
||||
Size esize = HNSW_ELEMENT_TUPLE_SIZE(dup->vec->dim);
|
||||
Size etupSize = HNSW_ELEMENT_TUPLE_SIZE(dup->vec->dim);
|
||||
HnswElementTuple etup;
|
||||
int i;
|
||||
|
||||
@@ -375,7 +375,7 @@ HnswAddDuplicate(Relation index, HnswElement element, HnswElement dup)
|
||||
etup->heaptids[i] = *((ItemPointer) linitial(element->heaptids));
|
||||
|
||||
/* Update index tuple */
|
||||
if (!PageIndexTupleOverwrite(page, dup->offno, (Item) etup, esize))
|
||||
if (!PageIndexTupleOverwrite(page, dup->offno, (Item) etup, etupSize))
|
||||
elog(ERROR, "failed to add index item to \"%s\"", RelationGetRelationName(index));
|
||||
|
||||
/* Commit */
|
||||
|
||||
@@ -556,7 +556,7 @@ HnswInitNeighbors(HnswElement element, int m)
|
||||
for (int lc = 0; lc <= level; lc++)
|
||||
{
|
||||
HnswNeighborArray *a;
|
||||
int lm = GetLayerM(m, lc);
|
||||
int lm = HnswGetLayerM(m, lc);
|
||||
|
||||
a = &element->neighbors[lc];
|
||||
a->length = 0;
|
||||
@@ -760,21 +760,19 @@ SearchLayer(Datum q, List *ep, int ef, int lc, Relation index, FmgrInfo *procinf
|
||||
|
||||
if (eDistance < f->distance || wlen < ef)
|
||||
{
|
||||
/* copy e */
|
||||
HnswCandidate *e2 = palloc(sizeof(HnswCandidate));
|
||||
/* Copy e */
|
||||
HnswCandidate *ec = palloc(sizeof(HnswCandidate));
|
||||
|
||||
e2->element = e->element;
|
||||
e2->distance = eDistance;
|
||||
ec->element = e->element;
|
||||
ec->distance = eDistance;
|
||||
|
||||
pairingheap_add(C, &(CreatePairingHeapNode(e2)->ph_node));
|
||||
pairingheap_add(W, &(CreatePairingHeapNode(e2)->ph_node));
|
||||
pairingheap_add(C, &(CreatePairingHeapNode(ec)->ph_node));
|
||||
pairingheap_add(W, &(CreatePairingHeapNode(ec)->ph_node));
|
||||
wlen++;
|
||||
|
||||
/* No need to decrement wlen */
|
||||
if (wlen > ef)
|
||||
{
|
||||
pairingheap_remove_first(W);
|
||||
wlen--;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -874,7 +872,7 @@ HnswInsertElement(HnswElement element, HnswElement entryPoint, Relation index, F
|
||||
|
||||
for (int lc = level; lc >= 0; lc--)
|
||||
{
|
||||
int lm = GetLayerM(m, lc);
|
||||
int lm = HnswGetLayerM(m, lc);
|
||||
|
||||
w = SearchLayer(q, ep, efConstruction, lc, index, procinfo, collation, true, skipPage, skipOffno);
|
||||
if (removeEntryPoint)
|
||||
@@ -893,7 +891,7 @@ HnswInsertElement(HnswElement element, HnswElement entryPoint, Relation index, F
|
||||
/* Update connections */
|
||||
for (int lc = level; lc >= 0; lc--)
|
||||
{
|
||||
int lm = GetLayerM(m, lc);
|
||||
int lm = HnswGetLayerM(m, lc);
|
||||
|
||||
AddConnections(element, newNeighbors[lc], lm, lc);
|
||||
|
||||
@@ -957,7 +955,7 @@ HnswSetNeighborTuple(HnswNeighborTuple ntup, HnswElement e, int m)
|
||||
for (int lc = e->level; lc >= 0; lc--)
|
||||
{
|
||||
HnswNeighborArray *neighbors = &e->neighbors[lc];
|
||||
int lm = GetLayerM(m, lc);
|
||||
int lm = HnswGetLayerM(m, lc);
|
||||
|
||||
for (int i = 0; i < lm; i++)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user