-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
gh-127937: convert decimal module to use import API for ints (PEP 757) #127925
base: main
Are you sure you want to change the base?
Conversation
Let's not hide this. Maybe someone is using it (it was removed then restored IIRC).
Not needed I think, unless you want to indicate the performance gain (it's always nice to know that something is faster). I did report the improvements of |
Co-authored-by: Bénédikt Tran <[email protected]>
Modules/_decimal/_decimal.c
Outdated
n = (mpd_sizeinbase(x, 2) + bpd - 1) / bpd; | ||
PyLongWriter *writer = PyLongWriter_Create(mpd_isnegative(x), n, | ||
(void**)&ob_digit); | ||
/* mpd_sizeinbase can overestimate size by 1 digit, set it to zero. */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW, this looks as a bug in the mpdecimal. C.f. the GNU GMP, the mpz_sizeinbase docs says: "If base is a power of 2, the result is always exact".
I've updated the pr descriptions with my research. So far, I've found just one use case. At least, I think we should deprecate (not soft) this. This apparently affects not so much projects and there is now a public alternative. @picnixz, what do you think? |
I would be fine with deprecating it, saying which alternative to use, so that we can simply remove it in some later versions. I think Victor was the one who removed and restored it so we should ask him as well. |
If you prefer doing it in a follow-up PR because you fear it would be too hard to review, then it's better. If the change is minimal, we can do it this one (I didn't check the code to change) |
You can estimate them looking on the gmpy2 pr (referenced in the PEP): aleaxit/gmpy#495 In principle, I don't think that this will complicate review to much. On another hand, changes looks logically independent. I would rather include here deprecation. |
|
This comment was marked as outdated.
This comment was marked as outdated.
Misc/NEWS.d/next/C_API/2024-12-14-03-40-15.gh-issue-127925.FF7aov.rst
Outdated
Show resolved
Hide resolved
Misc/NEWS.d/next/C_API/2024-12-14-03-40-15.gh-issue-127925.FF7aov.rst
Outdated
Show resolved
Hide resolved
* cleanup: forgotten PyLongWriter_Discard, pylong variable * clarify news
@serhiy-storchaka, now I did memset, zeroed all digits before import. I don't see a measurable difference:
Benchmark hidden because not significant (1): int(Decimal(1<<3000)) |
Ok, I did some cleanup, added asserts. I think that @serhiy-storchaka concerns were addressed: now Should we add a safe path for systems with broken log10? From my benchmarks it seems that caching the layout parameters has very little effect on performance (or no at all). So, I don't think we should do this. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If libmpdec uses floating-point log10, it will likely does not work for integers with more than 2**53
bits (and perhaps before this limit). The maximal Decimal has 2**62
bits.
cc @tim-one, @mdickinson, @skrah
It's used for base argument, which is |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
mpd_qexport_*() functions used here with assumption, that no resizing
occur, i.e. len was obtained by a call to mpd_sizeinbase.
IMO it's a reasonable trade-off and an acceptable risk.
It is not guaranteed, and there is no way to enforce that resize does not occur in How to estimate the risk? If Python has undefined behavior in one of billion cases, is it acceptable risk? |
Co-authored-by: Victor Stinner <[email protected]>
@serhiy-storchaka, we have a confirmation from the library author, that this expectation is correct, unless libm is broken. I guess it's not just one place where we depend on quality of system libraries. Or do you believe that mpd_sizeinbase() can underestimate size with correct log10? If so, it's a bug. Lets just fix one. Here is the function (IIRC it's same in latest upstream version): cpython/Modules/_decimal/libmpdec/mpdecimal.c Lines 8084 to 8113 in 65ae3d5
Edit:
For |
@picnixz: Would you mind to review the latest PR version? It changed a lot since last month. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few final comments on English wording and some variables. Otherwise, LGTM. Sorry Victor, the ping got under my radar.
Co-authored-by: Bénédikt Tran <[email protected]>
>>> sys.int_info[:2] (30, 4)