pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Dean Rasheed <dean.a.rasheed@gmail.com>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Improve the performance and accuracy of numeric sqrt() and ln().
Date: Sat, 28 Mar 2020 14:42:24 +0000
Message-ID: <E1jICfI-0000xD-0O@gemulon.postgresql.org> (raw)

Improve the performance and accuracy of numeric sqrt() and ln().

Instead of using Newton's method to compute numeric square roots, use
the Karatsuba square root algorithm, which performs better for numbers
of all sizes. In practice, this is 3-5 times faster for inputs with
just a few digits and up to around 10 times faster for larger inputs.

Also, the new algorithm guarantees that the final digit of the result
is correctly rounded, since it computes an integer square root with
truncation, containing at least 1 extra decimal digit before rounding.
The former algorithm would occasionally round the wrong way because
it rounded both the intermediate and final results.

In addition, arrange for sqrt_var() to explicitly support negative
rscale values (rounding before the decimal point). This allows the
argument reduction phase of ln_var() to be optimised for large inputs,
since it only needs to compute square roots with a few more digits
than the final ln() result, rather than computing all the digits
before the decimal point. For very large inputs, this can be many
thousands of times faster.

In passing, optimise div_var_fast() in a couple of places where it was
doing unnecessary work.

Patch be me, reviewed by Tom Lane and Tels.

Discussion: https://postgr.es/m/CAEZATCV1A7+jD3P30Zu31KjaxeSEyOn3v9d6tYegpxcq3cQu-g@mail.gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/4083f445c0cbda2eacb6b17297f9d555397e5eaa

Modified Files
--------------
src/backend/utils/adt/numeric.c       | 585 +++++++++++++++++++++++++++++++---
src/test/regress/expected/numeric.out |  51 +++
src/test/regress/sql/numeric.sql      |  13 +
3 files changed, 600 insertions(+), 49 deletions(-)



Message-ID: <E1jICfI-0000xD-0O@gemulon.postgresql.org>
Permalink:  ../E1jICfI-0000xD-0O@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1jICfI-0000xD-0O@gemulon.postgresql.org

 · 

reply

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-committers@postgresql.org
  Cc: dean.a.rasheed@gmail.com
  Subject: Re: pgsql: Improve the performance and accuracy of numeric sqrt() and ln().
  In-Reply-To: <E1jICfI-0000xD-0O@gemulon.postgresql.org>
  References: <E1jICfI-0000xD-0O@gemulon.postgresql.org>

* 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