Re: double and numeric conversion

Lists: pgsql-hackers
From: Theo Schlossnagle <jesus(at)omniti(dot)com>
To: PostgreSQL-development Development <pgsql-hackers(at)postgresql(dot)org>
Cc: Theo Schlossnagle <jesus(at)omniti(dot)com>
Subject: double and numeric conversion
Date: 2010-03-01 20:04:29
Message-ID: 64085043-3113-46C5-AE8F-5BBD2D9C1EF4@omniti.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hello all,

I'm writing some extension and I have a hot code path that has a lot of double (C type) data and needs to output NUMERIC tuple data. The current methods I can find in the code to convert sprintf the double to a buffer and then invoke the numeric_in function on them. I've profile my stuff and I'm spending (wasting) all my time in that conversion. Is there a more efficient method of converting a double into a postgres numeric value?

Best regards,

Theo

--
Theo Schlossnagle
http://omniti.com/is/theo-schlossnagle


From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Theo Schlossnagle <jesus(at)omniti(dot)com>
Cc: PostgreSQL-development Development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: double and numeric conversion
Date: 2010-03-01 20:58:44
Message-ID: 4B8C2A84.5070506@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Theo Schlossnagle wrote:
> Hello all,
>
> I'm writing some extension and I have a hot code path that has a lot of double (C type) data and needs to output NUMERIC tuple data. The current methods I can find in the code to convert sprintf the double to a buffer and then invoke the numeric_in function on them. I've profile my stuff and I'm spending (wasting) all my time in that conversion. Is there a more efficient method of converting a double into a postgres numeric value?
>
>
>

float8_numeric() ? Although it uses sprintf too, by the look of it.

cheers

andrew


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Theo Schlossnagle <jesus(at)omniti(dot)com>
Cc: PostgreSQL-development Development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: double and numeric conversion
Date: 2010-03-01 21:35:49
Message-ID: 24694.1267479349@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Theo Schlossnagle <jesus(at)omniti(dot)com> writes:
> I'm writing some extension and I have a hot code path that has a lot of double (C type) data and needs to output NUMERIC tuple data. The current methods I can find in the code to convert sprintf the double to a buffer and then invoke the numeric_in function on them. I've profile my stuff and I'm spending (wasting) all my time in that conversion. Is there a more efficient method of converting a double into a postgres numeric value?

If you're worried about micro-optimization, why are you using NUMERIC at
all? It's no speed demon.

Although you might be able to shave some cycles with a dedicated code
path for this conversion, binary to decimal is fundamentally not cheap.

regards, tom lane


