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

Commit a97bbe1

Browse files
committed
Reduce branches in heapgetpage()'s per-tuple loop
Until now, heapgetpage()'s loop over all tuples performed some conditional checks for each tuple, even though condition did not change across the loop. This commit fixes that by moving the loop into an inline function. By calling it with different constant arguments, the compiler can generate an optimized loop for the different conditions, at the price of two per-page checks. For cases of all-visible tables and an isolation level other than serializable, speedups of up to 25% have been measured. Reviewed-by: John Naylor <johncnaylorls@gmail.com> Reviewed-by: Zhang Mingli <zmlpostgres@gmail.com> Tested-by: Quan Zongliang <quanzongliang@yeah.net> Discussion: https://postgr.es/m/20230716015656.xjvemfbp5fysjiea@awork3.anarazel.de Discussion: https://postgr.es/m/2ef7ff1b-3d18-2283-61b1-bbd25fc6c7ce@yeah.net
1 parent 41c51f0 commit a97bbe1

File tree

1 file changed

+74
-28
lines changed

1 file changed

+74
-28
lines changed

src/backend/access/heap/heapam.c

+74-28
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,56 @@ heap_setscanlimits(TableScanDesc sscan, BlockNumber startBlk, BlockNumber numBlk
364364
scan->rs_numblocks = numBlks;
365365
}
366366

367+
/*
368+
* Per-tuple loop for heapgetpage() in pagemode. Pulled out so it can be
369+
* called multiple times, with constant arguments for all_visible,
370+
* check_serializable.
371+
*/
372+
pg_attribute_always_inline
373+
static int
374+
heapgetpage_collect(HeapScanDesc scan, Snapshot snapshot,
375+
Page page, Buffer buffer,
376+
BlockNumber block, int lines,
377+
bool all_visible, bool check_serializable)
378+
{
379+
int ntup = 0;
380+
OffsetNumber lineoff;
381+
382+
for (lineoff = FirstOffsetNumber; lineoff <= lines; lineoff++)
383+
{
384+
ItemId lpp = PageGetItemId(page, lineoff);
385+
HeapTupleData loctup;
386+
bool valid;
387+
388+
if (!ItemIdIsNormal(lpp))
389+
continue;
390+
391+
loctup.t_data = (HeapTupleHeader) PageGetItem(page, lpp);
392+
loctup.t_len = ItemIdGetLength(lpp);
393+
loctup.t_tableOid = RelationGetRelid(scan->rs_base.rs_rd);
394+
ItemPointerSet(&(loctup.t_self), block, lineoff);
395+
396+
if (all_visible)
397+
valid = true;
398+
else
399+
valid = HeapTupleSatisfiesVisibility(&loctup, snapshot, buffer);
400+
401+
if (check_serializable)
402+
HeapCheckForSerializableConflictOut(valid, scan->rs_base.rs_rd,
403+
&loctup, buffer, snapshot);
404+
405+
if (valid)
406+
{
407+
scan->rs_vistuples[ntup] = lineoff;
408+
ntup++;
409+
}
410+
}
411+
412+
Assert(ntup <= MaxHeapTuplesPerPage);
413+
414+
return ntup;
415+
}
416+
367417
/*
368418
* heap_prepare_pagescan - Prepare current scan page to be scanned in pagemode
369419
*
@@ -379,9 +429,8 @@ heap_prepare_pagescan(TableScanDesc sscan)
379429
Snapshot snapshot;
380430
Page page;
381431
int lines;
382-
int ntup;
383-
OffsetNumber lineoff;
384432
bool all_visible;
433+
bool check_serializable;
385434

386435
Assert(BufferGetBlockNumber(buffer) == block);
387436

@@ -403,7 +452,6 @@ heap_prepare_pagescan(TableScanDesc sscan)
403452

404453
page = BufferGetPage(buffer);
405454
lines = PageGetMaxOffsetNumber(page);
406-
ntup = 0;
407455

408456
/*
409457
* If the all-visible flag indicates that all tuples on the page are
@@ -426,37 +474,35 @@ heap_prepare_pagescan(TableScanDesc sscan)
426474
* tuple for visibility the hard way.
427475
*/
428476
all_visible = PageIsAllVisible(page) && !snapshot->takenDuringRecovery;
477+
check_serializable =
478+
CheckForSerializableConflictOutNeeded(scan->rs_base.rs_rd, snapshot);
429479

430-
for (lineoff = FirstOffsetNumber; lineoff <= lines; lineoff++)
480+
/*
481+
* We call heapgetpage_collect() with constant arguments, to get the
482+
* compiler to constant fold the constant arguments. Separate calls with
483+
* constant arguments, rather than variables, are needed on several
484+
* compilers to actually perform constant folding.
485+
*/
486+
if (likely(all_visible))
431487
{
432-
ItemId lpp = PageGetItemId(page, lineoff);
433-
HeapTupleData loctup;
434-
bool valid;
435-
436-
if (!ItemIdIsNormal(lpp))
437-
continue;
438-
439-
loctup.t_tableOid = RelationGetRelid(scan->rs_base.rs_rd);
440-
loctup.t_data = (HeapTupleHeader) PageGetItem(page, lpp);
441-
loctup.t_len = ItemIdGetLength(lpp);
442-
ItemPointerSet(&(loctup.t_self), block, lineoff);
443-
444-
if (all_visible)
445-
valid = true;
488+
if (likely(!check_serializable))
489+
scan->rs_ntuples = heapgetpage_collect(scan, snapshot, page, buffer,
490+
block, lines, true, false);
446491
else
447-
valid = HeapTupleSatisfiesVisibility(&loctup, snapshot, buffer);
448-
449-
HeapCheckForSerializableConflictOut(valid, scan->rs_base.rs_rd,
450-
&loctup, buffer, snapshot);
451-
452-
if (valid)
453-
scan->rs_vistuples[ntup++] = lineoff;
492+
scan->rs_ntuples = heapgetpage_collect(scan, snapshot, page, buffer,
493+
block, lines, true, true);
494+
}
495+
else
496+
{
497+
if (likely(!check_serializable))
498+
scan->rs_ntuples = heapgetpage_collect(scan, snapshot, page, buffer,
499+
block, lines, false, false);
500+
else
501+
scan->rs_ntuples = heapgetpage_collect(scan, snapshot, page, buffer,
502+
block, lines, false, true);
454503
}
455504

456505
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
457-
458-
Assert(ntup <= MaxHeapTuplesPerPage);
459-
scan->rs_ntuples = ntup;
460506
}
461507

462508
/*

0 commit comments

Comments
 (0)