-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
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-102471: convert decimal module to use PyLong_Export API (PEP 757) #128267
Open
skirpichev
wants to merge
4
commits into
python:main
Choose a base branch
from
skirpichev:decimal-use-PyLong_Export/127937
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+35
−23
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f7f031f
gh-102471: convert decimal module to use PyLong_Export API (PEP 757)
skirpichev bf687e5
XXX try PyLong_Export without value
skirpichev 021ab8e
Revert "XXX try PyLong_Export without value"
skirpichev 5764614
address review: add asserts
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
This file contains 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 |
---|---|---|
|
@@ -30,7 +30,6 @@ | |
#endif | ||
|
||
#include <Python.h> | ||
#include "pycore_long.h" // _PyLong_IsZero() | ||
#include "pycore_pystate.h" // _PyThreadState_GET() | ||
#include "pycore_typeobject.h" | ||
#include "complexobject.h" | ||
|
@@ -2323,38 +2322,51 @@ static PyObject * | |
dec_from_long(decimal_state *state, PyTypeObject *type, PyObject *v, | ||
const mpd_context_t *ctx, uint32_t *status) | ||
{ | ||
PyObject *dec; | ||
PyLongObject *l = (PyLongObject *)v; | ||
PyObject *dec = PyDecType_New(state, type); | ||
|
||
dec = PyDecType_New(state, type); | ||
if (dec == NULL) { | ||
return NULL; | ||
} | ||
|
||
if (_PyLong_IsZero(l)) { | ||
_dec_settriple(dec, MPD_POS, 0, 0); | ||
return dec; | ||
PyLongExport export_long; | ||
|
||
if (PyLong_Export(v, &export_long) == -1) { | ||
Py_DECREF(dec); | ||
return NULL; | ||
} | ||
if (export_long.digits) { | ||
const PyLongLayout *layout = PyLong_GetNativeLayout(); | ||
const uint32_t base = (uint32_t)1 << layout->bits_per_digit; | ||
const uint8_t sign = export_long.negative ? MPD_NEG : MPD_POS; | ||
const Py_ssize_t len = export_long.ndigits; | ||
|
||
uint8_t sign = _PyLong_IsNegative(l) ? MPD_NEG : MPD_POS; | ||
assert(layout->bits_per_digit <= 32); | ||
assert(layout->digits_order == -1); | ||
assert(layout->digit_endianness == (PY_LITTLE_ENDIAN ? -1 : 1)); | ||
assert(layout->digit_size == 2 || layout->digit_size == 4); | ||
|
||
if (_PyLong_IsCompact(l)) { | ||
_dec_settriple(dec, sign, l->long_value.ob_digit[0], 0); | ||
mpd_qfinalize(MPD(dec), ctx, status); | ||
return dec; | ||
if (layout->digit_size == 4) { | ||
mpd_qimport_u32(MPD(dec), export_long.digits, len, sign, | ||
base, ctx, status); | ||
} | ||
else { | ||
mpd_qimport_u16(MPD(dec), export_long.digits, len, sign, | ||
base, ctx, status); | ||
} | ||
PyLong_FreeExport(&export_long); | ||
} | ||
size_t len = _PyLong_DigitCount(l); | ||
|
||
#if PYLONG_BITS_IN_DIGIT == 30 | ||
mpd_qimport_u32(MPD(dec), l->long_value.ob_digit, len, sign, PyLong_BASE, | ||
ctx, status); | ||
#elif PYLONG_BITS_IN_DIGIT == 15 | ||
mpd_qimport_u16(MPD(dec), l->long_value.ob_digit, len, sign, PyLong_BASE, | ||
ctx, status); | ||
#else | ||
#error "PYLONG_BITS_IN_DIGIT should be 15 or 30" | ||
#endif | ||
else { | ||
const int64_t value = export_long.value; | ||
|
||
if (-(int64_t)UINT32_MAX <= value && value <= (int64_t)UINT32_MAX) { | ||
_dec_settriple(dec, value < 0 ? MPD_NEG : MPD_POS, | ||
(uint32_t)Py_ABS(value), 0); | ||
mpd_qfinalize(MPD(dec), ctx, status); | ||
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. Is it worth it to have this fast-path? (Why not always calling mpd_qset_i64()?) 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. It's up to reviewers. In old pr I did benchmarks: |
||
} | ||
else { | ||
mpd_qset_i64(MPD(dec), value, ctx, status); | ||
} | ||
} | ||
return dec; | ||
} | ||
|
||
|
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.
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.
Please remove redundant
const
s.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.
Should not we check if
layout->bits_per_digit >= 32
?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.
Which one looks redundant for you?
I was thinking about an assert. Do you think it's ok?
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.
All
const
s applied to POD types are redundant. 3 lines here and 1 line forexport_long.value
.Some style guides require to add
const
for all variables and function parameters, but this in not in CPython style guide. It makes the code too verbose and more difficult to read.It is okay for now. But if we want to make it an exemplary code, we should remove dependence on CPython implementation details.
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.
Why? Doing something like
value += 1
currently will produce an error at compilation time, while withoutconst
- not.It emphasize programmer intentions not just for compiler, but for human being as well.
Ok, then I'll add assertions for all assumptions, that are silent not (endianness, digit order, etc). I think it's fine for a stdlib module. Caching in the module state seems overkill for this.