aboutsummaryrefslogtreecommitdiff
path: root/src
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 /src
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 'src')
-rw-r--r--src/backend/commands/explain.c24
-rw-r--r--src/backend/utils/adt/varlena.c55
2 files changed, 39 insertions, 40 deletions
diff --git a/src/backend/commands/explain.c b/src/backend/commands/explain.c
index 755a64a9446..767d4c9f806 100644
--- a/src/backend/commands/explain.c
+++ b/src/backend/commands/explain.c
@@ -7,7 +7,7 @@
* Portions Copyright (c) 1994-5, Regents of the University of California
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/commands/explain.c,v 1.144 2006/02/28 04:10:27 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/commands/explain.c,v 1.145 2006/03/01 06:51:01 neilc Exp $
*
*-------------------------------------------------------------------------
*/
@@ -232,7 +232,7 @@ ExplainOnePlan(QueryDesc *queryDesc, ExplainStmt *stmt,
instr_time starttime;
double totaltime = 0;
ExplainState *es;
- StringInfo str;
+ StringInfoData buf;
int eflags;
INSTR_TIME_SET_CURRENT(starttime);
@@ -285,9 +285,8 @@ ExplainOnePlan(QueryDesc *queryDesc, ExplainStmt *stmt,
}
}
- str = makeStringInfo();
-
- explain_outNode(str, queryDesc->plantree, queryDesc->planstate,
+ initStringInfo(&buf);
+ explain_outNode(&buf, queryDesc->plantree, queryDesc->planstate,
NULL, 0, es);
/*
@@ -335,18 +334,18 @@ ExplainOnePlan(QueryDesc *queryDesc, ExplainStmt *stmt,
if (trig->tgisconstraint &&
(conname = GetConstraintNameForTrigger(trig->tgoid)) != NULL)
{
- appendStringInfo(str, "Trigger for constraint %s",
+ appendStringInfo(&buf, "Trigger for constraint %s",
conname);
pfree(conname);
}
else
- appendStringInfo(str, "Trigger %s", trig->tgname);
+ appendStringInfo(&buf, "Trigger %s", trig->tgname);
if (numrels > 1)
- appendStringInfo(str, " on %s",
+ appendStringInfo(&buf, " on %s",
RelationGetRelationName(rInfo->ri_RelationDesc));
- appendStringInfo(str, ": time=%.3f calls=%.0f\n",
+ appendStringInfo(&buf, ": time=%.3f calls=%.0f\n",
1000.0 * instr->total,
instr->ntuples);
}
@@ -370,12 +369,11 @@ ExplainOnePlan(QueryDesc *queryDesc, ExplainStmt *stmt,
totaltime += elapsed_time(&starttime);
if (stmt->analyze)
- appendStringInfo(str, "Total runtime: %.3f ms\n",
+ appendStringInfo(&buf, "Total runtime: %.3f ms\n",
1000.0 * totaltime);
- do_text_output_multiline(tstate, str->data);
+ do_text_output_multiline(tstate, buf.data);
- pfree(str->data);
- pfree(str);
+ pfree(buf.data);
pfree(es);
}
diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c
index c2119edcecc..cbfcf52ffe3 100644
--- a/src/backend/utils/adt/varlena.c
+++ b/src/backend/utils/adt/varlena.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/utils/adt/varlena.c,v 1.143 2006/02/26 02:23:41 neilc Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/adt/varlena.c,v 1.144 2006/03/01 06:51:01 neilc Exp $
*
*-------------------------------------------------------------------------
*/
@@ -2038,7 +2038,7 @@ replace_text(PG_FUNCTION_ARGS)
text *buf_text;
text *ret_text;
int curr_posn;
- StringInfo str;
+ StringInfoData str;
if (src_text_len == 0 || from_sub_text_len == 0)
PG_RETURN_TEXT_P(src_text);
@@ -2049,7 +2049,7 @@ replace_text(PG_FUNCTION_ARGS)
if (curr_posn == 0)
PG_RETURN_TEXT_P(src_text);
- str = makeStringInfo();
+ initStringInfo(&str);
buf_text = src_text;
while (curr_posn > 0)
@@ -2059,8 +2059,8 @@ replace_text(PG_FUNCTION_ARGS)
right_text = text_substring(PointerGetDatum(buf_text),
curr_posn + from_sub_text_len, -1, true);
- appendStringInfoText(str, left_text);
- appendStringInfoText(str, to_sub_text);
+ appendStringInfoText(&str, left_text);
+ appendStringInfoText(&str, to_sub_text);
if (buf_text != src_text)
pfree(buf_text);
@@ -2069,13 +2069,12 @@ replace_text(PG_FUNCTION_ARGS)
curr_posn = TEXTPOS(buf_text, from_sub_text);
}
- appendStringInfoText(str, buf_text);
+ appendStringInfoText(&str, buf_text);
if (buf_text != src_text)
pfree(buf_text);
- ret_text = PG_STR_GET_TEXT(str->data);
- pfree(str->data);
- pfree(str);
+ ret_text = PG_STR_GET_TEXT(str.data);
+ pfree(str.data);
PG_RETURN_TEXT_P(ret_text);
}
@@ -2227,8 +2226,7 @@ replace_text_regexp(text *src_text, void *regexp,
text *ret_text;
regex_t *re = (regex_t *) regexp;
int src_text_len = VARSIZE(src_text) - VARHDRSZ;
- StringInfo str = makeStringInfo();
- int regexec_result;
+ StringInfoData buf;
regmatch_t pmatch[REGEXP_REPLACE_BACKREF_CNT];
pg_wchar *data;
size_t data_len;
@@ -2236,6 +2234,8 @@ replace_text_regexp(text *src_text, void *regexp,
int data_pos;
bool have_escape;
+ initStringInfo(&buf);
+
/* Convert data string to wide characters. */
data = (pg_wchar *) palloc((src_text_len + 1) * sizeof(pg_wchar));
data_len = pg_mb2wchar_with_len(VARDATA(src_text), data, src_text_len);
@@ -2245,6 +2245,8 @@ replace_text_regexp(text *src_text, void *regexp,
for (search_start = data_pos = 0; search_start <= data_len;)
{
+ int regexec_result;
+
regexec_result = pg_regexec(re,
data,
data_len,
@@ -2254,20 +2256,19 @@ replace_text_regexp(text *src_text, void *regexp,
pmatch,
0);
- if (regexec_result != REG_OKAY && regexec_result != REG_NOMATCH)
+ if (regexec_result == REG_NOMATCH)
+ break;
+
+ if (regexec_result != REG_OKAY)
{
char errMsg[100];
- /* re failed??? */
pg_regerror(regexec_result, re, errMsg, sizeof(errMsg));
ereport(ERROR,
(errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
errmsg("regular expression failed: %s", errMsg)));
}
- if (regexec_result == REG_NOMATCH)
- break;
-
/*
* Copy the text to the left of the match position. Because we are
* working with character not byte indexes, it's easiest to use
@@ -2281,7 +2282,7 @@ replace_text_regexp(text *src_text, void *regexp,
data_pos + 1,
pmatch[0].rm_so - data_pos,
false);
- appendStringInfoText(str, left_text);
+ appendStringInfoText(&buf, left_text);
pfree(left_text);
}
@@ -2290,9 +2291,9 @@ replace_text_regexp(text *src_text, void *regexp,
* replace_text has escape characters.
*/
if (have_escape)
- appendStringInfoRegexpSubstr(str, replace_text, pmatch, src_text);
+ appendStringInfoRegexpSubstr(&buf, replace_text, pmatch, src_text);
else
- appendStringInfoText(str, replace_text);
+ appendStringInfoText(&buf, replace_text);
search_start = data_pos = pmatch[0].rm_eo;
@@ -2318,13 +2319,12 @@ replace_text_regexp(text *src_text, void *regexp,
right_text = text_substring(PointerGetDatum(src_text),
data_pos + 1, -1, true);
- appendStringInfoText(str, right_text);
+ appendStringInfoText(&buf, right_text);
pfree(right_text);
}
- ret_text = PG_STR_GET_TEXT(str->data);
- pfree(str->data);
- pfree(str);
+ ret_text = PG_STR_GET_TEXT(buf.data);
+ pfree(buf.data);
pfree(data);
return ret_text;
@@ -2512,7 +2512,7 @@ array_to_text(PG_FUNCTION_ARGS)
int typlen;
bool typbyval;
char typalign;
- StringInfo result_str = makeStringInfo();
+ StringInfoData buf;
bool printed = false;
char *p;
bits8 *bitmap;
@@ -2529,6 +2529,7 @@ array_to_text(PG_FUNCTION_ARGS)
PG_RETURN_TEXT_P(PG_STR_GET_TEXT(""));
element_type = ARR_ELEMTYPE(v);
+ initStringInfo(&buf);
/*
* We arrange to look up info about element type, including its output
@@ -2583,9 +2584,9 @@ array_to_text(PG_FUNCTION_ARGS)
itemvalue));
if (printed)
- appendStringInfo(result_str, "%s%s", fldsep, value);
+ appendStringInfo(&buf, "%s%s", fldsep, value);
else
- appendStringInfoString(result_str, value);
+ appendStringInfoString(&buf, value);
printed = true;
p = att_addlength(p, typlen, PointerGetDatum(p));
@@ -2604,7 +2605,7 @@ array_to_text(PG_FUNCTION_ARGS)
}
}
- PG_RETURN_TEXT_P(PG_STR_GET_TEXT(result_str->data));
+ PG_RETURN_TEXT_P(PG_STR_GET_TEXT(buf.data));
}
#define HEXBASE 16