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 1fyLsw-00043h-W7 for pgsql-hackers@arkaria.postgresql.org; Fri, 07 Sep 2018 18:53:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fyLsv-0008AV-Gs for pgsql-hackers@arkaria.postgresql.org; Fri, 07 Sep 2018 18:53:37 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fyLsv-0008AN-5p for pgsql-hackers@lists.postgresql.org; Fri, 07 Sep 2018 18:53:37 +0000 Received: from mail.postgrespro.ru ([93.174.131.138]) by magus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fyLsn-00025T-ON for pgsql-hackers@lists.postgresql.org; Fri, 07 Sep 2018 18:53:36 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.postgrespro.ru (Postfix) with ESMTP id 7887A21CA0F9; Fri, 7 Sep 2018 21:53:28 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at postgrespro.ru X-Spam-Flag: NO X-Spam-Score: 0 X-Spam-Level: X-Spam-Status: No, score=x tagged_above=-99 required=4 WHITELISTED tests=[] autolearn=unavailable Received: from [192.168.27.132] (gw.postgrespro.ru [93.174.131.141]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) by mail.postgrespro.ru (Postfix) with ESMTPSA id 4347521CA0EF; Fri, 7 Sep 2018 21:53:28 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1536346408; bh=cH4WvBvij7mPOeRWrX5PaCJ3l4t6LXm119gVNOJOYjM=; h=Subject:To:References:From:Date:In-Reply-To; b=a8+EzoJotJBeIrxBpVwX+6dZzepyWhk9bVoINp/b3CRdvwIuBq/8vNeBSwFwtce8w BpzRRIfOO05TyrCtXh1anGYFwEqkQ7sFhj3XJPMlXYINNGF4PoS5MMmJjlTW1mJTdf vGLayFdqUpHmn9cRoWMOkB4pVaeCWw+kMgJwa+kU= Subject: Re: Performance improvements for src/port/snprintf.c To: Tom Lane , pgsql-hackers@lists.postgresql.org References: <11787.1534530779@sss.pgh.pa.us> <2238.1534716465@sss.pgh.pa.us> From: Alexander Kuzmenkov Message-ID: Date: Fri, 7 Sep 2018 21:53:27 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 MIME-Version: 1.0 In-Reply-To: <2238.1534716465@sss.pgh.pa.us> Content-Type: multipart/alternative; boundary="------------1946F3DCF4ACDC986CAD92F9" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------1946F3DCF4ACDC986CAD92F9 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit I benchmarked this, using your testbed and comparing to libc sprintf (Ubuntu GLIBC 2.27-0ubuntu3) and another implementation I know [1], all compiled with gcc 5.4.0 with -O2. I used bigger decimals in one of the formats, but otherwise they are the same as yours. Here is the table of conversion time relative to libc: format                                 pg      stb ("%2$.*3$f %1$d\n", 42, 123.456, 2)    1.03    - ("%.*g", 15, 123.456)                  1.08    0.31 ("%10d", 15)                           0.63    0.52 ("%s", "012345678900123456789001234    2.06    6.20 ("%d 012345678900123456789001234567    2.03    1.81 ("%1$d 0123456789001234567890012345    1.34    - ("%d %d", 845879348, 994502893)        1.97    0.59 Surprisingly, our implementation is twice faster than libc on "%10d". Stb is faster than we are with floats, but it uses its own algorithm for that. It is also faster with decimals, probably because it uses a two-digit lookup table, not one-digit like we do. Unfortunately it doesn't support dollars. 1. https://github.com/nothings/stb/blob/master/stb_sprintf.h -- Alexander Kuzmenkov Postgres Professional: http://www.postgrespro.com The Russian Postgres Company --------------1946F3DCF4ACDC986CAD92F9 Content-Type: text/html; charset=windows-1252 Content-Transfer-Encoding: 8bit

I benchmarked this, using your testbed and comparing to libc sprintf (Ubuntu GLIBC 2.27-0ubuntu3) and another implementation I know [1], all compiled with gcc 5.4.0 with -O2. I used bigger decimals in one of the formats, but otherwise they are the same as yours. Here is the table of conversion time relative to libc:

format                                 pg      stb
("%2$.*3$f %1$d\n", 42, 123.456, 2)    1.03    -
("%.*g", 15, 123.456)                  1.08    0.31
("%10d", 15)                           0.63    0.52
("%s", "012345678900123456789001234    2.06    6.20
("%d 012345678900123456789001234567    2.03    1.81
("%1$d 0123456789001234567890012345    1.34    -
("%d %d", 845879348, 994502893)        1.97    0.59

Surprisingly, our implementation is twice faster than libc on "%10d". Stb is faster than we are with floats, but it uses its own algorithm for that. It is also faster with decimals, probably because it uses a two-digit lookup table, not one-digit like we do. Unfortunately it doesn't support dollars.

1. https://github.com/nothings/stb/blob/master/stb_sprintf.h

-- 
Alexander Kuzmenkov
Postgres Professional: http://www.postgrespro.com
The Russian Postgres Company
--------------1946F3DCF4ACDC986CAD92F9--