Skip site navigation (1) Skip section navigation (2)

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 (view raw or flat)
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: string_info_stack_alloc-3.patch
Description: text/x-patch (31.3 KB)

Responses

pgsql-patches by date

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

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group