summaryrefslogtreecommitdiff
path: root/src/backend/tsearch
diff options
context:
space:
mode:
authorDavid Rowley2021-07-01 03:29:06 +0000
committerDavid Rowley2021-07-01 03:29:06 +0000
commit3788c66788e9f8c6904c6fe903724c1f44812c4d (patch)
tree0b1acb1c37196dcf9dee74a7b2d8e03f411ed813 /src/backend/tsearch
parente45b0dfa1f1028948decad3abd3b0f6e913a44b0 (diff)
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
Diffstat (limited to 'src/backend/tsearch')
-rw-r--r--src/backend/tsearch/spell.c3
-rw-r--r--src/backend/tsearch/ts_parse.c2
2 files changed, 3 insertions, 2 deletions
diff --git a/src/backend/tsearch/spell.c b/src/backend/tsearch/spell.c
index ebc89604ac2..934be3d7d33 100644
--- a/src/backend/tsearch/spell.c
+++ b/src/backend/tsearch/spell.c
@@ -1600,7 +1600,8 @@ MergeAffix(IspellDict *Conf, int a1, int a2)
else if (*Conf->AffixData[a2] == '\0')
return a1;
- while (Conf->nAffixData + 1 >= Conf->lenAffixData)
+ /* Double the size of AffixData if there's not enough space */
+ if (Conf->nAffixData + 1 >= Conf->lenAffixData)
{
Conf->lenAffixData *= 2;
Conf->AffixData = (char **) repalloc(Conf->AffixData,
diff --git a/src/backend/tsearch/ts_parse.c b/src/backend/tsearch/ts_parse.c
index 92d95b4bd49..d978c8850de 100644
--- a/src/backend/tsearch/ts_parse.c
+++ b/src/backend/tsearch/ts_parse.c
@@ -436,7 +436,7 @@ parsetext(Oid cfgId, ParsedText *prs, char *buf, int buflen)
static void
hladdword(HeadlineParsedText *prs, char *buf, int buflen, int type)
{
- while (prs->curwords >= prs->lenwords)
+ if (prs->curwords >= prs->lenwords)
{
prs->lenwords *= 2;
prs->words = (HeadlineWordEntry *) repalloc((void *) prs->words, prs->lenwords * sizeof(HeadlineWordEntry));