-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
80f1a04
gh-102471: convert decimal module to use PyLongWriter API (PEP 757)
skirpichev c13b7d2
+ news
skirpichev 589f926
Apply suggestions from code review
skirpichev f27adef
Merge branch 'master' into long_export-decimal
skirpichev 6669b89
+ adapt dec_from_long() to use PEP 757
skirpichev 05ec274
Merge branch 'master' into long_export-decimal
skirpichev 6e46bc1
Don't use PyLong_GetNativeLayout()
skirpichev 7f0061f
Address review:
skirpichev bae0234
Apply suggestions from code review
skirpichev 4e0460c
address review: don't use digit type
skirpichev 87ded2e
+ forgot (char*) cast
skirpichev 541441e
Apply Serhiy suggestion
skirpichev 6ab20f1
add asserts
skirpichev 83471fd
speed up export
skirpichev cb169f7
Apply suggestions from code review
skirpichev 0ea0d59
Merge branch 'master' into long_export-decimal
skirpichev 61e76d2
use PyLong_GetNativeLayout() to query layout
skirpichev 90bafc1
+ comment
skirpichev c117956
Apply suggestions from code review
skirpichev 7b97855
address review:
skirpichev 7e0e9a9
Update Misc/NEWS.d/next/C_API/2024-12-14-03-40-15.gh-issue-127925.FF7…
skirpichev fec6666
Merge branch 'master' into long_export-decimal
skirpichev 5a00ee2
set all digits to 0
skirpichev 37ec841
Merge branch 'master' into long_export-decimal
skirpichev d4728c4
Merge branch 'master' into long_export-decimal
skirpichev f6a4afb
+ cleanup, add asserts
skirpichev 4b07189
Merge branch 'master' into long_export-decimal
skirpichev 4db7917
Merge branch 'master' into long_export-decimal
skirpichev 0fec6e1
address review
skirpichev 59636f9
address review
skirpichev b8bf49f
Update Modules/_decimal/_decimal.c
skirpichev a8189e6
Update Modules/_decimal/_decimal.c
skirpichev 3854262
Merge branch 'master' into long_export-decimal
skirpichev 336e881
use temporary buffer of digits, tmp_digits
skirpichev e658f2b
address review: comment
skirpichev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
Misc/NEWS.d/next/C_API/2024-12-14-03-40-15.gh-issue-127925.FF7aov.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Convert the :mod:`decimal` module to use :pep:`757` API. That offer some | ||
speedup, if integer part of the :class:`~decimal.Decimal` instance is small. | ||
skirpichev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Patch by Sergey B Kirpichev. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3639,12 +3639,13 @@ dec_format(PyObject *dec, PyObject *args) | |
static PyObject * | ||
dec_as_long(PyObject *dec, PyObject *context, int round) | ||
{ | ||
PyLongObject *pylong; | ||
PyObject *pylong; | ||
digit *ob_digit; | ||
size_t n; | ||
mpd_t *x; | ||
mpd_context_t workctx; | ||
uint32_t status = 0; | ||
const PyLongLayout *layout = PyLong_GetNativeLayout(); | ||
|
||
if (mpd_isspecial(MPD(dec))) { | ||
if (mpd_isnan(MPD(dec))) { | ||
|
@@ -3672,34 +3673,45 @@ dec_as_long(PyObject *dec, PyObject *context, int round) | |
} | ||
|
||
status = 0; | ||
ob_digit = NULL; | ||
|
||
int64_t val = mpd_qget_i64(x, &status); | ||
if (!status) { | ||
mpd_del(x); | ||
return PyLong_FromInt64(val); | ||
} | ||
|
||
const uint8_t bpd = layout->bits_per_digit; | ||
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 commentThe 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". |
||
ob_digit[n-1] = 0; | ||
if (writer == NULL) { | ||
PyLongWriter_Discard(writer); | ||
skirpichev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
mpd_del(x); | ||
return NULL; | ||
} | ||
|
||
status = 0; | ||
#if PYLONG_BITS_IN_DIGIT == 30 | ||
skirpichev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
n = mpd_qexport_u32(&ob_digit, 0, PyLong_BASE, x, &status); | ||
n = mpd_qexport_u32(&ob_digit, n, PyLong_BASE, x, &status); | ||
#elif PYLONG_BITS_IN_DIGIT == 15 | ||
n = mpd_qexport_u16(&ob_digit, 0, PyLong_BASE, x, &status); | ||
n = mpd_qexport_u16(&ob_digit, n, PyLong_BASE, x, &status); | ||
#else | ||
#error "PYLONG_BITS_IN_DIGIT should be 15 or 30" | ||
#endif | ||
|
||
if (n == SIZE_MAX) { | ||
PyErr_NoMemory(); | ||
PyLongWriter_Discard(writer); | ||
mpd_del(x); | ||
return NULL; | ||
} | ||
|
||
if (n == 1) { | ||
sdigit val = mpd_arith_sign(x) * ob_digit[0]; | ||
mpd_free(ob_digit); | ||
mpd_del(x); | ||
return PyLong_FromLong(val); | ||
} | ||
|
||
assert(n > 0); | ||
skirpichev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert(!mpd_iszero(x)); | ||
pylong = _PyLong_FromDigits(mpd_isnegative(x), n, ob_digit); | ||
mpd_free(ob_digit); | ||
pylong = PyLongWriter_Finish(writer); | ||
mpd_del(x); | ||
return (PyObject *) pylong; | ||
return pylong; | ||
skirpichev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/* Convert a Decimal to its exact integer ratio representation. */ | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.