Shave a few cycles off jsonb parsing.
authorAndrew Dunstan <[email protected]>
Mon, 17 Mar 2014 14:43:02 +0000 (10:43 -0400)
committerAndrew Dunstan <[email protected]>
Mon, 17 Mar 2014 14:43:02 +0000 (10:43 -0400)
Field names and string values can't be NULL, although they might be
empty, so don't waste time checking got see if they are NULL - the
parser will have seen to that already.

src/backend/utils/adt/jsonb.c

index fbe949370c8c3e3db268746f64dc158798762a9a..f4cf1145d08157b1bf3296dc16cea81a52ba3729 100644 (file)
@@ -86,7 +86,7 @@ jsonb_out(PG_FUNCTION_ARGS)
 /*
  * jsonb type send function
  *
- * Just send jsonb as a string of text
+ * Just send jsonb as a version number, then a string of text
  */
 Datum
 jsonb_send(PG_FUNCTION_ARGS)
@@ -244,9 +244,10 @@ jsonb_in_object_field_start(void *state, char *fname, bool isnull)
    JsonbInState *_state = (JsonbInState *) state;
    JsonbValue  v;
 
+   Assert (fname != NULL);
    v.type = jbvString;
-   v.string.len = fname ? checkStringLen(strlen(fname)) : 0;
-   v.string.val = fname ? pnstrdup(fname, v.string.len) : NULL;
+   v.string.len = checkStringLen(strlen(fname));
+   v.string.val = pnstrdup(fname, v.string.len);
    v.estSize = sizeof(JEntry) + v.string.len;
 
    _state->res = pushJsonbValue(&_state->state, WJB_KEY, &v);
@@ -294,9 +295,10 @@ jsonb_in_scalar(void *state, char *token, JsonTokenType tokentype)
    {
 
        case JSON_TOKEN_STRING:
+           Assert (token != NULL);
            v.type = jbvString;
-           v.string.len = token ? checkStringLen(strlen(token)) : 0;
-           v.string.val = token ? pnstrdup(token, v.string.len) : NULL;
+           v.string.len = checkStringLen(strlen(token));
+           v.string.val = pnstrdup(token, v.string.len);
            v.estSize += v.string.len;
            break;
        case JSON_TOKEN_NUMBER:
@@ -305,6 +307,7 @@ jsonb_in_scalar(void *state, char *token, JsonTokenType tokentype)
             * size is well in excess of the restriction we separately impose
             * of the size of JsonbValues
             */
+           Assert (token != NULL);
            v.type = jbvNumeric;
            v.numeric = DatumGetNumeric(DirectFunctionCall3(numeric_in, CStringGetDatum(token), 0, -1));
            v.estSize += VARSIZE_ANY(v.numeric) + sizeof(JEntry) /* alignment */ ;
@@ -321,6 +324,7 @@ jsonb_in_scalar(void *state, char *token, JsonTokenType tokentype)
            v.type = jbvNull;
            break;
        default:
+           /* should not be possible */
            elog(ERROR, "invalid json token type");
            break;
    }