From: Theo Schlossnagle <jesus(at)omniti(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Theo Schlossnagle <jesus(at)omniti(dot)com>, PostgreSQL-development Development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: double and numeric conversion
Date: 2010-03-02 14:37:12
Message-ID: B0DA3429-2865-4113-9E0F-CDAF1112B46C@omniti.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


On Mar 1, 2010, at 4:35 PM, Tom Lane wrote:

> Theo Schlossnagle <jesus(at)omniti(dot)com> writes:
>> I'm writing some extension and I have a hot code path that has a lot of double (C type) data and needs to output NUMERIC tuple data. The current methods I can find in the code to convert sprintf the double to a buffer and then invoke the numeric_in function on them. I've profile my stuff and I'm spending (wasting) all my time in that conversion. Is there a more efficient method of converting a double into a postgres numeric value?
>
> If you're worried about micro-optimization, why are you using NUMERIC at
> all? It's no speed demon.
>
> Although you might be able to shave some cycles with a dedicated code
> path for this conversion, binary to decimal is fundamentally not cheap.

I feared that was the case. I spent an hour or so coding that last night and the speedups for me were worth it, I see a 2 fold speedup in conversion operations (or a 50% reduction in CPU cycles per conversion). The integer ones were trivial, the double one has the imperfect issue of reasonably guessing the dscale, but seems to work in my tests.

I didn't look deeply at the postgres internals to see if there was a way to do double -> numeric and integer-types -> numeric without intermediary string format. If that sort of thing is easy to leverage, I'd be happy to share the code.

--
Theo Schlossnagle
http://omniti.com/is/theo-schlossnagle


From: Yeb Havinga <yebhavinga(at)gmail(dot)com>
To: Theo Schlossnagle <jesus(at)omniti(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, PostgreSQL-development Development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: double and numeric conversion
Date: 2010-03-03 10:01:20
Message-ID: 4B8E3370.4040409@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Theo Schlossnagle wrote:
> I didn't look deeply at the postgres internals to see if there was a way to do double -> numeric and integer-types -> numeric without intermediary string format. If that sort of thing is easy to leverage, I'd be happy to share the code.
>
I think your code could be valuable for postgres on the fact alone that
it is almost twice as fast, and probably easy to integrate and unit
test. We make heavy use of the numeric data type, so I'm very interested!

regards
Yeb Havinga


From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Yeb Havinga <yebhavinga(at)gmail(dot)com>
Cc: Theo Schlossnagle <jesus(at)omniti(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, PostgreSQL-development Development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: double and numeric conversion
Date: 2010-03-03 10:28:15
Message-ID: 162867791003030228n67a4111t4fdd9dc33346efa4@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2010/3/3 Yeb Havinga <yebhavinga(at)gmail(dot)com>:
> Theo Schlossnagle wrote:
>>
>> I didn't look deeply at the postgres internals to see if there was a way
>> to do double -> numeric and integer-types -> numeric without intermediary
>> string format.  If that sort of thing is easy to leverage, I'd be happy to
>> share the code.
>>
>
> I think your code could be valuable for postgres on the fact alone that it
> is almost twice as fast, and probably easy to integrate and unit test. We
> make heavy use of the numeric data type, so I'm very interested!

I did some test and numeric->double is about 5% faster than
numeric->string->double (on my PC)

Regards
Pavel Stehule

>
> regards
> Yeb Havinga
>
>
>
> --
> Sent via pgsql-hackers mailing list (pgsql-hackers(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-hackers
>


From: Yeb Havinga <yebhavinga(at)gmail(dot)com>
To: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
Cc: Theo Schlossnagle <jesus(at)omniti(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, PostgreSQL-development Development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: double and numeric conversion
Date: 2010-03-03 10:57:14
Message-ID: 4B8E408A.4090103@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Pavel Stehule wrote:
> 2010/3/3 Yeb Havinga <yebhavinga(at)gmail(dot)com>:
>
>> Theo Schlossnagle wrote:
>>
>>> I didn't look deeply at the postgres internals to see if there was a way
>>> to do double -> numeric and integer-types -> numeric without intermediary
>>> string format. If that sort of thing is easy to leverage, I'd be happy to
>>> share the code.
>>>
>>>
>> I think your code could be valuable for postgres on the fact alone that it
>> is almost twice as fast, and probably easy to integrate and unit test. We
>> make heavy use of the numeric data type, so I'm very interested!
>>
>
> I did some test and numeric->double is about 5% faster than
> numeric->string->double (on my PC)
>
numeric_to_double_no_overflow() also uses string as intermediate format.

Theo's conversions are the converse, from double to numeric, and do not
use string as intermediate format (if I understand it correct). (where
float8_numeric
http://doxygen.postgresql.org/backend_2utils_2adt_2numeric_8c.html#2de7f65c8de4b65dad441e77ea1bf402
does)

regards
Yeb Havinga


From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Yeb Havinga <yebhavinga(at)gmail(dot)com>
Cc: Theo Schlossnagle <jesus(at)omniti(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, PostgreSQL-development Development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: double and numeric conversion
Date: 2010-03-03 11:02:54
Message-ID: 162867791003030302j3f14d29bx1b0d65c54ba26561@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2010/3/3 Yeb Havinga <yebhavinga(at)gmail(dot)com>:
> Pavel Stehule wrote:
>>
>> 2010/3/3 Yeb Havinga <yebhavinga(at)gmail(dot)com>:
>>
>>>
>>> Theo Schlossnagle wrote:
>>>
>>>>
>>>> I didn't look deeply at the postgres internals to see if there was a way
>>>> to do double -> numeric and integer-types -> numeric without
>>>> intermediary
>>>> string format.  If that sort of thing is easy to leverage, I'd be happy
>>>> to
>>>> share the code.
>>>>
>>>>
>>>
>>> I think your code could be valuable for postgres on the fact alone that
>>> it
>>> is almost twice as fast, and probably easy to integrate and unit test. We
>>> make heavy use of the numeric data type, so I'm very interested!
>>>
>>
>> I did some test and numeric->double is about 5% faster than
>> numeric->string->double (on my PC)
>>
>
> numeric_to_double_no_overflow() also uses string as intermediate format.
>
> Theo's conversions are the converse, from double to numeric, and do not use
> string as intermediate format (if I understand it correct). (where
> float8_numeric
> http://doxygen.postgresql.org/backend_2utils_2adt_2numeric_8c.html#2de7f65c8de4b65dad441e77ea1bf402
> does)
>

aha - it is reason why time similar

Pavel

> regards
> Yeb Havinga
>
>


From: Theo Schlossnagle <jesus(at)omniti(dot)com>
To: Yeb Havinga <yebhavinga(at)gmail(dot)com>
Cc: Theo Schlossnagle <jesus(at)omniti(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, PostgreSQL-development Development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: double and numeric conversion
Date: 2010-03-03 14:52:46
Message-ID: BF2CDE34-127D-4469-B663-7B02ACF7968A@omniti.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

I can't release all of it, but the functions to convert uint64_t, int64_t and double to numeric Datum are the meat and I can expose those...

https://labs.omniti.com/pgsoltools/trunk/contrib/scratch/pg_type_to_numeric.c

As I mentioned, the dscale on the double_to_numeric is imperfect resulting in things like: 1.23 turning into 1.2300 in the numeric returned. This are significantly faster (as expected) than the type -> string -> numeric conversions.

On Mar 3, 2010, at 5:01 AM, Yeb Havinga wrote:

> Theo Schlossnagle wrote:
>> I didn't look deeply at the postgres internals to see if there was a way to do double -> numeric and integer-types -> numeric without intermediary string format. If that sort of thing is easy to leverage, I'd be happy to share the code.
>>
> I think your code could be valuable for postgres on the fact alone that it is almost twice as fast, and probably easy to integrate and unit test. We make heavy use of the numeric data type, so I'm very interested!
>
> regards
> Yeb Havinga
>
>

--
Theo Schlossnagle
http://omniti.com/is/theo-schlossnagle


From: Grzegorz Jaskiewicz <gj(at)pointblue(dot)com(dot)pl>
To: Theo Schlossnagle <jesus(at)omniti(dot)com>
Cc: Yeb Havinga <yebhavinga(at)gmail(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, PostgreSQL-development Development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: double and numeric conversion
Date: 2010-03-03 17:41:05
Message-ID: 8CCE5D19-F750-45D9-AC55-36CAF3AEC6AD@pointblue.com.pl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

if (p1 > buf)
++ * --p1;
else {

....

++ * --p1; ???

does it even compile ?


From: Grzegorz Jaskiewicz <gj(at)pointblue(dot)com(dot)pl>
To: PostgreSQL-development Development <pgsql-hackers(at)postgresql(dot)org>
Cc: Theo Schlossnagle <jesus(at)omniti(dot)com>, Yeb Havinga <yebhavinga(at)gmail(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Subject: Re: double and numeric conversion
Date: 2010-03-03 20:37:08
Message-ID: D33DC24F-9C7D-440B-B55C-A519CD1F3342@pointblue.com.pl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


On 3 Mar 2010, at 17:41, Grzegorz Jaskiewicz wrote:

> if (p1 > buf)
> ++ * --p1;
> else {
>
> ....
>
>
> ++ * --p1; ???
>
> does it even compile ?

Oh, I can see, that it is *(--p1)++ ,mea culpa.

Which doesn't change the fact, that the code is rather messy imo.