pgsql: Optimise numeric division for one and two base-NBASE digit divis

From: Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Optimise numeric division for one and two base-NBASE digit divis
Date: 2022-02-27 11:14:20
Message-ID: E1nOHVM-0008KS-8C@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Optimise numeric division for one and two base-NBASE digit divisors.

Formerly div_var() had "fast path" short division code that was
significantly faster when the divisor was just one base-NBASE digit,
but otherwise used long division.

This commit adds a new function div_var_int() that divides by an
arbitrary 32-bit integer, using the fast short division algorithm, and
updates both div_var() and div_var_fast() to use it for one and two
digit divisors. In the case of div_var(), this is slightly faster in
the one-digit case, because it avoids some digit array copying, and is
much faster in the two-digit case where it replaces long division. For
div_var_fast(), it is much faster in both cases because the main
div_var_fast() algorithm is optimised for larger inputs.

Additionally, optimise exp() and ln() by using div_var_int(), allowing
a NumericVar to be replaced by an int in a couple of places, most
notably in the Taylor series code. This produces a significant speedup
of exp(), ln() and the numeric_big regression test.

Dean Rasheed, reviewed by Tom Lane.

Discussion: https://postgr.es/m/CAEZATCVwsBi-ND-t82Cuuh1=8ee6jdOpzsmGN+CUZB6yjLg9jw@mail.gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/d1b307eef2818fe24760cc2c168d7d65d59775a8

Modified Files
--------------
src/backend/utils/adt/numeric.c | 223 ++++++++++++++++++++++++++++++++--------
1 file changed, 180 insertions(+), 43 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Michael Paquier 2022-02-28 01:54:36 pgsql: pg_stat_statements: Remove unnecessary call to GetUserId()
Previous Message Dean Rasheed 2022-02-27 10:42:48 pgsql: Simplify the inner loop of numeric division in div_var().