Re: micro-optimizing json.c - Mailing list pgsql-hackers

From David Rowley
Subject Re: micro-optimizing json.c
Date
Msg-id CAApHDvpjoRMrafos6N-Nkja+XSVE_mts7KY85=oy9kwZz-92=g@mail.gmail.com
Whole thread Raw
In response to micro-optimizing json.c  (Nathan Bossart <nathandbossart@gmail.com>)
Responses Re: micro-optimizing json.c
Re: micro-optimizing json.c
List pgsql-hackers
On Fri, 8 Dec 2023 at 12:13, Nathan Bossart <nathandbossart@gmail.com> wrote:
> Here's a patch that removes a couple of strlen() calls that showed up
> prominently in perf for a COPY TO (FORMAT json) on 110M integers.  On my
> laptop, I see a 20% speedup from ~23.6s to ~18.9s for this test.

+ seplen = use_line_feeds ? sizeof(",\n ") - 1 : sizeof(",") - 1;

Most modern compilers will be fine with just:

seplen = strlen(sep);

I had to go back to clang 3.4.1 and GCC 4.1.2 to see the strlen() call
with that code [1].

With:

  if (needsep)
- appendStringInfoString(result, sep);
+ appendBinaryStringInfo(result, sep, seplen);

I might be neater to get rid of the if condition and have:

sep = use_line_feeds ? ",\n " : ",";
seplen = strlen(sep);
slen = 0;

...
for (int i = 0; i < tupdesc->natts; i++)
{
    ...
    appendBinaryStringInfo(result, sep, slen);
    slen = seplen;
    ...
}

David

[1] https://godbolt.org/z/8dq8a88bP



pgsql-hackers by date:

Previous
From: shveta malik
Date:
Subject: Re: Synchronizing slots from primary to standby
Next
From: Thomas Munro
Date:
Subject: Re: UBSan pointer overflow in xlogreader.c