Re: [PATCH] Optimize json_lex_string by batching character copying
Andres Freund <andres@anarazel.de>
From: Andres Freund <andres@anarazel.de>
To: John Naylor <john.naylor@enterprisedb.com>
Cc: Jelte Fennema <Jelte.Fennema@microsoft.com>, "pgsql-hackers@postgresql.org" <pgsql-hackers@postgresql.org>, Merlin Moncure <mmoncure@gmail.com>, Tom Lane <tgl@sss.pgh.pa.us>, Andrew Dunstan <andrew.dunstan@2ndquadrant.com>, Stephen Frost <sfrost@snowman.net>
Date: 2022-07-06T05:18:01Z
Lists: pgsql-hackers
Hi,
On 2022-07-06 12:10:20 +0700, John Naylor wrote:
> diff --git a/src/common/jsonapi.c b/src/common/jsonapi.c
> index eeedc0645a..ad4858c623 100644
> --- a/src/common/jsonapi.c
> +++ b/src/common/jsonapi.c
> @@ -851,10 +851,26 @@ json_lex_string(JsonLexContext *lex)
> }
> else if (lex->strval != NULL)
> {
> + /* start lookahead at next byte */
> + char *p = s + 1;
> +
> if (hi_surrogate != -1)
> return JSON_UNICODE_LOW_SURROGATE;
>
> - appendStringInfoChar(lex->strval, *s);
> + while (p < end)
> + {
> + if (*p == '\\' || *p == '"' || (unsigned char) *p < 32)
> + break;
> + p++;
> + }
> +
> + appendBinaryStringInfo(lex->strval, s, p - s);
> +
> + /*
> + * s will be incremented at the top of the loop, so set it to just
> + * behind our lookahead position
> + */
> + s = p - 1;
> }
> }
>
> --
> 2.36.1
I think before committing something along those lines we should make the
relevant bits also be applicable when ->strval is NULL, as several functions
use that (notably json_in IIRC). Afaics we'd just need to move the strval
check to be around the appendBinaryStringInfo(). And it should simplify the
function, because some of the relevant code is duplicated outside as well...
Greetings,
Andres Freund
Commits
-
Speed up lexing of long JSON strings
- 0a8de93a48ce 16.0 landed
-
Add optimized functions for linear search within byte arrays
- e813e0e16852 16.0 landed
-
Build de-escaped JSON strings in larger chunks during lexing
- 3838fa269c15 16.0 landed
-
Simplify json lexing state
- 3de359f18f2b 16.0 landed