Skip to content

Commit 02a2e8b

Browse files
committed
Modify additional power 2 calculations to use new helper functions
2nd pass of modifying various places which obtain the next power of 2 of a number and make them use the new functions added in f0705bb. In passing, also modify num_combinations(). This can be implemented using simple bitshifting rather than looping. Reviewed-by: John Naylor Discussion: https://postgr.es/m/20200114173553.GE32763%40fetter.org
1 parent c018786 commit 02a2e8b

File tree

5 files changed

+16
-38
lines changed

5 files changed

+16
-38
lines changed

Diff for: src/backend/access/gin/ginfast.c

+3-9
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "catalog/pg_am.h"
2626
#include "commands/vacuum.h"
2727
#include "miscadmin.h"
28+
#include "port/pg_bitutils.h"
2829
#include "postmaster/autovacuum.h"
2930
#include "storage/indexfsm.h"
3031
#include "storage/lmgr.h"
@@ -503,10 +504,7 @@ ginHeapTupleFastCollect(GinState *ginstate,
503504
* initially. Make it a power of 2 to avoid wasting memory when
504505
* resizing (since palloc likes powers of 2).
505506
*/
506-
collector->lentuples = 16;
507-
while (collector->lentuples < nentries)
508-
collector->lentuples *= 2;
509-
507+
collector->lentuples = pg_nextpower2_32(Max(16, nentries));
510508
collector->tuples = (IndexTuple *) palloc(sizeof(IndexTuple) * collector->lentuples);
511509
}
512510
else if (collector->lentuples < collector->ntuples + nentries)
@@ -516,11 +514,7 @@ ginHeapTupleFastCollect(GinState *ginstate,
516514
* overflow, though we could get to a value that exceeds
517515
* MaxAllocSize/sizeof(IndexTuple), causing an error in repalloc.
518516
*/
519-
do
520-
{
521-
collector->lentuples *= 2;
522-
} while (collector->lentuples < collector->ntuples + nentries);
523-
517+
collector->lentuples = pg_nextpower2_32(collector->ntuples + nentries);
524518
collector->tuples = (IndexTuple *) repalloc(collector->tuples,
525519
sizeof(IndexTuple) * collector->lentuples);
526520
}

Diff for: src/backend/executor/nodeHash.c

+2-6
Original file line numberDiff line numberDiff line change
@@ -831,9 +831,7 @@ ExecChooseHashTableSize(double ntuples, int tupwidth, bool useskew,
831831
dbatch = ceil(inner_rel_bytes / (hash_table_bytes - bucket_bytes));
832832
dbatch = Min(dbatch, max_pointers);
833833
minbatch = (int) dbatch;
834-
nbatch = 2;
835-
while (nbatch < minbatch)
836-
nbatch <<= 1;
834+
nbatch = pg_nextpower2_32(Max(2, minbatch));
837835
}
838836

839837
Assert(nbuckets > 0);
@@ -2272,9 +2270,7 @@ ExecHashBuildSkewHash(HashJoinTable hashtable, Hash *node, int mcvsToUse)
22722270
* MaxAllocSize/sizeof(void *)/8, but that is not currently possible
22732271
* since we limit pg_statistic entries to much less than that.
22742272
*/
2275-
nbuckets = 2;
2276-
while (nbuckets <= mcvsToUse)
2277-
nbuckets <<= 1;
2273+
nbuckets = pg_nextpower2_32(mcvsToUse + 1);
22782274
/* use two more bits just to help avoid collisions */
22792275
nbuckets <<= 2;
22802276

Diff for: src/backend/nodes/list.c

+7-8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "postgres.h"
1919

2020
#include "nodes/pg_list.h"
21+
#include "port/pg_bitutils.h"
2122
#include "utils/memdebug.h"
2223
#include "utils/memutils.h"
2324

@@ -119,9 +120,7 @@ new_list(NodeTag type, int min_size)
119120
* that's more than twice the size of an existing list, so the size limits
120121
* within palloc will ensure that we don't overflow here.
121122
*/
122-
max_size = 8; /* semi-arbitrary small power of 2 */
123-
while (max_size < min_size + LIST_HEADER_OVERHEAD)
124-
max_size *= 2;
123+
max_size = pg_nextpower2_32(Max(8, min_size + LIST_HEADER_OVERHEAD));
125124
max_size -= LIST_HEADER_OVERHEAD;
126125
#else
127126

@@ -160,12 +159,12 @@ enlarge_list(List *list, int min_size)
160159

161160
/*
162161
* As above, we prefer power-of-two total allocations; but here we need
163-
* not account for list header overhead. The existing max length might
164-
* not be a power of 2, so don't rely on that.
162+
* not account for list header overhead.
165163
*/
166-
new_max_len = 16; /* semi-arbitrary small power of 2 */
167-
while (new_max_len < min_size)
168-
new_max_len *= 2;
164+
165+
/* clamp the minimum value to 16, a semi-arbitrary small power of 2 */
166+
new_max_len = pg_nextpower2_32(Max(16, min_size));
167+
169168
#else
170169
/* As above, don't allocate anything extra */
171170
new_max_len = min_size;

Diff for: src/backend/statistics/mvdistinct.c

+1-9
Original file line numberDiff line numberDiff line change
@@ -576,15 +576,7 @@ n_choose_k(int n, int k)
576576
static int
577577
num_combinations(int n)
578578
{
579-
int k;
580-
int ncombs = 1;
581-
582-
for (k = 1; k <= n; k++)
583-
ncombs *= 2;
584-
585-
ncombs -= (n + 1);
586-
587-
return ncombs;
579+
return (1 << n) - (n + 1);
588580
}
589581

590582
/*

Diff for: src/backend/utils/adt/arrayfuncs.c

+3-6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "nodes/nodeFuncs.h"
2525
#include "nodes/supportnodes.h"
2626
#include "optimizer/optimizer.h"
27+
#include "port/pg_bitutils.h"
2728
#include "utils/array.h"
2829
#include "utils/arrayaccess.h"
2930
#include "utils/builtins.h"
@@ -5313,9 +5314,7 @@ accumArrayResultArr(ArrayBuildStateArr *astate,
53135314
memcpy(&astate->lbs[1], lbs, ndims * sizeof(int));
53145315

53155316
/* Allocate at least enough data space for this item */
5316-
astate->abytes = 1024;
5317-
while (astate->abytes <= ndatabytes)
5318-
astate->abytes *= 2;
5317+
astate->abytes = pg_nextpower2_32(Max(1024, ndatabytes + 1));
53195318
astate->data = (char *) palloc(astate->abytes);
53205319
}
53215320
else
@@ -5362,9 +5361,7 @@ accumArrayResultArr(ArrayBuildStateArr *astate,
53625361
* First input with nulls; we must retrospectively handle any
53635362
* previous inputs by marking all their items non-null.
53645363
*/
5365-
astate->aitems = 256;
5366-
while (astate->aitems <= newnitems)
5367-
astate->aitems *= 2;
5364+
astate->aitems = pg_nextpower2_32(Max(256, newnitems + 1));
53685365
astate->nullbitmap = (bits8 *) palloc((astate->aitems + 7) / 8);
53695366
array_bitmap_copy(astate->nullbitmap, 0,
53705367
NULL, 0,

0 commit comments

Comments
 (0)