-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
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
base: main
Are you sure you want to change the base?
Conversation
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()).
Edit: For a context, Serhiy's concerns about using mpd_sizeinbase() from old pr:
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: cpython/Modules/_decimal/libmpdec/mpdecimal.c Lines 8084 to 8113 in 65ae3d5
Essentially, it does
For |
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.
Based on the previous PR and discussions, this looks correct to me. I'd like Serhiy's opinion on it, though, since he expressed concerns earlier.
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 hidden because not significant (2): int(Decimal(1<<38)), int(Decimal(1<<3000))