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 1sPm1o-006HBA-8T for pgsql-hackers@arkaria.postgresql.org; Fri, 05 Jul 2024 16:43:21 +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 1sPm1k-00BjPw-O4 for pgsql-hackers@arkaria.postgresql.org; Fri, 05 Jul 2024 16:43:17 +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 1sPm1j-00BjLQ-8D for pgsql-hackers@lists.postgresql.org; Fri, 05 Jul 2024 16:43:17 +0000 Received: from fout8-smtp.messagingengine.com ([103.168.172.151]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sPm1f-000Z0i-96 for pgsql-hackers@postgresql.org; Fri, 05 Jul 2024 16:43:14 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailfout.nyi.internal (Postfix) with ESMTP id B337E13804A0; Fri, 5 Jul 2024 12:43:09 -0400 (EDT) Received: from imap48 ([10.202.2.98]) by compute6.internal (MEProxy); Fri, 05 Jul 2024 12:43:09 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=compiler.org; h= cc:cc:content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1720197789; x=1720284189; bh=5F5yHN2pc0 MbhL0t3LMo8sK3qvuEx87cIuHh3YW9IJ4=; b=BZInJOUbtvvkk0zojIsEeIb59u pB+zrFVcjRixXjmmOnUlDUj9P6m7nMP0UtqjD1HY4VnPc4Z3D1eozGZlkVto0fKC qQG3Jgam5ynvnT4T7Ay6nYoGxw9BVNOasFsqhfsFQmrXUqde1ObBcLPn7nL3scUy TXxl78drjGK3G65xmTiCRvZlkSKtFOIXaMAKVek3O1SzWB9B63tq4ocAI9EkCjq1 qJT5hhvvk9Thz0gCxuv/oS0knvmlTDtSBiSFC7fRzX3ikg+AC/+z9CHwoOYgOZ8S /jGTcoQBt07/UGvxRDT7x8lsuKxuZ32uxtON4+UsXtv0lJzF0pKxvF4qPfkQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1720197789; x=1720284189; bh=5F5yHN2pc0MbhL0t3LMo8sK3qvuE x87cIuHh3YW9IJ4=; b=MR8ZR2MP1QhonTVDuUrXiSJPD4Fl1JmiSSFPXUeLrGl0 lny8u9EYllPvulfIFYenVBeCeznXB65V9o4YUAuBBPp5CoLBZKCv23jymhWz7M1W 1nV1/3MZCdzKeBwsWFQCEBaoHWraUcK8Us1E4lVMTNDr9JrT6YFcLZAIIUC1DJVV Dih+mXac41kuM4m0M1kAKHwJKXR/vXue0MqilsNfimmfRy/LIJyQukmeLv+wKi3W 2Gbz2psiLv7pSdXtmOMXgzvoVaKGVP3T/kdU3iUJGdQyqqz18b3hAFqn3+w7K/m4 9IcYG2eLDDMDOShKqSq8BxfDx7sh3ksQPpX5rXCt7w== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvddugddutdefucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepofgfggfkjghffffhvfevufgtsehttdertderredtnecuhfhrohhmpedflfho vghlucflrggtohgsshhonhdfuceojhhovghlsegtohhmphhilhgvrhdrohhrgheqnecugg ftrfgrthhtvghrnhephfefudegffdvtdetleeuhfffkeeltddukefhkeekieelleffhfef tdffjefgjeefnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrh homhepjhhovghlsegtohhmphhilhgvrhdrohhrgh X-ME-Proxy: Feedback-ID: ic6394509:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 5B71B31A0066; Fri, 5 Jul 2024 12:43:09 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.11.0-alpha0-566-g3812ddbbc-fm-20240627.001-g3812ddbb MIME-Version: 1.0 Message-Id: <58e5e7d2-7ad8-40b4-9b76-a5c3049346e5@app.fastmail.com> In-Reply-To: References: <44d2ffca-d560-4919-b85a-4d07060946aa@app.fastmail.com> <87zfr1e1uj.fsf@wibble.ilmari.org> <9ff90ff9-5ac1-4529-b2f4-f93b37734b9c@app.fastmail.com> <81348268-b9fd-4308-bf52-056070aa7c2a@app.fastmail.com> <8e909218-1965-4515-99e3-4bb5b625e004@app.fastmail.com> <0459ded5-6daf-4bf0-9ab0-e1f11ab15ec6@app.fastmail.com> <4e54bf4f-59fe-4c9a-b446-a2b64cbfd584@app.fastmail.com> <27d42b25-f328-4231-9db0-3811dc77c8cf@app.fastmail.com> <33c51978-8ee8-438c-9804-e46c0ca8f3f9@app.fastmail.com> <018dd6e8-8f21-4b3b-8d80-2d128f6d00b4@app.fastmail.com> <32510d71-8c00-4896-9c11-5ea4b3c01ffc@app.fastmail.com> <31b8386d-4bcb-4909-b7da-bbeb9a8ece55@app.fastmail.com> <42d93ebe-fbed-448f-b550-096be9e577db@app.fastmail.com> Date: Fri, 05 Jul 2024 18:42:45 +0200 From: "Joel Jacobson" To: "Dean Rasheed" Cc: =?UTF-8?Q?Dagfinn_Ilmari_Manns=C3=A5ker?= , pgsql-hackers Subject: Re: Optimize numeric multiplication for one and two base-NBASE digit multiplicands. Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, Jul 5, 2024, at 17:41, Dean Rasheed wrote: > On Fri, 5 Jul 2024 at 12:56, Joel Jacobson wrote: >> >> Interesting you got so bad bench results for v6-mul_var_int64.patch >> for var1ndigits=4, that patch is actually the winner on AMD Ryzen 9 7950X3D. > > Interesting. I remeasured just to be sure, and yes, it was the winner among the previous patches, but the new v7 beats it. >> On Intel Core i9-14900K the winner is v6-optimize-numeric-mul_var-small-var1-arbitrary-var2.patch. > > That must be random noise, since > v6-optimize-numeric-mul_var-small-var1-arbitrary-var2.patch doesn't > invoke mul_var_small() for 4-digit inputs. Yes, something was off with the HEAD measurements for that one, I remeasured and then got almost identical results (as expected) between HEAD and v6-optimize-numeric-mul_var-small-var1-arbitrary-var2.patch for 4-digit inputs. >> On Apple M3 Max, HEAD is the winner. > > Importantly, mul_var_int64() is around 1.25x slower there, and it was > even worse on my machine. > > Attached is a v7 mul_var_small() patch adding 4-digit support. For me, > this gives a nice speedup: > > SELECT SUM(var1*var2) FROM bench_mul_var_var1ndigits_4; > Time: 5617.150 ms (00:05.617) -- HEAD > Time: 8203.081 ms (00:08.203) -- v6-mul_var_int64.patch > Time: 4750.212 ms (00:04.750) -- v7-mul_var_small.patch > > The other advantage, of course, is that it doesn't require 128-bit > integer support. Very nice, v7-optimize-numeric-mul_var-small-var1-arbitrary-var2.patch is now the winner on all my CPUs: -- Apple M3 Max SELECT SUM(var1*var2) FROM bench_mul_var_var1ndigits_4; -- HEAD Time: 3574.865 ms (00:03.575) Time: 3573.678 ms (00:03.574) Time: 3576.953 ms (00:03.577) Time: 3580.536 ms (00:03.581) Time: 3589.007 ms (00:03.589) SELECT SUM(var1*var2) FROM bench_mul_var_var1ndigits_4; -- v7-optimize-numeric-mul_var-small-var1-arbitrary-var2.patch Time: 3110.171 ms (00:03.110) Time: 3098.558 ms (00:03.099) Time: 3105.873 ms (00:03.106) Time: 3104.484 ms (00:03.104) Time: 3109.035 ms (00:03.109) -- Intel Core i9-14900K SELECT SUM(var1*var2) FROM bench_mul_var_var1ndigits_4; -- HEAD Time: 3751.767 ms (00:03.752) Time: 3745.916 ms (00:03.746) Time: 3742.542 ms (00:03.743) Time: 3746.139 ms (00:03.746) Time: 3745.493 ms (00:03.745) SELECT SUM(var1*var2) FROM bench_mul_var_var1ndigits_4; -- v6-optimize-numeric-mul_var-small-var1-arbitrary-var2.patch Time: 3747.640 ms (00:03.748) Time: 3747.231 ms (00:03.747) Time: 3747.965 ms (00:03.748) Time: 3748.309 ms (00:03.748) Time: 3746.498 ms (00:03.746) SELECT SUM(var1*var2) FROM bench_mul_var_var1ndigits_4; -- v7-optimize-numeric-mul_var-small-var1-arbitrary-var2.patch Time: 3417.924 ms (00:03.418) Time: 3417.088 ms (00:03.417) Time: 3415.708 ms (00:03.416) Time: 3415.453 ms (00:03.415) Time: 3419.566 ms (00:03.420) -- AMD Ryzen 9 7950X3D SELECT SUM(var1*var2) FROM bench_mul_var_var1ndigits_4; -- HEAD Time: 3970.131 ms (00:03.970) Time: 3924.335 ms (00:03.924) Time: 3927.863 ms (00:03.928) Time: 3924.761 ms (00:03.925) Time: 3926.290 ms (00:03.926) SELECT SUM(var1*var2) FROM bench_mul_var_var1ndigits_4; -- v6-add-mul_var_int64.patch Time: 3874.769 ms (00:03.875) Time: 3858.071 ms (00:03.858) Time: 3836.698 ms (00:03.837) Time: 3871.388 ms (00:03.871) Time: 3844.907 ms (00:03.845) SELECT SUM(var1*var2) FROM bench_mul_var_var1ndigits_4; -- v7-optimize-numeric-mul_var-small-var1-arbitrary-var2.patch Time: 3397.846 ms (00:03.398) Time: 3398.050 ms (00:03.398) Time: 3395.279 ms (00:03.395) Time: 3393.285 ms (00:03.393) Time: 3402.570 ms (00:03.403) Code wise I think it's now very nice and clear, with just enough comments. Also nice to see that the var1ndigits=4 case isn't much more complex than var1ndigits=3, since it follows the same pattern. Regards, Joel