Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d3c72e2

Browse files
committedMay 9, 2014
Avoid some pnstrdup()s when constructing jsonb
This speeds up text to jsonb parsing and hstore to jsonb conversions somewhat.
1 parent 14d309c commit d3c72e2

File tree

4 files changed

+10
-9
lines changed

4 files changed

+10
-9
lines changed
 

‎contrib/hstore/hstore_io.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,7 +1386,7 @@ hstore_to_jsonb(PG_FUNCTION_ARGS)
13861386

13871387
key.type = jbvString;
13881388
key.val.string.len = HS_KEYLEN(entries, i);
1389-
key.val.string.val = pnstrdup(HS_KEY(entries, base, i), key.val.string.len);
1389+
key.val.string.val = HS_KEY(entries, base, i);
13901390

13911391
res = pushJsonbValue(&state, WJB_KEY, &key);
13921392

@@ -1398,7 +1398,7 @@ hstore_to_jsonb(PG_FUNCTION_ARGS)
13981398
{
13991399
val.type = jbvString;
14001400
val.val.string.len = HS_VALLEN(entries, i);
1401-
val.val.string.val = pnstrdup(HS_VAL(entries, base, i), val.val.string.len);
1401+
val.val.string.val = HS_VAL(entries, base, i);
14021402
}
14031403
res = pushJsonbValue(&state, WJB_VALUE, &val);
14041404
}
@@ -1433,7 +1433,7 @@ hstore_to_jsonb_loose(PG_FUNCTION_ARGS)
14331433

14341434
key.type = jbvString;
14351435
key.val.string.len = HS_KEYLEN(entries, i);
1436-
key.val.string.val = pnstrdup(HS_KEY(entries, base, i), key.val.string.len);
1436+
key.val.string.val = HS_KEY(entries, base, i);
14371437

14381438
res = pushJsonbValue(&state, WJB_KEY, &key);
14391439

@@ -1507,7 +1507,7 @@ hstore_to_jsonb_loose(PG_FUNCTION_ARGS)
15071507
{
15081508
val.type = jbvString;
15091509
val.val.string.len = HS_VALLEN(entries, i);
1510-
val.val.string.val = pnstrdup(HS_VAL(entries, base, i), val.val.string.len);
1510+
val.val.string.val = HS_VAL(entries, base, i);
15111511
}
15121512
}
15131513
res = pushJsonbValue(&state, WJB_VALUE, &val);

‎src/backend/utils/adt/json.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,9 +381,6 @@ parse_object_field(JsonLexContext *lex, JsonSemAction *sem)
381381

382382
if (oend != NULL)
383383
(*oend) (sem->semstate, fname, isnull);
384-
385-
if (fname != NULL)
386-
pfree(fname);
387384
}
388385

389386
static void

‎src/backend/utils/adt/jsonb.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ jsonb_in_object_field_start(void *pstate, char *fname, bool isnull)
247247
Assert(fname != NULL);
248248
v.type = jbvString;
249249
v.val.string.len = checkStringLen(strlen(fname));
250-
v.val.string.val = pnstrdup(fname, v.val.string.len);
250+
v.val.string.val = fname;
251251

252252
_state->res = pushJsonbValue(&_state->parseState, WJB_KEY, &v);
253253
}
@@ -295,7 +295,7 @@ jsonb_in_scalar(void *pstate, char *token, JsonTokenType tokentype)
295295
Assert(token != NULL);
296296
v.type = jbvString;
297297
v.val.string.len = checkStringLen(strlen(token));
298-
v.val.string.val = pnstrdup(token, v.val.string.len);
298+
v.val.string.val = token;
299299
break;
300300
case JSON_TOKEN_NUMBER:
301301

‎src/include/utils/jsonapi.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ typedef void (*json_scalar_action) (void *state, char *token, JsonTokenType toke
7373
* point, Likewise, semstate can be NULL. Using an all-NULL structure amounts
7474
* to doing a pure parse with no side-effects, and is therefore exactly
7575
* what the json input routines do.
76+
*
77+
* The 'fname' and 'token' strings passed to these actions are palloc'd.
78+
* They are not free'd or used further by the parser, so the action function
79+
* is free to do what it wishes with them.
7680
*/
7781
typedef struct JsonSemAction
7882
{

0 commit comments

Comments
 (0)
Please sign in to comment.