Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f5ae3ba

Browse files
committed
Make tbm_add_tuples more efficient by caching the last acccessed page.
When adding a large number of tuples to a TID bitmap using tbm_add_tuples() sometimes a lot of time was spent looking up a page's entry in the bitmap's internal hashtable. Improve efficiency by caching the last accessed page, while iterating over the passed in tuples, hoping consecutive tuples will often be on the same page. In many cases that's a good bet, and in the rest the added overhead isn't big. Discussion: 54479A85.8060309@sigaev.ru Author: Teodor Sigaev Reviewed-By: David Rowley
1 parent aa1d2fc commit f5ae3ba

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

src/backend/nodes/tidbitmap.c

+18-6
Original file line numberDiff line numberDiff line change
@@ -268,25 +268,33 @@ void
268268
tbm_add_tuples(TIDBitmap *tbm, const ItemPointer tids, int ntids,
269269
bool recheck)
270270
{
271-
int i;
271+
int i;
272+
PagetableEntry *page = NULL;
272273

273274
Assert(!tbm->iterating);
274275
for (i = 0; i < ntids; i++)
275276
{
276277
BlockNumber blk = ItemPointerGetBlockNumber(tids + i);
277278
OffsetNumber off = ItemPointerGetOffsetNumber(tids + i);
278-
PagetableEntry *page;
279279
int wordnum,
280280
bitnum;
281281

282282
/* safety check to ensure we don't overrun bit array bounds */
283283
if (off < 1 || off > MAX_TUPLES_PER_PAGE)
284284
elog(ERROR, "tuple offset out of range: %u", off);
285285

286-
if (tbm_page_is_lossy(tbm, blk))
287-
continue; /* whole page is already marked */
288-
289-
page = tbm_get_pageentry(tbm, blk);
286+
if (page == NULL || page->blockno != blk)
287+
{
288+
if (tbm_page_is_lossy(tbm, blk))
289+
continue; /* whole page is already marked */
290+
291+
/*
292+
* Cache this page as it's quite likely that we'll see the same
293+
* page again in the next iteration. This will save having to
294+
* lookup the page in the hashtable again.
295+
*/
296+
page = tbm_get_pageentry(tbm, blk);
297+
}
290298

291299
if (page->ischunk)
292300
{
@@ -303,7 +311,11 @@ tbm_add_tuples(TIDBitmap *tbm, const ItemPointer tids, int ntids,
303311
page->recheck |= recheck;
304312

305313
if (tbm->nentries > tbm->maxentries)
314+
{
306315
tbm_lossify(tbm);
316+
/* Cached page could become lossy or freed */
317+
page = NULL;
318+
}
307319
}
308320
}
309321

0 commit comments

Comments
 (0)