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

Commit f1b059a

Browse files
committed
A couple of tiny performance hacks in _bt_step(). Remove PageIsEmpty
checks, which were once needed because PageGetMaxOffsetNumber would fail on empty pages, but are now just redundant. Also, don't set up local variables that aren't needed in the fast path --- most of the time, we only need to advance offnum and not step across a page boundary. Motivated by noticing _bt_step at the top of OProfile profile for a pgbench run.
1 parent 10a2df2 commit f1b059a

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/backend/access/nbtree/nbtsearch.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/access/nbtree/nbtsearch.c,v 1.97 2005/11/22 18:17:06 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/access/nbtree/nbtsearch.c,v 1.98 2005/12/07 18:03:48 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -889,9 +889,9 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
889889
bool
890890
_bt_step(IndexScanDesc scan, Buffer *bufP, ScanDirection dir)
891891
{
892-
Relation rel = scan->indexRelation;
893892
ItemPointer current = &(scan->currentItemData);
894893
BTScanOpaque so = (BTScanOpaque) scan->opaque;
894+
Relation rel;
895895
Page page;
896896
BTPageOpaque opaque;
897897
OffsetNumber offnum,
@@ -905,16 +905,17 @@ _bt_step(IndexScanDesc scan, Buffer *bufP, ScanDirection dir)
905905
offnum = current->ip_posid;
906906

907907
page = BufferGetPage(*bufP);
908-
opaque = (BTPageOpaque) PageGetSpecialPointer(page);
909908
maxoff = PageGetMaxOffsetNumber(page);
910909

911910
if (ScanDirectionIsForward(dir))
912911
{
913-
if (!PageIsEmpty(page) && offnum < maxoff)
912+
if (offnum < maxoff)
914913
offnum = OffsetNumberNext(offnum);
915914
else
916915
{
917916
/* Walk right to the next page with data */
917+
rel = scan->indexRelation;
918+
opaque = (BTPageOpaque) PageGetSpecialPointer(page);
918919
for (;;)
919920
{
920921
/* if we're at end of scan, release the buffer and return */
@@ -932,10 +933,10 @@ _bt_step(IndexScanDesc scan, Buffer *bufP, ScanDirection dir)
932933
opaque = (BTPageOpaque) PageGetSpecialPointer(page);
933934
if (!P_IGNORE(opaque))
934935
{
935-
maxoff = PageGetMaxOffsetNumber(page);
936936
/* done if it's not empty */
937+
maxoff = PageGetMaxOffsetNumber(page);
937938
offnum = P_FIRSTDATAKEY(opaque);
938-
if (!PageIsEmpty(page) && offnum <= maxoff)
939+
if (offnum <= maxoff)
939940
break;
940941
}
941942
}
@@ -944,6 +945,7 @@ _bt_step(IndexScanDesc scan, Buffer *bufP, ScanDirection dir)
944945
else
945946
{
946947
/* backwards scan */
948+
opaque = (BTPageOpaque) PageGetSpecialPointer(page);
947949
if (offnum > P_FIRSTDATAKEY(opaque))
948950
offnum = OffsetNumberPrev(offnum);
949951
else
@@ -955,6 +957,7 @@ _bt_step(IndexScanDesc scan, Buffer *bufP, ScanDirection dir)
955957
* plus the possibility that the page we were on gets deleted
956958
* after we leave it. See nbtree/README for details.
957959
*/
960+
rel = scan->indexRelation;
958961
for (;;)
959962
{
960963
*bufP = _bt_walk_left(rel, *bufP);
@@ -978,8 +981,7 @@ _bt_step(IndexScanDesc scan, Buffer *bufP, ScanDirection dir)
978981
{
979982
maxoff = PageGetMaxOffsetNumber(page);
980983
offnum = maxoff;
981-
if (!PageIsEmpty(page) &&
982-
maxoff >= P_FIRSTDATAKEY(opaque))
984+
if (maxoff >= P_FIRSTDATAKEY(opaque))
983985
break;
984986
}
985987
}

0 commit comments

Comments
 (0)