Re: Use pg_nextpower2_* in a few more places

From: Zhihong Yu <zyu(at)yugabyte(dot)com>
To: David Rowley <dgrowleyml(at)gmail(dot)com>
Cc: PostgreSQL Developers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Use pg_nextpower2_* in a few more places
Date: 2021-06-12 12:55:13
Message-ID: CALNJ-vSDUpX+OiE4ZLKkAmbrC+udLBATWFbZ+J__jLTsy75_Vg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Sat, Jun 12, 2021 at 5:32 AM David Rowley <dgrowleyml(at)gmail(dot)com> wrote:

> Back in f0705bb62, we added pg_nextpower2_32 and pg_nextpower2_64 to
> efficiently obtain the next power of 2 of a given number using an
> intrinsic function to find the left-most 1 bit.
>
> In d025cf88b and 02a2e8b44, I added some usages of these new functions
> but I didn't quite get all of them done. The attached replaces all
> of the remaining ones that I'm happy enough to go near.
>
> The ones that I left behind are ones in the form of:
>
> while (reqsize >= buflen)
> {
> buflen *= 2;
> buf = repalloc(buf, buflen);
> }
>
> The reason I left those behind is that I was too scared that I might
> introduce an opportunity to wrap buflen back around to zero again. At
> the moment the repalloc() would prevent that as we'd go above
> MaxAllocSize before we wrapped buflen back to zero again. All the
> other places I touched does not change the risk of that happening.
>
> It would be nice to get rid of doing that repalloc() in a loop, but it
> would need a bit more study to ensure we couldn't wrap or we'd need to
> add some error checking code that raised an ERROR if it did wrap. I
> don't want to touch those as part of this effort.
>
> I've also fixed up a few places that were just doubling the size of a
> buffer but used a "while" loop to do this when a simple "if" would
> have done. Using an "if" is ever so slightly more optimal since the
> condition will be checked once rather than twice when the buffer needs
> to increase in size.
>
> I'd like to fix these for PG15.
>
> David
>
Hi,

- newalloc = Max(LWLockTrancheNamesAllocated, 8);
- while (newalloc <= tranche_id)
- newalloc *= 2;
+ newalloc = pg_nextpower2_32(Max(8, tranche_id + 1));

Should LWLockTrancheNamesAllocated be included in the Max() expression (in
case it gets to a high value) ?

Cheers

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Amit Kapila 2021-06-12 12:57:02 Re: Failure in subscription test 004_sync.pl
Previous Message David Rowley 2021-06-12 12:31:31 Use pg_nextpower2_* in a few more places