mirror of
https://github.com/pgvector/pgvector.git
synced 2026-07-14 16:46:54 +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 HnswIsElementTuple(tup) ((tup)->type == HNSW_ELEMENT_TUPLE_TYPE)
|
||||||
#define HnswIsNeighborTuple(tup) ((tup)->type == HNSW_NEIGHBOR_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))
|
#define HnswGetMl(m) (1 / log(m))
|
||||||
|
|
||||||
/* Variables */
|
/* Variables */
|
||||||
@@ -84,8 +84,8 @@ typedef struct HnswElementData
|
|||||||
HnswNeighborArray *neighbors;
|
HnswNeighborArray *neighbors;
|
||||||
BlockNumber blkno;
|
BlockNumber blkno;
|
||||||
OffsetNumber offno;
|
OffsetNumber offno;
|
||||||
BlockNumber neighborPage;
|
|
||||||
OffsetNumber neighborOffno;
|
OffsetNumber neighborOffno;
|
||||||
|
BlockNumber neighborPage;
|
||||||
Vector *vec;
|
Vector *vec;
|
||||||
} HnswElementData;
|
} HnswElementData;
|
||||||
|
|
||||||
|
|||||||
@@ -345,7 +345,7 @@ HnswAddDuplicate(Relation index, HnswElement element, HnswElement dup)
|
|||||||
Buffer buf;
|
Buffer buf;
|
||||||
Page page;
|
Page page;
|
||||||
GenericXLogState *state;
|
GenericXLogState *state;
|
||||||
Size esize = HNSW_ELEMENT_TUPLE_SIZE(dup->vec->dim);
|
Size etupSize = HNSW_ELEMENT_TUPLE_SIZE(dup->vec->dim);
|
||||||
HnswElementTuple etup;
|
HnswElementTuple etup;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
@@ -375,7 +375,7 @@ HnswAddDuplicate(Relation index, HnswElement element, HnswElement dup)
|
|||||||
etup->heaptids[i] = *((ItemPointer) linitial(element->heaptids));
|
etup->heaptids[i] = *((ItemPointer) linitial(element->heaptids));
|
||||||
|
|
||||||
/* Update index tuple */
|
/* 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));
|
elog(ERROR, "failed to add index item to \"%s\"", RelationGetRelationName(index));
|
||||||
|
|
||||||
/* Commit */
|
/* Commit */
|
||||||
|
|||||||
@@ -556,7 +556,7 @@ HnswInitNeighbors(HnswElement element, int m)
|
|||||||
for (int lc = 0; lc <= level; lc++)
|
for (int lc = 0; lc <= level; lc++)
|
||||||
{
|
{
|
||||||
HnswNeighborArray *a;
|
HnswNeighborArray *a;
|
||||||
int lm = GetLayerM(m, lc);
|
int lm = HnswGetLayerM(m, lc);
|
||||||
|
|
||||||
a = &element->neighbors[lc];
|
a = &element->neighbors[lc];
|
||||||
a->length = 0;
|
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)
|
if (eDistance < f->distance || wlen < ef)
|
||||||
{
|
{
|
||||||
/* copy e */
|
/* Copy e */
|
||||||
HnswCandidate *e2 = palloc(sizeof(HnswCandidate));
|
HnswCandidate *ec = palloc(sizeof(HnswCandidate));
|
||||||
|
|
||||||
e2->element = e->element;
|
ec->element = e->element;
|
||||||
e2->distance = eDistance;
|
ec->distance = eDistance;
|
||||||
|
|
||||||
pairingheap_add(C, &(CreatePairingHeapNode(e2)->ph_node));
|
pairingheap_add(C, &(CreatePairingHeapNode(ec)->ph_node));
|
||||||
pairingheap_add(W, &(CreatePairingHeapNode(e2)->ph_node));
|
pairingheap_add(W, &(CreatePairingHeapNode(ec)->ph_node));
|
||||||
wlen++;
|
wlen++;
|
||||||
|
|
||||||
|
/* No need to decrement wlen */
|
||||||
if (wlen > ef)
|
if (wlen > ef)
|
||||||
{
|
|
||||||
pairingheap_remove_first(W);
|
pairingheap_remove_first(W);
|
||||||
wlen--;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -874,7 +872,7 @@ HnswInsertElement(HnswElement element, HnswElement entryPoint, Relation index, F
|
|||||||
|
|
||||||
for (int lc = level; lc >= 0; lc--)
|
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);
|
w = SearchLayer(q, ep, efConstruction, lc, index, procinfo, collation, true, skipPage, skipOffno);
|
||||||
if (removeEntryPoint)
|
if (removeEntryPoint)
|
||||||
@@ -893,7 +891,7 @@ HnswInsertElement(HnswElement element, HnswElement entryPoint, Relation index, F
|
|||||||
/* Update connections */
|
/* Update connections */
|
||||||
for (int lc = level; lc >= 0; lc--)
|
for (int lc = level; lc >= 0; lc--)
|
||||||
{
|
{
|
||||||
int lm = GetLayerM(m, lc);
|
int lm = HnswGetLayerM(m, lc);
|
||||||
|
|
||||||
AddConnections(element, newNeighbors[lc], lm, 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--)
|
for (int lc = e->level; lc >= 0; lc--)
|
||||||
{
|
{
|
||||||
HnswNeighborArray *neighbors = &e->neighbors[lc];
|
HnswNeighborArray *neighbors = &e->neighbors[lc];
|
||||||
int lm = GetLayerM(m, lc);
|
int lm = HnswGetLayerM(m, lc);
|
||||||
|
|
||||||
for (int i = 0; i < lm; i++)
|
for (int i = 0; i < lm; i++)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user