From 02a2e8b442002a698336954633b0ccc4e30061e6 Mon Sep 17 00:00:00 2001 From: David Rowley Date: Wed, 8 Apr 2020 18:29:51 +1200 Subject: 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 --- src/backend/nodes/list.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'src/backend/nodes') diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c index bd0c58cd814..80fa8c84e49 100644 --- a/src/backend/nodes/list.c +++ b/src/backend/nodes/list.c @@ -18,6 +18,7 @@ #include "postgres.h" #include "nodes/pg_list.h" +#include "port/pg_bitutils.h" #include "utils/memdebug.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 * within palloc will ensure that we don't overflow here. */ - max_size = 8; /* semi-arbitrary small power of 2 */ - while (max_size < min_size + LIST_HEADER_OVERHEAD) - max_size *= 2; + max_size = pg_nextpower2_32(Max(8, min_size + LIST_HEADER_OVERHEAD)); max_size -= LIST_HEADER_OVERHEAD; #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 - * not account for list header overhead. The existing max length might - * not be a power of 2, so don't rely on that. + * not account for list header overhead. */ - new_max_len = 16; /* semi-arbitrary small power of 2 */ - while (new_max_len < min_size) - new_max_len *= 2; + + /* clamp the minimum value to 16, a semi-arbitrary small power of 2 */ + new_max_len = pg_nextpower2_32(Max(16, min_size)); + #else /* As above, don't allocate anything extra */ new_max_len = min_size; -- cgit v1.2.3