Re: micro-optimizing json.c

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Jeff Davis <pgsql(at)j-davis(dot)com>
Cc: Nathan Bossart <nathandbossart(at)gmail(dot)com>, Joe Conway <mail(at)joeconway(dot)com>, Andrew Dunstan <andrew(at)dunslane(dot)net>, Davin Shearer <davin(at)apache(dot)org>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: micro-optimizing json.c
Date: 2023-12-08 00:40:30
Message-ID: 1340019.1701996030@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Jeff Davis <pgsql(at)j-davis(dot)com> writes:
> Nice improvement. The use of (len = ...) in a conditional is slightly
> out of the ordinary, but it makes the conditionals a bit simpler and
> you have a comment, so it's fine with me.

Yeah. It's a little not per project style, but I don't see a nice
way to do it differently, granting that we don't want to put the
strlen() ahead of the !key_scalar test. More straightforward
coding would end up with two else-path calls of escape_json,
which doesn't seem all that much more straightforward.

> I wonder, if there were an efficient cast from numeric to text, then
> perhaps you could avoid the strlen() entirely?

Hmm ... I think that might not be the way to think about it. What
I'm wondering is why we need a test as expensive as IsValidJsonNumber
in the first place, given that we know this is a numeric data type's
output. ISTM we only need to reject "Inf"/"-Inf" and "NaN", which
surely does not require a full parse. Skip over a sign, check for
"I"/"N", and you're done.

... and for that matter, why does quoting of Inf/NaN require
that we apply something as expensive as escape_json? Several other
paths in this switch have no hesitation about assuming that they
can just plaster double quotes around what was emitted. How is
that safe for timestamps but not Inf/NaN?

> In any case, +1 to your simple change.

If we end up not using IsValidJsonNumber then this strlen hackery
would become irrelevant, so maybe that idea should be looked at first.

regards, tom lane

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Bruce Momjian 2023-12-08 01:06:42 Re: [HACKERS] pg_upgrade vs vacuum_cost_delay
Previous Message Jeff Davis 2023-12-08 00:34:57 Re: Improve WALRead() to suck data directly from WAL buffers when possible