Skip to content

Commit 0d1adae

Browse files
Micro-optimize datum_to_json_internal() some more.
Commit dc3f9bc mainly targeted the JSONTYPE_NUMERIC code path. This commit applies similar optimizations (e.g., removing unnecessary runtime calls to strlen() and palloc()) to nearby code. Reviewed-by: Tom Lane Discussion: https://postgr.es/m/20231208203708.GA4126315%40nathanxps13
1 parent 4908c58 commit 0d1adae

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/backend/utils/adt/json.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ datum_to_json_internal(Datum val, bool is_null, StringInfo result,
188188

189189
if (is_null)
190190
{
191-
appendStringInfoString(result, "null");
191+
appendBinaryStringInfo(result, "null", strlen("null"));
192192
return;
193193
}
194194

@@ -210,11 +210,14 @@ datum_to_json_internal(Datum val, bool is_null, StringInfo result,
210210
composite_to_json(val, result, false);
211211
break;
212212
case JSONTYPE_BOOL:
213-
outputstr = DatumGetBool(val) ? "true" : "false";
214213
if (key_scalar)
215-
escape_json(result, outputstr);
214+
appendStringInfoChar(result, '"');
215+
if (DatumGetBool(val))
216+
appendBinaryStringInfo(result, "true", strlen("true"));
216217
else
217-
appendStringInfoString(result, outputstr);
218+
appendBinaryStringInfo(result, "false", strlen("false"));
219+
if (key_scalar)
220+
appendStringInfoChar(result, '"');
218221
break;
219222
case JSONTYPE_NUMERIC:
220223
outputstr = OidOutputFunctionCall(outfuncoid, val);
@@ -277,9 +280,8 @@ datum_to_json_internal(Datum val, bool is_null, StringInfo result,
277280
case JSONTYPE_CAST:
278281
/* outfuncoid refers to a cast function, not an output function */
279282
jsontext = DatumGetTextPP(OidFunctionCall1(outfuncoid, val));
280-
outputstr = text_to_cstring(jsontext);
281-
appendStringInfoString(result, outputstr);
282-
pfree(outputstr);
283+
appendBinaryStringInfo(result, VARDATA_ANY(jsontext),
284+
VARSIZE_ANY_EXHDR(jsontext));
283285
pfree(jsontext);
284286
break;
285287
default:

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy