Re: Optimize numeric multiplication for one and two base-NBASE digit multiplicands.
Dean Rasheed <dean.a.rasheed@gmail.com>
From: Dean Rasheed <dean.a.rasheed@gmail.com>
To: Joel Jacobson <joel@compiler.org>
Cc: Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>, pgsql-hackers <pgsql-hackers@postgresql.org>
Date: 2024-07-01T22:19:36Z
Lists: pgsql-hackers
Attachments
On Mon, 1 Jul 2024 at 20:56, Joel Jacobson <joel@compiler.org> wrote: > > Below is a more realistic benchmark > > CREATE TABLE bench_mul_var (num1 numeric, num2 numeric); > > INSERT INTO bench_mul_var (num1, num2) > SELECT random(0::numeric,1e8::numeric), random(0::numeric,1e8::numeric) FROM generate_series(1,1e8); > > SELECT SUM(num1*num2) FROM bench_mul_var; I had a play with this, and came up with a slightly different way of doing it that works for var2 of any size, as long as var1 is just 1 or 2 digits. Repeating your benchmark where both numbers have up to 2 NBASE-digits, this new approach was slightly faster: SELECT SUM(num1*num2) FROM bench_mul_var; -- HEAD Time: 4762.990 ms (00:04.763) Time: 4332.166 ms (00:04.332) Time: 4276.211 ms (00:04.276) Time: 4247.321 ms (00:04.247) Time: 4166.738 ms (00:04.167) SELECT SUM(num1*num2) FROM bench_mul_var; -- v2 patch Time: 4398.812 ms (00:04.399) Time: 3672.668 ms (00:03.673) Time: 3650.227 ms (00:03.650) Time: 3611.420 ms (00:03.611) Time: 3534.218 ms (00:03.534) SELECT SUM(num1*num2) FROM bench_mul_var; -- this patch Time: 3350.596 ms (00:03.351) Time: 3336.224 ms (00:03.336) Time: 3335.599 ms (00:03.336) Time: 3336.990 ms (00:03.337) Time: 3351.453 ms (00:03.351) (This was on an older Intel Core i9-9900K, so I'm not sure why all the timings are faster. What compiler settings are you using?) The approach taken in this patch only uses 32-bit integers, so in theory it could be extended to work for var1ndigits = 3, 4, or even more, but the code would get increasingly complex, and I ran out of steam at 2 digits. It might be worth trying though. Regards, Dean
Commits
-
Optimise numeric multiplication for short inputs.
- ca481d3c9ab7 18.0 landed
-
Optimise numeric division for 3 and 4 base-NBASE digit divisors.
- 0aa38db56bf4 16.0 cited
-
Optimise numeric division for one and two base-NBASE digit divisors.
- d1b307eef281 15.0 cited