Repository navigation
Avoid temporary allocation in dec_as_long() #129275
Description
Feature or enhancement
Proposal:
After #127925, new code in the decimal module (using the PEP 757) do temporary allocation with mpdecimal's memory functions (in mpd_qexport_u32/16()), just as before:
cpython/Modules/_decimal/_decimal.c
Lines 3691 to 3706 in f10b7b2
| uint32_t base = (uint32_t)1 << layout->bits_per_digit; | |
| /* We use a temporary buffer for digits for now, as for nonzero rdata | |
| mpd_qexport_u32/u16() require either space "allocated by one of | |
| libmpdec’s allocation functions" or "rlen MUST be correct" (to avoid | |
| reallocation). This can be further optimized by using rlen from | |
| mpd_sizeinbase(). See gh-127925. */ | |
| void *tmp_digits = NULL; | |
| size_t n; | |
| status = 0; | |
| if (layout->digit_size == 4) { | |
| n = mpd_qexport_u32((uint32_t **)&tmp_digits, 0, base, x, &status); | |
| } | |
| else { | |
| n = mpd_qexport_u16((uint16_t **)&tmp_digits, 0, base, x, &status); | |
| } |
According to the documentation, we can prepare array of digits, which size could be estimated by mpd_sizeinbase(). Given this, the mpd_qexport_u32/16() shouldn't do any resize for this array. Here is Stefan comment on how safe this approach is:
mpd_sizeinbase() uses log10() from math.h for performance reasons. If log10() is IEEE compliant, the result should be sufficiently large. Resizing is for guarding against broken log10() implementations.
The current code in _decimal.c sets the libmpdec allocation functions to PyMem_Malloc() etc. So if longobject uses PyMem_Free() it is safe even when resizing occurs.
If the new API allows for allocators other that PyMem_Malloc(), it will rely on the IEEE compliance of log10().
Stefan Krah
We can expect slight performance boost for small (2-3 digits) integers.
The thread on mpdecimal-discuss maillist:
https://lists.sr.ht/~mpdecimal/mpdecimal-discuss/%3CZ2kIike42Wl0_YKd@skirpichev.msk.ru%3E
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
No response
Activity
pythongh-129275: avoid temporary buffer in dec_as_long()
serhiy-storchaka commentedon May 13, 2025
We should look not only at the documentation, but also at what they do in the code to prevent reallocation. Has their code changed since then?
This may not work for ndigits >
2**53or close. And your benchmarks show no difference for large ndigits.On other hand, for small integers (but outside of 64-bit range, which is already optimized) we can use a fixed size array allocated at stack.
skirpichev commentedon May 13, 2025
If we can't rely on the documentation - I don't think we should add optimizations, based on current code behavior.
Docs says: "Resizing is slow and should not occur if rlen has been obtained by a call to mpd_sizeinbase." Are you insist on something more strict like "and must not occur"? If so, we could ask Stefan to adjust docs, I think it's something he actually meant here.
Such cases we can filter out like mpd_sizeinbase does:
The mpd_qexport_u32() returns now SIZE_MAX in such cases, that means a memory error on our side.
I'll benchmark this.
skirpichev commentedon May 17, 2025
@serhiy-storchaka, are you talking about using export functions with
rdata!=NULLonly for smallrlen(say, < 15)? Or about using different allocation functions in libmpdec in this case?In any case, this looks much more complex than proposed approach (which uses dedicated library API).
serhiy-storchaka commentedon May 17, 2025
The former. This is a pretty small change.
skirpichev commentedon May 17, 2025
@serhiy-storchaka, I don't think so. It's essentially a third path in the code flow: you need to estimate size (how, if you don't trust the mpd_sizeinbase?), then use export functions with nonzero rdata, then memcpy that to digits array.
I doubt this worth code complications. I'm going to close this, unless someone else want to take over this issue.
serhiy-storchaka commentedon May 17, 2025
Something like this:
We only need to find the threshold. We can trust
mpd_sizeinbase()for small values. Or reserve a digit for greater certainty.skirpichev commentedon May 18, 2025
This is something I meant (my version was roughly same code, except for buffer size and naming things). And:
Your version also miss memset (to zero). With it's addition my benchmark results:
So, it's a kinda works, but with more code.
Details
I'm not sure. Though, I can imagine that such broken log10 is a much less probable case.
@serhiy-storchaka, what do you think on using a special code in mpd_sizeinbase to handle 2-bases? If we will have a correct lower bound for size - we can rely on Stefan's reply: "Resizing is for guarding against broken log10() implementations." Here is the patch for the bundled copy, please note the guard for digits size in CONFIG_64 branch:
skirpichev commentedon May 24, 2025
See also maillist thread.