From: Tom Lane <tgl@sss.pgh.pa.us>
To: Andres Freund <andres@anarazel.de>
Cc: pgsql-hackers@lists.postgresql.org, Thomas Munro <thomas.munro@enterprisedb.com>
Cc: Andrew Gierth <andrew@tao11.riddles.org.uk>
Cc: Alexander Kuzmenkov <a.kuzmenkov@postgrespro.ru>
Subject: Re: Performance improvements for src/port/snprintf.c
Date: Wed, 03 Oct 2018 11:59:27 -0400
Message-ID: <26193.1538582367@sss.pgh.pa.us> (raw)
In-Reply-To: <30551.1538517271@sss.pgh.pa.us>
References: <11787.1534530779@sss.pgh.pa.us>
<2238.1534716465@sss.pgh.pa.us>
<f5dd41da-911c-fd97-1b78-e0f8c0141897@postgrespro.ru>
<15785.1536776055@sss.pgh.pa.us>
<20180926220419.lorxpb564gakdjkz@alap3.anarazel.de>
<20180926221814.el5nwmkez6zfmzbz@alap3.anarazel.de>
<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>
<CAEepm=1-3AG2O25VNnLXsjCM3EHBLW=jyJtpp_AMGG6a-GRJZA@mail.gmail.com>
<6FEC34CD-B8C0-44E2-8D1F-2320568BDC78@anarazel.de>
<30551.1538517271@sss.pgh.pa.us>
I wrote:
> ... However, I did add recent glibc (Fedora 28)
> to the mix, and I was interested to discover that they seem to have
> added a fast-path for format strings that are exactly "%s", just as
> NetBSD did. I wonder if we should reconsider our position on doing
> that. It'd be a simple enough addition...
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.
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
Test case: %s
snprintf time = 8.83615 ms total, 8.83615e-06 ms per iteration
pg_snprintf time = 23.9372 ms total, 2.39372e-05 ms per iteration
ratio = 2.709
Test case: %sx
snprintf time = 59.4481 ms total, 5.94481e-05 ms per iteration
pg_snprintf time = 29.8983 ms total, 2.98983e-05 ms per iteration
ratio = 0.503
versus what we have as of this morning's commit:
Test case: %s
snprintf time = 7.7427 ms total, 7.7427e-06 ms per iteration
pg_snprintf time = 26.2439 ms total, 2.62439e-05 ms per iteration
ratio = 3.390
Test case: %sx
snprintf time = 61.4452 ms total, 6.14452e-05 ms per iteration
pg_snprintf time = 32.7516 ms total, 3.27516e-05 ms per iteration
ratio = 0.533
The penalty for non-%s cases seems to be a percent or so, although
it's barely above the noise floor in my tests.
regards, tom lane
Attachments:make-plain-percent-s-faster.patch (text/x-diff, 715B) downloaddiff --git a/src/port/snprintf.c b/src/port/snprintf.cindex cad7345..b9b6add 100644
*** a/src/port/snprintf.c
--- b/src/port/snprintf.c
*************** dopr(PrintfTarget *target, const char *f
*** 431,436 ****
--- 431,449 ----
/* Process conversion spec starting at *format */
format++;
+ + /* Fast path for conversion spec that is exactly %s */+ if (*format == 's')+ {+ format++;+ strvalue = va_arg(args, char *);+ Assert(strvalue != NULL);+ dostr(strvalue, strlen(strvalue), target);+ if (target->failed)+ break;+ continue;+ }+
fieldwidth = precision = zpad = leftjust = forcesign = 0;
longflag = longlongflag = pointflag = 0;
fmtpos = accum = 0;
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-hackers@postgresql.org
Cc: tgl@sss.pgh.pa.us, andres@anarazel.de, thomas.munro@enterprisedb.com, andrew@tao11.riddles.org.uk, a.kuzmenkov@postgrespro.ru
Subject: Re: Performance improvements for src/port/snprintf.c
In-Reply-To: <26193.1538582367@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox