8000 Use separate memory context for updating neighbors, which improves pe… · postgrespro/pgvector@57248ba · GitHub
[go: up one dir, main page]

Skip to content

Commit 57248ba

Browse files
committed
Use separate memory context for updating neighbors, which improves performance around 10% for larger vectors
1 parent ff6da4f commit 57248ba

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
- Added casts for arrays to `sparsevec`
44
- Improved cost estimation
5+
- Improved performance of HNSW inserts and on-disk index builds
56
- Reduced memory usage for HNSW index scans
67
- Dropped support for Postgres 12
78

src/hnswinsert.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,11 +394,12 @@ LoadElementsForInsert(HnswNeighborArray * neighbors, Datum q, int *idx, Relation
394394
* Get update index
395395
*/
396396
static int
397-
GetUpdateIndex(HnswElement element, HnswElement newElement, float distance, int m, int lm, int lc, Relation index, FmgrInfo *procinfo, Oid collation)
397+
GetUpdateIndex(HnswElement element, HnswElement newElement, float distance, int m, int lm, int lc, Relation index, FmgrInfo *procinfo, Oid collation, MemoryContext updateCtx)
398398
{
399399
char *base = NULL;
400400
int idx = -1;
401401
HnswNeighborArray *neighbors;
402+
MemoryContext oldCtx = MemoryContextSwitchTo(updateCtx);
402403

403404
/*
404405
* Get latest neighbors since they may have changed. Do not lock yet since
@@ -426,6 +427,9 @@ GetUpdateIndex(HnswElement element, HnswElement newElement, float distance, int
426427
HnswUpdateConnection(base, neighbors, newElement, distance, lm, &idx, index, procinfo, collation);
427428
}
428429

430+
MemoryContextSwitchTo(oldCtx);
431+
MemoryContextReset(updateCtx);
432+
429433
return idx;
430434
}
431435

@@ -529,6 +533,14 @@ HnswUpdateNeighborsOnDisk(Relation index, FmgrInfo *procinfo, Oid collation, Hns
< 8000 code>529533
{
530534
char *base = NULL;
531535

536+
/* Use separate memory context to improve performance for larger vectors */
537+
MemoryContext updateCtx = GenerationContextCreate(CurrentMemoryContext,
538+
"Hnsw insert update context",
539+
#if PG_VERSION_NUM >= 150000
540+
128 * 1024, 128 * 1024,
541+
#endif
542+
128 * 1024);
543+
532544
for (int lc = e->level; lc >= 0; lc--)
533545
{
534546
int lm = HnswGetLayerM(m, lc);
@@ -540,7 +552,7 @@ HnswUpdateNeighborsOnDisk(Relation index, FmgrInfo *procinfo, Oid collation, Hns
540552
HnswElement neighborElement = HnswPtrAccess(base, hc->element);
541553
int idx;
542554

543-
idx = GetUpdateIndex(neighborElement, e, hc->distance, m, lm, lc, index, procinfo, collation);
555+
idx = GetUpdateIndex(neighborElement, e, hc->distance, m, lm, lc, index, procinfo, collation, updateCtx);
544556

545557
/* New element was not selected as a neighbor */
546558
if (idx == -1)

0 commit comments

Comments
 (0)
0