From a8656a3ab02db5b1de0d696dc8cf588efe7c1341 Mon Sep 17 00:00:00 2001 From: Robert Haas Date: Mon, 2 Dec 2013 10:51:06 -0500 Subject: [PATCH] Make NUM_TOCHAR_prepare and NUM_TOCHAR_finish macros declare "len". Remove the variable from the enclosing scopes so that nothing can be relying on it. The net result of this refactoring is that we get rid of a few unnecessary strlen() calls. Original patch from Greg Jaskiewicz, substantially expanded by me. --- src/backend/utils/adt/formatting.c | 33 ++++++++++++++---------------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/src/backend/utils/adt/formatting.c b/src/backend/utils/adt/formatting.c index 36353c39c1..946f3e28c6 100644 --- a/src/backend/utils/adt/formatting.c +++ b/src/backend/utils/adt/formatting.c @@ -4877,7 +4877,7 @@ NUM_processor(FormatNode *node, NUMDesc *Num, char *inout, char *number, */ #define NUM_TOCHAR_prepare \ do { \ - len = VARSIZE_ANY_EXHDR(fmt); \ + int len = VARSIZE_ANY_EXHDR(fmt); \ if (len <= 0 || len >= (INT_MAX-VARHDRSZ)/NUM_MAX_ITEM_SIZ) \ PG_RETURN_TEXT_P(cstring_to_text("")); \ result = (text *) palloc0((len * NUM_MAX_ITEM_SIZ) + 1 + VARHDRSZ); \ @@ -4890,6 +4890,8 @@ do { \ */ #define NUM_TOCHAR_finish \ do { \ + int len; \ + \ NUM_processor(format, &Num, VARDATA(result), numstr, plen, sign, true, PG_GET_COLLATION()); \ \ if (shouldFree) \ @@ -4961,8 +4963,7 @@ numeric_to_char(PG_FUNCTION_ARGS) FormatNode *format; text *result; bool shouldFree; - int len = 0, - plen = 0, + int plen = 0, sign = 0; char *numstr, *orgnum, @@ -5008,16 +5009,15 @@ numeric_to_char(PG_FUNCTION_ARGS) numstr = (char *) palloc(strlen(orgnum) + 2); *numstr = ' '; strcpy(numstr + 1, orgnum); - len = strlen(numstr); } else { numstr = orgnum; - len = strlen(orgnum); } } else { + int len; Numeric val = value; if (IS_MULTI(&Num)) @@ -5084,8 +5084,7 @@ int4_to_char(PG_FUNCTION_ARGS) FormatNode *format; text *result; bool shouldFree; - int len = 0, - plen = 0, + int plen = 0, sign = 0; char *numstr, *orgnum; @@ -5111,11 +5110,12 @@ int4_to_char(PG_FUNCTION_ARGS) if (*orgnum == '+') *orgnum = ' '; - len = strlen(orgnum); numstr = orgnum; } else { + int len; + if (IS_MULTI(&Num)) { orgnum = DatumGetCString(DirectFunctionCall1(int4out, @@ -5175,8 +5175,7 @@ int8_to_char(PG_FUNCTION_ARGS) FormatNode *format; text *result; bool shouldFree; - int len = 0, - plen = 0, + int plen = 0, sign = 0; char *numstr, *orgnum; @@ -5211,16 +5210,16 @@ int8_to_char(PG_FUNCTION_ARGS) numstr = (char *) palloc(strlen(orgnum) + 2); *numstr = ' '; strcpy(numstr + 1, orgnum); - len = strlen(numstr); } else { numstr = orgnum; - len = strlen(orgnum); } } else { + int len; + if (IS_MULTI(&Num)) { double multi = pow((double) 10, (double) Num.multi); @@ -5282,8 +5281,7 @@ float4_to_char(PG_FUNCTION_ARGS) FormatNode *format; text *result; bool shouldFree; - int len = 0, - plen = 0, + int plen = 0, sign = 0; char *numstr, *orgnum, @@ -5317,13 +5315,13 @@ float4_to_char(PG_FUNCTION_ARGS) if (*orgnum == '+') *orgnum = ' '; - len = strlen(orgnum); numstr = orgnum; } } else { float4 val = value; + int len; if (IS_MULTI(&Num)) { @@ -5386,8 +5384,7 @@ float8_to_char(PG_FUNCTION_ARGS) FormatNode *format; text *result; bool shouldFree; - int len = 0, - plen = 0, + int plen = 0, sign = 0; char *numstr, *orgnum, @@ -5421,13 +5418,13 @@ float8_to_char(PG_FUNCTION_ARGS) if (*orgnum == '+') *orgnum = ' '; - len = strlen(orgnum); numstr = orgnum; } } else { float8 val = value; + int len; if (IS_MULTI(&Num)) {