Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rBOv7-00AjH0-D5 for pgsql-hackers@arkaria.postgresql.org; Fri, 08 Dec 2023 00:40:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rBOv6-0033Xl-2F for pgsql-hackers@arkaria.postgresql.org; Fri, 08 Dec 2023 00:40:44 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rBOv5-0033Xd-OU for pgsql-hackers@lists.postgresql.org; Fri, 08 Dec 2023 00:40:43 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rBOv3-009KS2-4S for pgsql-hackers@postgresql.org; Fri, 08 Dec 2023 00:40:42 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 3B80eUKp1340020; Thu, 7 Dec 2023 19:40:30 -0500 From: Tom Lane To: Jeff Davis cc: Nathan Bossart , Joe Conway , Andrew Dunstan , Davin Shearer , PostgreSQL-development Subject: Re: micro-optimizing json.c In-reply-to: <8d5cce64d7413eff04c5595a0a80d2b5a809e2fe.camel@j-davis.com> References: <20231207231251.GB3359478@nathanxps13> <8d5cce64d7413eff04c5595a0a80d2b5a809e2fe.camel@j-davis.com> Comments: In-reply-to Jeff Davis message dated "Thu, 07 Dec 2023 15:43:47 -0800" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1340018.1701996030.1@sss.pgh.pa.us> Date: Thu, 07 Dec 2023 19:40:30 -0500 Message-ID: <1340019.1701996030@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Jeff Davis 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