Skip to content

Commit d961a56

Browse files
committed
Avoid unnecessary palloc overhead in _bt_first(). The temporary
scankeys arrays that it needs can never have more than INDEX_MAX_KEYS entries, so it's reasonable to just allocate them as fixed-size local arrays, and save the cost of palloc/pfree. Not a huge savings, but a cycle saved is a cycle earned ...
1 parent fc65458 commit d961a56

File tree

1 file changed

+4
-21
lines changed

1 file changed

+4
-21
lines changed

src/backend/access/nbtree/nbtsearch.c

Lines changed: 4 additions & 21 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.92 2005/06/13 23:14:48 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/access/nbtree/nbtsearch.c,v 1.93 2005/06/19 22:41:00 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -495,8 +495,8 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
495495
bool nextkey;
496496
bool goback;
497497
bool continuescan;
498-
ScanKey scankeys;
499-
ScanKey *startKeys = NULL;
498+
ScanKey startKeys[INDEX_MAX_KEYS];
499+
ScanKeyData scankeys[INDEX_MAX_KEYS];
500500
int keysCount = 0;
501501
int i;
502502
StrategyNumber strat_total;
@@ -552,8 +552,6 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
552552
ScanKey chosen;
553553
ScanKey cur;
554554

555-
startKeys = (ScanKey *) palloc(so->numberOfKeys * sizeof(ScanKey));
556-
557555
/*
558556
* chosen is the so-far-chosen key for the current attribute, if
559557
* any. We don't cast the decision in stone until we reach keys
@@ -636,18 +634,14 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
636634
* scan from there.
637635
*/
638636
if (keysCount == 0)
639-
{
640-
if (startKeys)
641-
pfree(startKeys);
642637
return _bt_endpoint(scan, dir);
643-
}
644638

645639
/*
646640
* We want to start the scan somewhere within the index. Set up a
647641
* 3-way-comparison scankey we can use to search for the boundary
648642
* point we identified above.
649643
*/
650-
scankeys = (ScanKey) palloc(keysCount * sizeof(ScanKeyData));
644+
Assert(keysCount <= INDEX_MAX_KEYS);
651645
for (i = 0; i < keysCount; i++)
652646
{
653647
ScanKey cur = startKeys[i];
@@ -657,12 +651,7 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
657651
* code later
658652
*/
659653
if (cur->sk_flags & SK_ISNULL)
660-
{
661-
pfree(startKeys);
662-
pfree(scankeys);
663654
elog(ERROR, "btree doesn't support is(not)null, yet");
664-
return false;
665-
}
666655

667656
/*
668657
* If scankey operator is of default subtype, we can use the
@@ -699,8 +688,6 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
699688
}
700689
}
701690

702-
pfree(startKeys);
703-
704691
/*
705692
* Examine the selected initial-positioning strategy to determine
706693
* exactly where we need to start the scan, and set flag variables to
@@ -809,7 +796,6 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
809796
/* Only get here if index is completely empty */
810797
ItemPointerSetInvalid(current);
811798
so->btso_curbuf = InvalidBuffer;
812-
pfree(scankeys);
813799
return false;
814800
}
815801

@@ -823,9 +809,6 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
823809
blkno = BufferGetBlockNumber(buf);
824810
ItemPointerSet(current, blkno, offnum);
825811

826-
/* done with manufactured scankey, now */
827-
pfree(scankeys);
828-
829812
/*
830813
* If nextkey = false, we are positioned at the first item >= scan
831814
* key, or possibly at the end of a page on which all the existing

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy