Skip to content

Commit 9f1ca6c

Browse files
committed
Use appendStringInfoSpaces in more places
This adjusts a few places which were appending a string constant containing spaces onto a StringInfo. We have appendStringInfoSpaces for that job, so let's use that instead. For the change to jsonb.c's add_indent() function, appendStringInfoString was being called inside a loop to append 4 spaces on each loop. This meant that enlargeStringInfo would get called once per loop. Here it should be much more efficient to get rid of the loop and just calculate the number of spaces with "level * 4" and just append all the spaces in one go. Here we additionally adjust the appendStringInfoSpaces function so it makes use of memset rather than a while loop to apply the required spaces to the StringInfo. One of the problems with the while loop was that it was incrementing one variable and decrementing another variable once per loop. That's more work than what's required to get the job done. We may as well use memset for this rather than trying to optimize the existing loop. Some testing has shown memset is faster even for very small sizes. Discussion: https://postgr.es/m/CAApHDvp_rKkvwudBKgBHniNRg67bzXVjyvVKfX0G2zS967K43A@mail.gmail.com
1 parent 1ca604c commit 9f1ca6c

File tree

3 files changed

+4
-7
lines changed

3 files changed

+4
-7
lines changed

src/backend/commands/explain.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3324,7 +3324,7 @@ show_hashagg_info(AggState *aggstate, ExplainState *es)
33243324
if (!gotone)
33253325
ExplainIndentText(es);
33263326
else
3327-
appendStringInfoString(es->str, " ");
3327+
appendStringInfoSpaces(es->str, 2);
33283328

33293329
appendStringInfo(es->str, "Batches: %d Memory Usage: " INT64_FORMAT "kB",
33303330
aggstate->hash_batches_used, memPeakKb);

src/backend/utils/adt/jsonb.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -626,11 +626,8 @@ add_indent(StringInfo out, bool indent, int level)
626626
{
627627
if (indent)
628628
{
629-
int i;
630-
631629
appendStringInfoCharMacro(out, '\n');
632-
for (i = 0; i < level; i++)
633-
appendBinaryStringInfo(out, " ", 4);
630+
appendStringInfoSpaces(out, level * 4);
634631
}
635632
}
636633

src/common/stringinfo.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,8 @@ appendStringInfoSpaces(StringInfo str, int count)
211211
enlargeStringInfo(str, count);
212212

213213
/* OK, append the spaces */
214-
while (--count >= 0)
215-
str->data[str->len++] = ' ';
214+
memset(&str->data[str->len], ' ', count);
215+
str->len += count;
216216
str->data[str->len] = '\0';
217217
}
218218
}

0 commit comments

Comments
 (0)