aboutsummaryrefslogtreecommitdiff
path: root/contrib/tablefunc/tablefunc.c
diff options
context:
space:
mode:
authorNeil Conway <neilc@samurai.com>2006-03-01 06:51:01 +0000
committerNeil Conway <neilc@samurai.com>2006-03-01 06:51:01 +0000
commit0d9742f99aa0ba6b96d0729dd2cb2634a29d0be9 (patch)
tree3936c2893718f65ff538c359bd75bdcf14d9725e /contrib/tablefunc/tablefunc.c
parent8e5a10d46c38976e40504456a5978caa53b77b3c (diff)
downloadpostgresql-0d9742f99aa0ba6b96d0729dd2cb2634a29d0be9.tar.gz
postgresql-0d9742f99aa0ba6b96d0729dd2cb2634a29d0be9.zip
Attached is a patch that replaces a bunch of places where StringInfos
are unnecessarily allocated on the heap rather than the stack. 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. 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. 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.
Diffstat (limited to 'contrib/tablefunc/tablefunc.c')
-rw-r--r--contrib/tablefunc/tablefunc.c51
1 files changed, 24 insertions, 27 deletions
diff --git a/contrib/tablefunc/tablefunc.c b/contrib/tablefunc/tablefunc.c
index 0beb44f5845..aacc949ff28 100644
--- a/contrib/tablefunc/tablefunc.c
+++ b/contrib/tablefunc/tablefunc.c
@@ -1260,13 +1260,10 @@ build_tuplestore_recursively(char *key_fld,
{
TupleDesc tupdesc = attinmeta->tupdesc;
MemoryContext oldcontext;
- StringInfo sql = makeStringInfo();
int ret;
int proc;
int serial_column;
- StringInfo branchstr = NULL;
- StringInfo chk_branchstr = NULL;
- StringInfo chk_current_key = NULL;
+ StringInfoData sql;
char **values;
char *current_key;
char *current_key_parent;
@@ -1278,17 +1275,12 @@ build_tuplestore_recursively(char *key_fld,
if (max_depth > 0 && level > max_depth)
return tupstore;
- /* start a new branch */
- branchstr = makeStringInfo();
-
- /* need these to check for recursion */
- chk_branchstr = makeStringInfo();
- chk_current_key = makeStringInfo();
+ initStringInfo(&sql);
/* Build initial sql statement */
if (!show_serial)
{
- appendStringInfo(sql, "SELECT %s, %s FROM %s WHERE %s = %s AND %s IS NOT NULL AND %s <> %s",
+ appendStringInfo(&sql, "SELECT %s, %s FROM %s WHERE %s = %s AND %s IS NOT NULL AND %s <> %s",
key_fld,
parent_key_fld,
relname,
@@ -1299,7 +1291,7 @@ build_tuplestore_recursively(char *key_fld,
}
else
{
- appendStringInfo(sql, "SELECT %s, %s FROM %s WHERE %s = %s AND %s IS NOT NULL AND %s <> %s ORDER BY %s",
+ appendStringInfo(&sql, "SELECT %s, %s FROM %s WHERE %s = %s AND %s IS NOT NULL AND %s <> %s ORDER BY %s",
key_fld,
parent_key_fld,
relname,
@@ -1359,7 +1351,7 @@ build_tuplestore_recursively(char *key_fld,
}
/* Retrieve the desired rows */
- ret = SPI_execute(sql->data, true, 0);
+ ret = SPI_execute(sql.data, true, 0);
proc = SPI_processed;
/* Check for qualifying tuples */
@@ -1369,6 +1361,9 @@ build_tuplestore_recursively(char *key_fld,
SPITupleTable *tuptable = SPI_tuptable;
TupleDesc spi_tupdesc = tuptable->tupdesc;
int i;
+ StringInfoData branchstr;
+ StringInfoData chk_branchstr;
+ StringInfoData chk_current_key;
/* First time through, do a little more setup */
if (level == 0)
@@ -1389,28 +1384,35 @@ build_tuplestore_recursively(char *key_fld,
for (i = 0; i < proc; i++)
{
+ /* start a new branch */
+ initStringInfo(&branchstr);
+
+ /* need these to check for recursion */
+ initStringInfo(&chk_branchstr);
+ initStringInfo(&chk_current_key);
+
/* initialize branch for this pass */
- appendStringInfo(branchstr, "%s", branch);
- appendStringInfo(chk_branchstr, "%s%s%s", branch_delim, branch, branch_delim);
+ appendStringInfo(&branchstr, "%s", branch);
+ appendStringInfo(&chk_branchstr, "%s%s%s", branch_delim, branch, branch_delim);
/* get the next sql result tuple */
spi_tuple = tuptable->vals[i];
/* get the current key and parent */
current_key = SPI_getvalue(spi_tuple, spi_tupdesc, 1);
- appendStringInfo(chk_current_key, "%s%s%s", branch_delim, current_key, branch_delim);
+ appendStringInfo(&chk_current_key, "%s%s%s", branch_delim, current_key, branch_delim);
current_key_parent = pstrdup(SPI_getvalue(spi_tuple, spi_tupdesc, 2));
/* get the current level */
sprintf(current_level, "%d", level);
/* check to see if this key is also an ancestor */
- if (strstr(chk_branchstr->data, chk_current_key->data))
+ if (strstr(chk_branchstr.data, chk_current_key.data))
elog(ERROR, "infinite recursion detected");
/* OK, extend the branch */
- appendStringInfo(branchstr, "%s%s", branch_delim, current_key);
- current_branch = branchstr->data;
+ appendStringInfo(&branchstr, "%s%s", branch_delim, current_key);
+ current_branch = branchstr.data;
/* build a tuple */
values[0] = pstrdup(current_key);
@@ -1461,14 +1463,9 @@ build_tuplestore_recursively(char *key_fld,
tupstore);
/* reset branch for next pass */
- xpfree(branchstr->data);
- initStringInfo(branchstr);
-
- xpfree(chk_branchstr->data);
- initStringInfo(chk_branchstr);
-
- xpfree(chk_current_key->data);
- initStringInfo(chk_current_key);
+ xpfree(branchstr.data);
+ xpfree(chk_branchstr.data);
+ xpfree(chk_current_key.data);
}
}