Skip to content

gh-129275: avoid temporary buffer in dec_as_long() #129630

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

skirpichev
Copy link
Member

@skirpichev skirpichev commented Feb 4, 2025

According to the documentation: "If rdata is non-NULL, it MUST be allocated by one of libmpdec’s allocation functions and rlen MUST be correct. If necessary, the function will resize rdata. Resizing is slow and should not occur if rlen has been obtained by a call to mpd_sizeinbase."

So, possible resizing in mpd_qexport_u32/16() is for guarding against broken log10() implementations (log10 is used in the mpd_sizeinbase()).


Temporary memory allocation slowdown conversion for small integers (~2 digits):

Benchmark ref patch
int(Decimal(1<<7)) 499 ns 495 ns: 1.01x faster
int(Decimal(1<<300)) 2.16 us 2.06 us: 1.05x faster
Geometric mean (ref) 1.02x faster

Benchmark hidden because not significant (2): int(Decimal(1<<38)), int(Decimal(1<<3000))

# bench_Decimal-to-int.py

import pyperf
from decimal import Decimal

values = ['1<<7', '1<<38', '1<<300', '1<<3000']

runner = pyperf.Runner()
for v in values:
    d = Decimal(eval(v))
    bn = 'int(Decimal('+v+'))'
    runner.bench_func(bn, int, d)

According to the documentation: "If rdata is non-NULL, it MUST be
allocated by one of libmpdec’s allocation functions and rlen MUST be
correct.  If necessary, the function will resize rdata.  Resizing is
slow and should not occur if rlen has been obtained by a call to
mpd_sizeinbase."

So, possible resizing in mpd_qexport_u32/16() is for guarding against
broken log10() implementations (log10 is used in the mpd_sizeinbase()).
@skirpichev
Copy link
Member Author

In our case (base is a power of 2) we can avoid using mpd_sizeinbase() and implement more accurate version. Here is the mod_sizeinbase code:

size_t
mpd_sizeinbase(const mpd_t *a, uint32_t base)
{
double x;
size_t digits;
double upper_bound;
assert(mpd_isinteger(a));
assert(base >= 2);
if (mpd_iszero(a)) {
return 1;
}
digits = a->digits+a->exp;
#ifdef CONFIG_64
/* ceil(2711437152599294 / log10(2)) + 4 == 2**53 */
if (digits > 2711437152599294ULL) {
return SIZE_MAX;
}
upper_bound = (double)((1ULL<<53)-1);
#else
upper_bound = (double)(SIZE_MAX-1);
#endif
x = (double)digits / log10(base);
return (x > upper_bound) ? SIZE_MAX : (size_t)x + 1;
}

Essentially, it does ndigits * log2(10)/shift. This should be also a correct bound:

(size_t)(3.321928094887363*((ndigits + shift - 1)/shift))

For shift=30 and ndigits ~ 1<<53 (upper_bound for typical case) - it will overestimate size in just 1 digit.

@skirpichev skirpichev marked this pull request as ready for review February 4, 2025 08:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant