pgsql: Micro-optimize JSONTYPE_NUMERIC code path in json.c.

From: Nathan Bossart <nathan(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Micro-optimize JSONTYPE_NUMERIC code path in json.c.
Date: 2023-12-08 19:41:40
Message-ID: E1rBgjE-009FKn-Cp@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Micro-optimize JSONTYPE_NUMERIC code path in json.c.

This commit does the following:

* In datum_to_json_internal(), the call to IsValidJsonNumber() is
replaced with simplified validation code. This avoids an extra
call to strlen() in this path, and it avoids validating the
entire string (which is okay since we know we're dealing with a
numeric data type's output).

* In datum_to_json_internal(), the call to escape_json() in the
JSONTYPE_NUMERIC path is replaced with code that just surrounds
the string with quotes. In passing, some other nearby calls to
appendStringInfo() have been replaced with similar code to avoid
unnecessary calls to vsnprintf().

* In composite_to_json(), the length of the separator is now
determined at compile time to avoid unnecessary calls to
strlen().

On my machine, this speeds up a benchmark for the proposed COPY TO
(FORMAT json) command with many integers by upwards of 20%. There
are likely other code paths that could be given a similar
treatment, but that is left as a future exercise.

Reviewed-by: Jeff Davis, Tom Lane, David Rowley, John Naylor
Discussion: https://postgr.es/m/20231207231251.GB3359478%40nathanxps13

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/dc3f9bc549d4cc8cb1d4030bdc94e788d70fdcfe

Modified Files
--------------
src/backend/utils/adt/json.c | 37 +++++++++++++++++++++++++++++--------
1 file changed, 29 insertions(+), 8 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Peter Geoghegan 2023-12-08 23:38:09 pgsql: Fix nbtree backward scan race condition comments.
Previous Message Jeff Davis 2023-12-08 19:32:40 pgsql: Cache opaque handle for GUC option to avoid repeasted lookups.