]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Micro-optimize datum_to_json_internal() some more.
authorNathan Bossart <nathan@postgresql.org>
Mon, 18 Dec 2023 16:34:33 +0000 (10:34 -0600)
committerNathan Bossart <nathan@postgresql.org>
Mon, 18 Dec 2023 16:34:33 +0000 (10:34 -0600)
Commit dc3f9bc549 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

src/backend/utils/adt/json.c

index 0778d8dec927e7d16e57530d9a2fcadf7d4051d9..507c1c22b71aaa855d204dc76d918507c4231e60 100644 (file)
@@ -188,7 +188,7 @@ datum_to_json_internal(Datum val, bool is_null, StringInfo result,
 
        if (is_null)
        {
-               appendStringInfoString(result, "null");
+               appendBinaryStringInfo(result, "null", strlen("null"));
                return;
        }
 
@@ -210,11 +210,14 @@ datum_to_json_internal(Datum val, bool is_null, StringInfo result,
                        composite_to_json(val, result, false);
                        break;
                case JSONTYPE_BOOL:
-                       outputstr = DatumGetBool(val) ? "true" : "false";
                        if (key_scalar)
-                               escape_json(result, outputstr);
+                               appendStringInfoChar(result, '"');
+                       if (DatumGetBool(val))
+                               appendBinaryStringInfo(result, "true", strlen("true"));
                        else
-                               appendStringInfoString(result, outputstr);
+                               appendBinaryStringInfo(result, "false", strlen("false"));
+                       if (key_scalar)
+                               appendStringInfoChar(result, '"');
                        break;
                case JSONTYPE_NUMERIC:
                        outputstr = OidOutputFunctionCall(outfuncoid, val);
@@ -277,9 +280,8 @@ datum_to_json_internal(Datum val, bool is_null, StringInfo result,
                case JSONTYPE_CAST:
                        /* outfuncoid refers to a cast function, not an output function */
                        jsontext = DatumGetTextPP(OidFunctionCall1(outfuncoid, val));
-                       outputstr = text_to_cstring(jsontext);
-                       appendStringInfoString(result, outputstr);
-                       pfree(outputstr);
+                       appendBinaryStringInfo(result, VARDATA_ANY(jsontext),
+                                                                  VARSIZE_ANY_EXHDR(jsontext));
                        pfree(jsontext);
                        break;
                default: