Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
gh-127937: convert decimal module to use import API for ints (PEP 757)#127925
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
Conversation
skirpichev commented Dec 13, 2024 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
picnixz commented Dec 13, 2024
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 |
Uh oh!
There was an error while loading. Please reload this page.
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
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".
skirpichev commented Dec 14, 2024
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? |
picnixz commented Dec 14, 2024
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. |
picnixz commented Dec 14, 2024
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) |
skirpichev commented Dec 14, 2024
You can estimate them looking on the gmpy2 pr (referenced in the PEP): gmpy2/gmpy2#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. |
picnixz commented Dec 14, 2024 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
|
This comment was marked as outdated.
This comment was marked as outdated.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Misc/NEWS.d/next/C_API/2024-12-14-03-40-15.gh-issue-127925.FF7aov.rst Outdated Show resolvedHide resolved
Uh oh!
There was an error while loading. Please reload this page.
Misc/NEWS.d/next/C_API/2024-12-14-03-40-15.gh-issue-127925.FF7aov.rst Outdated Show resolvedHide resolved
Uh oh!
There was an error while loading. Please reload this page.
* cleanup: forgotten PyLongWriter_Discard, pylong variable * clarify news
vstinner left a comment
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.
Uh oh!
There was an error while loading. Please reload this page.
serhiy-storchaka commented Jan 7, 2025
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 <vstinner@python.org>
skirpichev commented Jan 8, 2025 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
@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 |
vstinner commented Jan 8, 2025
@picnixz: Would you mind to review the latest PR version? It changed a lot since last month. |
picnixz left a comment
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.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
vstinner commented Jan 13, 2025
@serhiy-storchaka: Are you ok with this change? Or are you worried about the mpd_sizeinbase() issue? |
skirpichev commented Jan 24, 2025
I would prefer not do this, but to make some progress we could introduce a temporary buffer. @vstinner? |
skirpichev commented Jan 24, 2025
Ok, with a buffer (latest change) I got something like this:
Benchmark hidden because not significant (1): int(Decimal(1<<3000)) I'm not sure if the third case is a real speed regression or just a noise. But I think it can be acceptable, as this clear Serhiy concerns, that blocked the pr before. Hence, I push this. |
serhiy-storchaka left a comment
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. Later we can optimize it more. For very large values the total time is dominated by non-linear conversion time, so additional allocation and copying does not matter.
Uh oh!
There was an error while loading. Please reload this page.
picnixz commented Jan 24, 2025
LGTM (I've reviewed the two new commits and it's fine). |
vstinner left a comment
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
3d8fc8b into python:mainUh oh!
There was an error while loading. Please reload this page.
vstinner commented Jan 24, 2025
Merged, thanks @skirpichev for the tedious change! |
skirpichev commented Jan 24, 2025
Thanks for reviews. I'll open an issue to track possible improvement (no temporary buffer). |
>>> sys.int_info[:2] (30, 4)Details