Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g7kFN-0005rM-Ox for pgsql-hackers@arkaria.postgresql.org; Wed, 03 Oct 2018 16:43:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1g7kFL-0007zc-Ke for pgsql-hackers@arkaria.postgresql.org; Wed, 03 Oct 2018 16:43:35 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g7kFL-0007zV-9q for pgsql-hackers@lists.postgresql.org; Wed, 03 Oct 2018 16:43:35 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g7kFI-0005wN-Pf for pgsql-hackers@lists.postgresql.org; Wed, 03 Oct 2018 16:43:34 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id w93GhQfB027859; Wed, 3 Oct 2018 12:43:26 -0400 From: Tom Lane To: Andres Freund cc: pgsql-hackers@lists.postgresql.org, Thomas Munro , Andrew Gierth , Alexander Kuzmenkov Subject: Re: Performance improvements for src/port/snprintf.c In-reply-to: <20181003163246.r3cr6kxt3gsl5hfc@alap3.anarazel.de> References: <14302.1538005507@sss.pgh.pa.us> <20180926235821.zmpxqv66ob7v2ffi@alap3.anarazel.de> <15882.1538007944@sss.pgh.pa.us> <20180927004022.lfhffbj3roxzf5ca@alap3.anarazel.de> <20180927005705.shbl3n5hq3t65n2j@alap3.anarazel.de> <87r2hffwda.fsf@news-spur.riddles.org.uk> <6FEC34CD-B8C0-44E2-8D1F-2320568BDC78@anarazel.de> <30551.1538517271@sss.pgh.pa.us> <26193.1538582367@sss.pgh.pa.us> <20181003163246.r3cr6kxt3gsl5hfc@alap3.anarazel.de> Comments: In-reply-to Andres Freund message dated "Wed, 03 Oct 2018 09:32:46 -0700" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <27857.1538585006.1@sss.pgh.pa.us> Date: Wed, 03 Oct 2018 12:43:26 -0400 Message-ID: <27858.1538585006@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Andres Freund writes: > On 2018-10-03 11:59:27 -0400, Tom Lane wrote: >> I experimented with adding an initial check for "format is exactly %s" >> at the top of dopr(), and couldn't get excited about that. Instrumenting >> things showed that the optimization fired in only 1.8% of the calls >> during a run of our core regression tests. Now, that might not count >> as a really representative workload, but it doesn't make me think that >> the case is worth optimizing for us. > Seems right. I also have a hard time to believe that any of those "%s" > printfs are performance critical - we'd hopefully just have avoided the > sprintf in that case. Yup, that's probably a good chunk of the reason why there aren't very many. But we *do* use %s a lot to assemble multiple strings or combine them with fixed text, which is why the other form of the optimization is useful. >> But then it occurred to me that there's more than one way to skin this >> cat. We could, for an even cheaper extra test, detect that any one >> format specifier is just "%s", and use the same kind of fast-path >> within the loop. With the same sort of instrumentation, I found that >> a full 45% of the format specs executed in the core regression tests >> are just %s. That makes me think that a patch along the lines of the >> attached is a good win for our use-cases. Comparing to Fedora 28's >> glibc, this gets us to > Hm, especially if we special case the float->string conversions directly > at the hot callsites, that seems reasonable. I kinda wish we could just > easily move the format string processing to compile-time, but given > translatability that won't be widely possible even if it were otherwise > feasible. Yeah, there's a mighty big pile of infrastructure that depends on the way *printf works. I agree that one way or another we're going to be special-casing float8out and float4out. BTW, I poked around in the related glibc sources the other day, and it seemed like they are doing some sort of quasi-compilation of format strings. I couldn't figure out how they made it pay, though --- without some way to avoid re-compiling the same format string over and over, seems like it couldn't net out as a win. But if they are avoiding that, I didn't find where. regards, tom lane