Re: micro-optimizing json.c
David Rowley <dgrowleyml@gmail.com>
From: David Rowley <dgrowleyml@gmail.com>
To: Nathan Bossart <nathandbossart@gmail.com>
Cc: Joe Conway <mail@joeconway.com>, Tom Lane <tgl@sss.pgh.pa.us>, Andrew Dunstan <andrew@dunslane.net>, Davin Shearer <davin@apache.org>, PostgreSQL-development <pgsql-hackers@postgresql.org>
Date: 2023-12-08T03:11:52Z
Lists: 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
Commits
-
Micro-optimize datum_to_json_internal() some more.
- 0d1adae6f739 17.0 landed
-
Micro-optimize JSONTYPE_NUMERIC code path in json.c.
- dc3f9bc549d4 17.0 landed