mirror of
https://github.com/pgvector/pgvector.git
synced 2026-07-15 00:56:54 +08:00
Reduced memory and allocations for heap TIDs - closes #385
This commit is contained in:
@@ -103,7 +103,8 @@ typedef struct HnswNeighborArray HnswNeighborArray;
|
|||||||
|
|
||||||
typedef struct HnswElementData
|
typedef struct HnswElementData
|
||||||
{
|
{
|
||||||
List *heaptids;
|
ItemPointerData heaptids[HNSW_HEAPTIDS];
|
||||||
|
uint8 heaptidsLength;
|
||||||
uint8 level;
|
uint8 level;
|
||||||
uint8 deleted;
|
uint8 deleted;
|
||||||
uint32 hash;
|
uint32 hash;
|
||||||
|
|||||||
@@ -490,7 +490,7 @@ HnswAddDuplicate(Relation index, HnswElement element, HnswElement dup, bool buil
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Add heap TID */
|
/* Add heap TID */
|
||||||
etup->heaptids[i] = *((ItemPointer) linitial(element->heaptids));
|
etup->heaptids[i] = element->heaptids[0];
|
||||||
|
|
||||||
/* Overwrite tuple */
|
/* Overwrite tuple */
|
||||||
if (!PageIndexTupleOverwrite(page, dup->offno, (Item) etup, etupSize))
|
if (!PageIndexTupleOverwrite(page, dup->offno, (Item) etup, etupSize))
|
||||||
|
|||||||
@@ -188,15 +188,13 @@ hnswgettuple(IndexScanDesc scan, ScanDirection dir)
|
|||||||
ItemPointer heaptid;
|
ItemPointer heaptid;
|
||||||
|
|
||||||
/* Move to next element if no valid heap TIDs */
|
/* Move to next element if no valid heap TIDs */
|
||||||
if (list_length(hc->element->heaptids) == 0)
|
if (hc->element->heaptidsLength == 0)
|
||||||
{
|
{
|
||||||
so->w = list_delete_last(so->w);
|
so->w = list_delete_last(so->w);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
heaptid = llast(hc->element->heaptids);
|
heaptid = &hc->element->heaptids[--hc->element->heaptidsLength];
|
||||||
|
|
||||||
hc->element->heaptids = list_delete_last(hc->element->heaptids);
|
|
||||||
|
|
||||||
MemoryContextSwitchTo(oldCtx);
|
MemoryContextSwitchTo(oldCtx);
|
||||||
|
|
||||||
|
|||||||
@@ -231,7 +231,7 @@ HnswInitElement(ItemPointer heaptid, int m, double ml, int maxLevel)
|
|||||||
if (level > maxLevel)
|
if (level > maxLevel)
|
||||||
level = maxLevel;
|
level = maxLevel;
|
||||||
|
|
||||||
element->heaptids = NIL;
|
element->heaptidsLength = 0;
|
||||||
HnswAddHeapTid(element, heaptid);
|
HnswAddHeapTid(element, heaptid);
|
||||||
|
|
||||||
element->level = level;
|
element->level = level;
|
||||||
@@ -251,7 +251,6 @@ void
|
|||||||
HnswFreeElement(HnswElement element)
|
HnswFreeElement(HnswElement element)
|
||||||
{
|
{
|
||||||
HnswFreeNeighbors(element);
|
HnswFreeNeighbors(element);
|
||||||
list_free_deep(element->heaptids);
|
|
||||||
if (DatumGetPointer(element->value))
|
if (DatumGetPointer(element->value))
|
||||||
pfree(DatumGetPointer(element->value));
|
pfree(DatumGetPointer(element->value));
|
||||||
pfree(element);
|
pfree(element);
|
||||||
@@ -263,10 +262,7 @@ HnswFreeElement(HnswElement element)
|
|||||||
void
|
void
|
||||||
HnswAddHeapTid(HnswElement element, ItemPointer heaptid)
|
HnswAddHeapTid(HnswElement element, ItemPointer heaptid)
|
||||||
{
|
{
|
||||||
ItemPointer copy = palloc(sizeof(ItemPointerData));
|
element->heaptids[element->heaptidsLength++] = *heaptid;
|
||||||
|
|
||||||
ItemPointerCopy(heaptid, copy);
|
|
||||||
element->heaptids = lappend(element->heaptids, copy);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -397,8 +393,8 @@ HnswSetElementTuple(HnswElementTuple etup, HnswElement element)
|
|||||||
etup->deleted = 0;
|
etup->deleted = 0;
|
||||||
for (int i = 0; i < HNSW_HEAPTIDS; i++)
|
for (int i = 0; i < HNSW_HEAPTIDS; i++)
|
||||||
{
|
{
|
||||||
if (i < list_length(element->heaptids))
|
if (i < element->heaptidsLength)
|
||||||
etup->heaptids[i] = *((ItemPointer) list_nth(element->heaptids, i));
|
etup->heaptids[i] = element->heaptids[i];
|
||||||
else
|
else
|
||||||
ItemPointerSetInvalid(&etup->heaptids[i]);
|
ItemPointerSetInvalid(&etup->heaptids[i]);
|
||||||
}
|
}
|
||||||
@@ -509,7 +505,7 @@ HnswLoadElementFromTuple(HnswElement element, HnswElementTuple etup, bool loadHe
|
|||||||
element->deleted = etup->deleted;
|
element->deleted = etup->deleted;
|
||||||
element->neighborPage = ItemPointerGetBlockNumber(&etup->neighbortid);
|
element->neighborPage = ItemPointerGetBlockNumber(&etup->neighbortid);
|
||||||
element->neighborOffno = ItemPointerGetOffsetNumber(&etup->neighbortid);
|
element->neighborOffno = ItemPointerGetOffsetNumber(&etup->neighbortid);
|
||||||
element->heaptids = NIL;
|
element->heaptidsLength = 0;
|
||||||
|
|
||||||
if (loadHeaptids)
|
if (loadHeaptids)
|
||||||
{
|
{
|
||||||
@@ -682,7 +678,7 @@ HnswSearchLayer(Datum q, List *ep, int ef, int lc, Relation index, FmgrInfo *pro
|
|||||||
* would be ideal to do this for inserts as well, but this could
|
* would be ideal to do this for inserts as well, but this could
|
||||||
* affect insert performance.
|
* affect insert performance.
|
||||||
*/
|
*/
|
||||||
if (skipElement == NULL || list_length(hc->element->heaptids) != 0)
|
if (skipElement == NULL || hc->element->heaptidsLength != 0)
|
||||||
wlen++;
|
wlen++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -741,7 +737,7 @@ HnswSearchLayer(Datum q, List *ep, int ef, int lc, Relation index, FmgrInfo *pro
|
|||||||
* vacuuming. It would be ideal to do this for inserts as
|
* vacuuming. It would be ideal to do this for inserts as
|
||||||
* well, but this could affect insert performance.
|
* well, but this could affect insert performance.
|
||||||
*/
|
*/
|
||||||
if (skipElement == NULL || list_length(e->element->heaptids) != 0)
|
if (skipElement == NULL || e->element->heaptidsLength != 0)
|
||||||
{
|
{
|
||||||
wlen++;
|
wlen++;
|
||||||
|
|
||||||
@@ -953,7 +949,7 @@ HnswFindDuplicate(HnswElement e)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
/* Check for space */
|
/* Check for space */
|
||||||
if (list_length(neighbor->element->heaptids) < HNSW_HEAPTIDS)
|
if (neighbor->element->heaptidsLength < HNSW_HEAPTIDS)
|
||||||
return neighbor->element;
|
return neighbor->element;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1014,7 +1010,7 @@ HnswUpdateConnection(HnswElement element, HnswCandidate * hc, int m, int lc, int
|
|||||||
hc3->distance = GetCandidateDistance(hc3, q, procinfo, collation);
|
hc3->distance = GetCandidateDistance(hc3, q, procinfo, collation);
|
||||||
|
|
||||||
/* Prune element if being deleted */
|
/* Prune element if being deleted */
|
||||||
if (list_length(hc3->element->heaptids) == 0)
|
if (hc3->element->heaptidsLength == 0)
|
||||||
{
|
{
|
||||||
pruned = ¤tNeighbors->items[i];
|
pruned = ¤tNeighbors->items[i];
|
||||||
break;
|
break;
|
||||||
@@ -1072,7 +1068,7 @@ RemoveElements(List *w, HnswElement skipElement)
|
|||||||
if (skipElement != NULL && hc->element->blkno == skipElement->blkno && hc->element->offno == skipElement->offno)
|
if (skipElement != NULL && hc->element->blkno == skipElement->blkno && hc->element->offno == skipElement->offno)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (list_length(hc->element->heaptids) != 0)
|
if (hc->element->heaptidsLength != 0)
|
||||||
w2 = lappend(w2, hc);
|
w2 = lappend(w2, hc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -206,7 +206,7 @@ RepairGraphElement(HnswVacuumState * vacuumstate, HnswElement element, HnswEleme
|
|||||||
|
|
||||||
/* Init fields */
|
/* Init fields */
|
||||||
HnswInitNeighbors(element, m);
|
HnswInitNeighbors(element, m);
|
||||||
element->heaptids = NIL;
|
element->heaptidsLength = 0;
|
||||||
|
|
||||||
/* Add element to graph, skipping itself */
|
/* Add element to graph, skipping itself */
|
||||||
HnswInsertElement(element, entryPoint, index, procinfo, collation, m, efConstruction, true);
|
HnswInsertElement(element, entryPoint, index, procinfo, collation, m, efConstruction, true);
|
||||||
|
|||||||
Reference in New Issue
Block a user