summaryrefslogtreecommitdiff
path: root/src/backend/utils/adt/jsonfuncs.c
diff options
context:
space:
mode:
authorDavid Rowley2025-11-06 01:59:48 +0000
committerDavid Rowley2025-11-06 01:59:48 +0000
commit6d0eba66275b125bf634bbdffda90c70856e3f93 (patch)
treee8457e698dcb49a6f9591e79373d8dfddd617f9e /src/backend/utils/adt/jsonfuncs.c
parentcf638b46aff2ccb8d4811e3b5d8a2c2410638190 (diff)
Use stack allocated StringInfoDatas, where possible
Various places that were using StringInfo but didn't need that StringInfo to exist beyond the scope of the function were using makeStringInfo(), which allocates both a StringInfoData and the buffer it uses as two separate allocations. It's more efficient for these cases to use a StringInfoData on the stack and initialize it with initStringInfo(), which only allocates the string buffer. This also simplifies the cleanup, in a few cases. Author: Mats Kindahl <mats.kindahl@gmail.com> Reviewed-by: David Rowley <dgrowleyml@gmail.com> Reviewed-by: Chao Li <li.evan.chao@gmail.com> Discussion: https://postgr.es/m/4379aac8-26f1-42f2-a356-ff0e886228d3@gmail.com
Diffstat (limited to 'src/backend/utils/adt/jsonfuncs.c')
-rw-r--r--src/backend/utils/adt/jsonfuncs.c18
1 files changed, 12 insertions, 6 deletions
diff --git a/src/backend/utils/adt/jsonfuncs.c b/src/backend/utils/adt/jsonfuncs.c
index 41862872e8a..8898f0f90a1 100644
--- a/src/backend/utils/adt/jsonfuncs.c
+++ b/src/backend/utils/adt/jsonfuncs.c
@@ -4507,14 +4507,16 @@ json_strip_nulls(PG_FUNCTION_ARGS)
text *json = PG_GETARG_TEXT_PP(0);
bool strip_in_arrays = PG_NARGS() == 2 ? PG_GETARG_BOOL(1) : false;
StripnullState *state;
+ StringInfoData strbuf;
JsonLexContext lex;
JsonSemAction *sem;
state = palloc0(sizeof(StripnullState));
sem = palloc0(sizeof(JsonSemAction));
+ initStringInfo(&strbuf);
state->lex = makeJsonLexContext(&lex, json, true);
- state->strval = makeStringInfo();
+ state->strval = &strbuf;
state->skip_next_null = false;
state->strip_in_arrays = strip_in_arrays;
@@ -4607,11 +4609,12 @@ Datum
jsonb_pretty(PG_FUNCTION_ARGS)
{
Jsonb *jb = PG_GETARG_JSONB_P(0);
- StringInfo str = makeStringInfo();
+ StringInfoData str;
- JsonbToCStringIndent(str, &jb->root, VARSIZE(jb));
+ initStringInfo(&str);
+ JsonbToCStringIndent(&str, &jb->root, VARSIZE(jb));
- PG_RETURN_TEXT_P(cstring_to_text_with_len(str->data, str->len));
+ PG_RETURN_TEXT_P(cstring_to_text_with_len(str.data, str.len));
}
/*
@@ -5846,7 +5849,7 @@ transform_jsonb_string_values(Jsonb *jsonb, void *action_state,
* Iterate over a json, and apply a specified JsonTransformStringValuesAction
* to every string value or element. Any necessary context for a
* JsonTransformStringValuesAction can be passed in the action_state variable.
- * Function returns a StringInfo, which is a copy of an original json with
+ * Function returns a Text Datum, which is a copy of an original json with
* transformed values.
*/
text *
@@ -5856,9 +5859,12 @@ transform_json_string_values(text *json, void *action_state,
JsonLexContext lex;
JsonSemAction *sem = palloc0(sizeof(JsonSemAction));
TransformJsonStringValuesState *state = palloc0(sizeof(TransformJsonStringValuesState));
+ StringInfoData strbuf;
+
+ initStringInfo(&strbuf);
state->lex = makeJsonLexContext(&lex, json, true);
- state->strval = makeStringInfo();
+ state->strval = &strbuf;
state->action = transform_action;
state->action_state = action_state;