cleanup StringInfo usage

From: Neil Conway <neilc(at)samurai(dot)com>
To: pgsql-patches <pgsql-patches(at)postgresql(dot)org>
Subject: cleanup StringInfo usage
Date: 2006-02-28 19:21:10
Message-ID: 1141154470.8830.266.camel@localhost.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-patches

Attached is a patch that replaces a bunch of places where StringInfos
are unnecessarily allocated on the heap rather than the stack. That is,
this is sub-optimal:

{
StringInfo str;

str = makeStringInfo();
/* use str */

pfree(str->data);
pfree(str);
}

If the StringInfo doesn't outlive the stack frame in which it is
created, there is no need to allocate it on the heap via
makeStringInfo() -- stack allocation is faster:

{
StringInfoData str;

initStringInfo(&str);
/* use str */

pfree(str.data);
}

While it's not a big deal unless the code is in a critical path, I don't
see a reason not to save a few cycles -- using stack allocation is not
less readable.

A bunch of places in the tree (mostly contrib/) were using
makeStringInfo() when there was no need to do so -- this patch replaces
that with a stack-allocated StringInfoData and initStringInfo(). I also
cleaned up a bit of code along the way: moved variable declarations into
a more tightly-enclosing scope where possible, fixed some pointless
copying of strings in dblink, etc.

Barring any objections I'll apply this tomorrow.

-Neil

Attachment Content-Type Size
string_info_stack_alloc-3.patch text/x-patch 31.3 KB

Responses

Browse pgsql-patches by date

  From Date Subject
Next Message Neil Conway 2006-02-28 19:29:50 Re: <> operator
Previous Message Bruce Momjian 2006-02-28 18:37:51 Re: Zeroing damaged pages