postgresql/src/backend/tsearch
David Rowley 3788c66788 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
2021-07-01 15:29:06 +12:00
..
dicts Update copyright for 2019 2019-01-02 12:44:25 -05:00
Makefile Update copyright for 2021 2021-01-02 13:06:25 -05:00
dict.c Update copyright for 2021 2021-01-02 13:06:25 -05:00
dict_ispell.c Update copyright for 2021 2021-01-02 13:06:25 -05:00
dict_simple.c Update copyright for 2021 2021-01-02 13:06:25 -05:00
dict_synonym.c Update copyright for 2021 2021-01-02 13:06:25 -05:00
dict_thesaurus.c Update copyright for 2021 2021-01-02 13:06:25 -05:00
regis.c Update copyright for 2021 2021-01-02 13:06:25 -05:00
spell.c Improve various places that double the size of a buffer 2021-07-01 15:29:06 +12:00
to_tsany.c Fix parsing of complex morphs to tsquery 2021-01-31 20:14:29 +03:00
ts_locale.c Update copyright for 2021 2021-01-02 13:06:25 -05:00
ts_parse.c Improve various places that double the size of a buffer 2021-07-01 15:29:06 +12:00
ts_selfuncs.c Update copyright for 2021 2021-01-02 13:06:25 -05:00
ts_typanalyze.c Update copyright for 2021 2021-01-02 13:06:25 -05:00
ts_utils.c Update copyright for 2021 2021-01-02 13:06:25 -05:00
wparser.c Update copyright for 2021 2021-01-02 13:06:25 -05:00
wparser_def.c Update copyright for 2021 2021-01-02 13:06:25 -05:00