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
f0705bb62.

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
This commit is contained in:
David Rowley 2020-04-08 18:29:51 +12:00
parent c0187869a0
commit 02a2e8b442
5 changed files with 16 additions and 38 deletions

View File

@ -25,6 +25,7 @@
#include "catalog/pg_am.h" #include "catalog/pg_am.h"
#include "commands/vacuum.h" #include "commands/vacuum.h"
#include "miscadmin.h" #include "miscadmin.h"
#include "port/pg_bitutils.h"
#include "postmaster/autovacuum.h" #include "postmaster/autovacuum.h"
#include "storage/indexfsm.h" #include "storage/indexfsm.h"
#include "storage/lmgr.h" #include "storage/lmgr.h"
@ -503,10 +504,7 @@ ginHeapTupleFastCollect(GinState *ginstate,
* initially. Make it a power of 2 to avoid wasting memory when * initially. Make it a power of 2 to avoid wasting memory when
* resizing (since palloc likes powers of 2). * resizing (since palloc likes powers of 2).
*/ */
collector->lentuples = 16; collector->lentuples = pg_nextpower2_32(Max(16, nentries));
while (collector->lentuples < nentries)
collector->lentuples *= 2;
collector->tuples = (IndexTuple *) palloc(sizeof(IndexTuple) * collector->lentuples); collector->tuples = (IndexTuple *) palloc(sizeof(IndexTuple) * collector->lentuples);
} }
else if (collector->lentuples < collector->ntuples + nentries) else if (collector->lentuples < collector->ntuples + nentries)
@ -516,11 +514,7 @@ ginHeapTupleFastCollect(GinState *ginstate,
* overflow, though we could get to a value that exceeds * overflow, though we could get to a value that exceeds
* MaxAllocSize/sizeof(IndexTuple), causing an error in repalloc. * MaxAllocSize/sizeof(IndexTuple), causing an error in repalloc.
*/ */
do collector->lentuples = pg_nextpower2_32(collector->ntuples + nentries);
{
collector->lentuples *= 2;
} while (collector->lentuples < collector->ntuples + nentries);
collector->tuples = (IndexTuple *) repalloc(collector->tuples, collector->tuples = (IndexTuple *) repalloc(collector->tuples,
sizeof(IndexTuple) * collector->lentuples); sizeof(IndexTuple) * collector->lentuples);
} }

View File

@ -831,9 +831,7 @@ ExecChooseHashTableSize(double ntuples, int tupwidth, bool useskew,
dbatch = ceil(inner_rel_bytes / (hash_table_bytes - bucket_bytes)); dbatch = ceil(inner_rel_bytes / (hash_table_bytes - bucket_bytes));
dbatch = Min(dbatch, max_pointers); dbatch = Min(dbatch, max_pointers);
minbatch = (int) dbatch; minbatch = (int) dbatch;
nbatch = 2; nbatch = pg_nextpower2_32(Max(2, minbatch));
while (nbatch < minbatch)
nbatch <<= 1;
} }
Assert(nbuckets > 0); Assert(nbuckets > 0);
@ -2272,9 +2270,7 @@ ExecHashBuildSkewHash(HashJoinTable hashtable, Hash *node, int mcvsToUse)
* MaxAllocSize/sizeof(void *)/8, but that is not currently possible * MaxAllocSize/sizeof(void *)/8, but that is not currently possible
* since we limit pg_statistic entries to much less than that. * since we limit pg_statistic entries to much less than that.
*/ */
nbuckets = 2; nbuckets = pg_nextpower2_32(mcvsToUse + 1);
while (nbuckets <= mcvsToUse)
nbuckets <<= 1;
/* use two more bits just to help avoid collisions */ /* use two more bits just to help avoid collisions */
nbuckets <<= 2; nbuckets <<= 2;

View File

@ -18,6 +18,7 @@
#include "postgres.h" #include "postgres.h"
#include "nodes/pg_list.h" #include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h" #include "utils/memdebug.h"
#include "utils/memutils.h" #include "utils/memutils.h"
@ -119,9 +120,7 @@ new_list(NodeTag type, int min_size)
* that's more than twice the size of an existing list, so the size limits * that's more than twice the size of an existing list, so the size limits
* within palloc will ensure that we don't overflow here. * within palloc will ensure that we don't overflow here.
*/ */
max_size = 8; /* semi-arbitrary small power of 2 */ max_size = pg_nextpower2_32(Max(8, min_size + LIST_HEADER_OVERHEAD));
while (max_size < min_size + LIST_HEADER_OVERHEAD)
max_size *= 2;
max_size -= LIST_HEADER_OVERHEAD; max_size -= LIST_HEADER_OVERHEAD;
#else #else
@ -160,12 +159,12 @@ enlarge_list(List *list, int min_size)
/* /*
* As above, we prefer power-of-two total allocations; but here we need * As above, we prefer power-of-two total allocations; but here we need
* not account for list header overhead. The existing max length might * not account for list header overhead.
* not be a power of 2, so don't rely on that.
*/ */
new_max_len = 16; /* semi-arbitrary small power of 2 */
while (new_max_len < min_size) /* clamp the minimum value to 16, a semi-arbitrary small power of 2 */
new_max_len *= 2; new_max_len = pg_nextpower2_32(Max(16, min_size));
#else #else
/* As above, don't allocate anything extra */ /* As above, don't allocate anything extra */
new_max_len = min_size; new_max_len = min_size;

View File

@ -576,15 +576,7 @@ n_choose_k(int n, int k)
static int static int
num_combinations(int n) num_combinations(int n)
{ {
int k; return (1 << n) - (n + 1);
int ncombs = 1;
for (k = 1; k <= n; k++)
ncombs *= 2;
ncombs -= (n + 1);
return ncombs;
} }
/* /*

View File

@ -24,6 +24,7 @@
#include "nodes/nodeFuncs.h" #include "nodes/nodeFuncs.h"
#include "nodes/supportnodes.h" #include "nodes/supportnodes.h"
#include "optimizer/optimizer.h" #include "optimizer/optimizer.h"
#include "port/pg_bitutils.h"
#include "utils/array.h" #include "utils/array.h"
#include "utils/arrayaccess.h" #include "utils/arrayaccess.h"
#include "utils/builtins.h" #include "utils/builtins.h"
@ -5313,9 +5314,7 @@ accumArrayResultArr(ArrayBuildStateArr *astate,
memcpy(&astate->lbs[1], lbs, ndims * sizeof(int)); memcpy(&astate->lbs[1], lbs, ndims * sizeof(int));
/* Allocate at least enough data space for this item */ /* Allocate at least enough data space for this item */
astate->abytes = 1024; astate->abytes = pg_nextpower2_32(Max(1024, ndatabytes + 1));
while (astate->abytes <= ndatabytes)
astate->abytes *= 2;
astate->data = (char *) palloc(astate->abytes); astate->data = (char *) palloc(astate->abytes);
} }
else else
@ -5362,9 +5361,7 @@ accumArrayResultArr(ArrayBuildStateArr *astate,
* First input with nulls; we must retrospectively handle any * First input with nulls; we must retrospectively handle any
* previous inputs by marking all their items non-null. * previous inputs by marking all their items non-null.
*/ */
astate->aitems = 256; astate->aitems = pg_nextpower2_32(Max(256, newnitems + 1));
while (astate->aitems <= newnitems)
astate->aitems *= 2;
astate->nullbitmap = (bits8 *) palloc((astate->aitems + 7) / 8); astate->nullbitmap = (bits8 *) palloc((astate->aitems + 7) / 8);
array_bitmap_copy(astate->nullbitmap, 0, array_bitmap_copy(astate->nullbitmap, 0,
NULL, 0, NULL, 0,