Commit d3c72e23 authored by Heikki Linnakangas's avatar Heikki Linnakangas

Avoid some pnstrdup()s when constructing jsonb

This speeds up text to jsonb parsing and hstore to jsonb conversions
somewhat.
parent 14d309cc
...@@ -1386,7 +1386,7 @@ hstore_to_jsonb(PG_FUNCTION_ARGS) ...@@ -1386,7 +1386,7 @@ hstore_to_jsonb(PG_FUNCTION_ARGS)
key.type = jbvString; key.type = jbvString;
key.val.string.len = HS_KEYLEN(entries, i); key.val.string.len = HS_KEYLEN(entries, i);
key.val.string.val = pnstrdup(HS_KEY(entries, base, i), key.val.string.len); key.val.string.val = HS_KEY(entries, base, i);
res = pushJsonbValue(&state, WJB_KEY, &key); res = pushJsonbValue(&state, WJB_KEY, &key);
...@@ -1398,7 +1398,7 @@ hstore_to_jsonb(PG_FUNCTION_ARGS) ...@@ -1398,7 +1398,7 @@ hstore_to_jsonb(PG_FUNCTION_ARGS)
{ {
val.type = jbvString; val.type = jbvString;
val.val.string.len = HS_VALLEN(entries, i); val.val.string.len = HS_VALLEN(entries, i);
val.val.string.val = pnstrdup(HS_VAL(entries, base, i), val.val.string.len); val.val.string.val = HS_VAL(entries, base, i);
} }
res = pushJsonbValue(&state, WJB_VALUE, &val); res = pushJsonbValue(&state, WJB_VALUE, &val);
} }
...@@ -1433,7 +1433,7 @@ hstore_to_jsonb_loose(PG_FUNCTION_ARGS) ...@@ -1433,7 +1433,7 @@ hstore_to_jsonb_loose(PG_FUNCTION_ARGS)
key.type = jbvString; key.type = jbvString;
key.val.string.len = HS_KEYLEN(entries, i); key.val.string.len = HS_KEYLEN(entries, i);
key.val.string.val = pnstrdup(HS_KEY(entries, base, i), key.val.string.len); key.val.string.val = HS_KEY(entries, base, i);
res = pushJsonbValue(&state, WJB_KEY, &key); res = pushJsonbValue(&state, WJB_KEY, &key);
...@@ -1507,7 +1507,7 @@ hstore_to_jsonb_loose(PG_FUNCTION_ARGS) ...@@ -1507,7 +1507,7 @@ hstore_to_jsonb_loose(PG_FUNCTION_ARGS)
{ {
val.type = jbvString; val.type = jbvString;
val.val.string.len = HS_VALLEN(entries, i); val.val.string.len = HS_VALLEN(entries, i);
val.val.string.val = pnstrdup(HS_VAL(entries, base, i), val.val.string.len); val.val.string.val = HS_VAL(entries, base, i);
} }
} }
res = pushJsonbValue(&state, WJB_VALUE, &val); res = pushJsonbValue(&state, WJB_VALUE, &val);
......
...@@ -381,9 +381,6 @@ parse_object_field(JsonLexContext *lex, JsonSemAction *sem) ...@@ -381,9 +381,6 @@ parse_object_field(JsonLexContext *lex, JsonSemAction *sem)
if (oend != NULL) if (oend != NULL)
(*oend) (sem->semstate, fname, isnull); (*oend) (sem->semstate, fname, isnull);
if (fname != NULL)
pfree(fname);
} }
static void static void
......
...@@ -247,7 +247,7 @@ jsonb_in_object_field_start(void *pstate, char *fname, bool isnull) ...@@ -247,7 +247,7 @@ jsonb_in_object_field_start(void *pstate, char *fname, bool isnull)
Assert(fname != NULL); Assert(fname != NULL);
v.type = jbvString; v.type = jbvString;
v.val.string.len = checkStringLen(strlen(fname)); v.val.string.len = checkStringLen(strlen(fname));
v.val.string.val = pnstrdup(fname, v.val.string.len); v.val.string.val = fname;
_state->res = pushJsonbValue(&_state->parseState, WJB_KEY, &v); _state->res = pushJsonbValue(&_state->parseState, WJB_KEY, &v);
} }
...@@ -295,7 +295,7 @@ jsonb_in_scalar(void *pstate, char *token, JsonTokenType tokentype) ...@@ -295,7 +295,7 @@ jsonb_in_scalar(void *pstate, char *token, JsonTokenType tokentype)
Assert(token != NULL); Assert(token != NULL);
v.type = jbvString; v.type = jbvString;
v.val.string.len = checkStringLen(strlen(token)); v.val.string.len = checkStringLen(strlen(token));
v.val.string.val = pnstrdup(token, v.val.string.len); v.val.string.val = token;
break; break;
case JSON_TOKEN_NUMBER: case JSON_TOKEN_NUMBER:
......
...@@ -73,6 +73,10 @@ typedef void (*json_scalar_action) (void *state, char *token, JsonTokenType toke ...@@ -73,6 +73,10 @@ typedef void (*json_scalar_action) (void *state, char *token, JsonTokenType toke
* point, Likewise, semstate can be NULL. Using an all-NULL structure amounts * point, Likewise, semstate can be NULL. Using an all-NULL structure amounts
* to doing a pure parse with no side-effects, and is therefore exactly * to doing a pure parse with no side-effects, and is therefore exactly
* what the json input routines do. * what the json input routines do.
*
* The 'fname' and 'token' strings passed to these actions are palloc'd.
* They are not free'd or used further by the parser, so the action function
* is free to do what it wishes with them.
*/ */
typedef struct JsonSemAction typedef struct JsonSemAction
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment