From 3fa94e33789a779b2ecd5de1df7a672276a0d17e Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Tue, 6 Oct 2026 23:43:14 -0700 Subject: [PATCH] Avoid unnecessary lookup [skip ci] --- src/hnswinsert.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/hnswinsert.c b/src/hnswinsert.c index 9d6d057..4c55871 100644 --- a/src/hnswinsert.c +++ b/src/hnswinsert.c @@ -144,7 +144,7 @@ HnswInsertAppendPage(Relation index, Buffer *nbuf, Page *npage, GenericXLogState * Add to element and neighbor pages */ static void -AddElementOnDisk(Relation index, HnswElement e, int m, BlockNumber insertPage, BlockNumber *updatedInsertPage, bool building) +AddElementOnDisk(Relation index, HnswElement e, int m, BlockNumber *updatedInsertPage, bool building) { Buffer buf; Page page; @@ -161,6 +161,7 @@ AddElementOnDisk(Relation index, HnswElement e, int m, BlockNumber insertPage, B Page npage; OffsetNumber freeOffno = InvalidOffsetNumber; OffsetNumber freeNeighborOffno = InvalidOffsetNumber; + BlockNumber insertPage = InvalidBlockNumber; BlockNumber newInsertPage = InvalidBlockNumber; uint8 tupleVersion; char *base = NULL; @@ -202,7 +203,10 @@ AddElementOnDisk(Relation index, HnswElement e, int m, BlockNumber insertPage, B } if (!BlockNumberIsValid(currentPage)) + { + insertPage = GetInsertPage(index); currentPage = insertPage; + } buf = ReadBuffer(index, currentPage); LockBuffer(buf, BUFFER_LOCK_EXCLUSIVE); @@ -381,7 +385,7 @@ AddElementOnDisk(Relation index, HnswElement e, int m, BlockNumber insertPage, B UnlockReleaseBuffer(nbuf); /* Update the insert page */ - if (BlockNumberIsValid(newInsertPage) && newInsertPage != insertPage) + if (BlockNumberIsValid(newInsertPage) && BlockNumberIsValid(insertPage) && newInsertPage != insertPage) *updatedInsertPage = newInsertPage; } @@ -714,7 +718,7 @@ UpdateGraphOnDisk(Relation index, HnswSupport * support, HnswElement element, in return; /* Add element */ - AddElementOnDisk(index, element, m, GetInsertPage(index), &newInsertPage, building); + AddElementOnDisk(index, element, m, &newInsertPage, building); /* Update insert page if needed */ if (BlockNumberIsValid(newInsertPage))