Use stack allocated StringInfoDatas, where possible (round 2)

Started by Bertrand Drouvot8 months ago3 messageshackers
Jump to latest
#1Bertrand Drouvot
bertranddrouvot.pg@gmail.com

Hi hackers,

Commit 6d0eba66275 already did most of the changes but missed the opportunities
to $SUBJECT in sequencesync.c.

Indeed, 5509055d6956 added a few cases that are using StringInfo but don't need
that StringInfo to exist beyond the scope of the function were using makeStringInfo(),
which allocates both a StringInfoData and the buffer it uses as two separate
allocations. It's more efficient for these cases to use a StringInfoData on the
stack and initialize it with initStringInfo(), which only allocates the string
buffer.

The reason 6d0eba66275 missed those is that 5509055d6956 has been committed
between the patch proposal for 6d0eba66275 and 6d0eba66275.

I used Mats's coccinelle script (mentioned in [1]/messages/by-id/4379aac8-26f1-42f2-a356-ff0e886228d3@gmail.com) to find those and they are the
only remaining ones.

[1]: /messages/by-id/4379aac8-26f1-42f2-a356-ff0e886228d3@gmail.com

Regards,

--
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com

Attachments:

v1-0001-Use-stack-allocated-StringInfoDatas-where-possibl.patchtext/x-diff; charset=us-asciiDownload+21-19
#2David Rowley
dgrowleyml@gmail.com
In reply to: Bertrand Drouvot (#1)
Re: Use stack allocated StringInfoDatas, where possible (round 2)

On Sun, 12 Apr 2026 at 22:49, Bertrand Drouvot
<bertranddrouvot.pg@gmail.com> wrote:

Indeed, 5509055d6956 added a few cases that are using StringInfo but don't need
that StringInfo to exist beyond the scope of the function were using makeStringInfo(),
which allocates both a StringInfoData and the buffer it uses as two separate
allocations. It's more efficient for these cases to use a StringInfoData on the
stack and initialize it with initStringInfo(), which only allocates the string
buffer.

I think the author of copy_sequences() doesn't know what
resetStringInfo() does. I expect they think that it'll pfree all the
memory, but that's what destroyStringInfo() is for.

Since the strings are not used after the resetStringInfo(), the call
is pointless. Could you ask Amit K to fix that first? Then rebase your
patch atop.

I think it makes sense to apply your patch as a StringInfo fixup
post-freeze for v19 as leaving this for v20 will just result in the
code not matching for no particularly good reason.

David

#3David Rowley
dgrowleyml@gmail.com
In reply to: David Rowley (#2)
Re: Use stack allocated StringInfoDatas, where possible (round 2)

On Mon, 13 Apr 2026 at 01:34, David Rowley <dgrowleyml@gmail.com> wrote:

I think the author of copy_sequences() doesn't know what
resetStringInfo() does. I expect they think that it'll pfree all the
memory, but that's what destroyStringInfo() is for.

That was poor analysis. The resetStringInfo is there to reset the
StringInfo before the next loop. So nothing is wrong with it.

I've now pushed your patch.

David