Re: [PATCH] Optimize json_lex_string by batching character copying - Mailing list pgsql-hackers

From Andres Freund
Subject Re: [PATCH] Optimize json_lex_string by batching character copying
Date
Msg-id 20220706051801.gfbzjxgflbsglyt6@awork3.anarazel.de
Whole thread Raw
In response to Re: [PATCH] Optimize json_lex_string by batching character copying  (John Naylor <john.naylor@enterprisedb.com>)
Responses Re: [PATCH] Optimize json_lex_string by batching character copying
List 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



pgsql-hackers by date:

Previous
From: John Naylor
Date:
Subject: Re: [PATCH] Optimize json_lex_string by batching character copying
Next
From: Andres Freund
Date:
Subject: Re: AIX support - alignment issues