pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: David Rowley <drowley@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Improve various places that double the size of a buffer
Date: Thu, 01 Jul 2021 03:29:38 +0000
Message-ID: <E1lynOU-0005Ms-W5@gemulon.postgresql.org> (raw)

Improve various places that double the size of a buffer

Several places were performing a tight loop to determine the first power
of 2 number that's > or >= the required memory.  Instead of using a loop
for that, we can use pg_nextpower2_32 or pg_nextpower2_64.  When we need a
power of 2 number equal to or greater than a given amount, we just pass
the amount to the nextpower2 function.  When we need a power of 2 greater
than the amount, we just pass the amount + 1.

Additionally, in tsearch there were a couple of locations that were
performing a while loop when a simple "if" would have done.  In both of
these locations only 1 item is being added, so the loop could only have
ever iterated once.  Changing the loop into an if statement makes the code
very slightly more optimal as the condition is checked once rather than
twice.

There are quite a few remaining locations that increase the size of the
buffer in the following form:

  while (reqsize >= buflen)
  {
     buflen *= 2;
     buf = repalloc(buf, buflen);
  }

These are not touched in this commit.  repalloc will error out for sizes
larger than MaxAllocSize.  Changing these to use pg_nextpower2_32 would
remove the chance of that error being raised.  It's unclear from the code
if the sizes could ever become that large, so err on the side of caution.

Discussion: https://postgr.es/m/CAApHDvp=tns7RL4PH0ZR0M+M-YFLquK7218x=0B_zO+DbOma+w@mail.gmail.com
Reviewed-by: Zhihong Yu

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/3788c66788e9f8c6904c6fe903724c1f44812c4d

Modified Files
--------------
src/backend/parser/scan.l          |  6 ++----
src/backend/storage/ipc/shm_mq.c   | 14 +++++++++-----
src/backend/storage/lmgr/lwlock.c  | 10 +++-------
src/backend/tsearch/spell.c        |  3 ++-
src/backend/tsearch/ts_parse.c     |  2 +-
src/backend/utils/cache/inval.c    |  4 ++--
src/backend/utils/cache/typcache.c |  6 ++----
7 files changed, 21 insertions(+), 24 deletions(-)



Message-ID: <E1lynOU-0005Ms-W5@gemulon.postgresql.org>
Permalink:  ../E1lynOU-0005Ms-W5@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1lynOU-0005Ms-W5@gemulon.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-committers@postgresql.org
  Cc: drowley@postgresql.org
  Subject: Re: pgsql: Improve various places that double the size of a buffer
  In-Reply-To: <E1lynOU-0005Ms-W5@gemulon.postgresql.org>
  References: <E1lynOU-0005Ms-W5@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox