Skip to content
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

Open
wants to merge 32 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
80f1a04
gh-102471: convert decimal module to use PyLongWriter API (PEP 757)
skirpichev Jul 6, 2024
c13b7d2
+ news
skirpichev Dec 14, 2024
589f926
Apply suggestions from code review
skirpichev Dec 14, 2024
f27adef
Merge branch 'master' into long_export-decimal
skirpichev Dec 14, 2024
6669b89
+ adapt dec_from_long() to use PEP 757
skirpichev Dec 14, 2024
05ec274
Merge branch 'master' into long_export-decimal
skirpichev Dec 16, 2024
6e46bc1
Don't use PyLong_GetNativeLayout()
skirpichev Dec 16, 2024
7f0061f
Address review:
skirpichev Dec 16, 2024
bae0234
Apply suggestions from code review
skirpichev Dec 16, 2024
4e0460c
address review: don't use digit type
skirpichev Dec 16, 2024
87ded2e
+ forgot (char*) cast
skirpichev Dec 16, 2024
541441e
Apply Serhiy suggestion
skirpichev Dec 16, 2024
6ab20f1
add asserts
skirpichev Dec 17, 2024
83471fd
speed up export
skirpichev Dec 17, 2024
cb169f7
Apply suggestions from code review
skirpichev Dec 17, 2024
0ea0d59
Merge branch 'master' into long_export-decimal
skirpichev Dec 19, 2024
61e76d2
use PyLong_GetNativeLayout() to query layout
skirpichev Dec 19, 2024
90bafc1
+ comment
skirpichev Dec 19, 2024
c117956
Apply suggestions from code review
skirpichev Dec 19, 2024
7b97855
address review:
skirpichev Dec 20, 2024
7e0e9a9
Update Misc/NEWS.d/next/C_API/2024-12-14-03-40-15.gh-issue-127925.FF7…
skirpichev Dec 22, 2024
fec6666
Merge branch 'master' into long_export-decimal
skirpichev Dec 26, 2024
5a00ee2
set all digits to 0
skirpichev Dec 26, 2024
37ec841
Merge branch 'master' into long_export-decimal
skirpichev Jan 6, 2025
d4728c4
Merge branch 'master' into long_export-decimal
skirpichev Jan 6, 2025
f6a4afb
+ cleanup, add asserts
skirpichev Jan 7, 2025
4b07189
Merge branch 'master' into long_export-decimal
skirpichev Jan 7, 2025
4db7917
Merge branch 'master' into long_export-decimal
skirpichev Jan 7, 2025
0fec6e1
address review
skirpichev Jan 7, 2025
59636f9
address review
skirpichev Jan 7, 2025
b8bf49f
Update Modules/_decimal/_decimal.c
skirpichev Jan 8, 2025
a8189e6
Update Modules/_decimal/_decimal.c
skirpichev Jan 12, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Convert the :mod:`decimal` module to use :pep:`757` C API (export-import
integers), offering some speed-up if the integer part of the
:class:`~decimal.Decimal` instance is small. Patch by Sergey B Kirpichev.
82 changes: 50 additions & 32 deletions Modules/_decimal/_decimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -2336,15 +2336,16 @@ dec_from_long(decimal_state *state, PyTypeObject *type, PyObject *v,
}
if (export_long.digits) {
const PyLongLayout *layout = PyLong_GetNativeLayout();
uint32_t base = (uint32_t)1 << layout->bits_per_digit;
uint8_t sign = export_long.negative ? MPD_NEG : MPD_POS;
Py_ssize_t len = export_long.ndigits;

assert(layout->bits_per_digit <= 32);
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);

uint32_t base = (uint32_t)1 << layout->bits_per_digit;
uint8_t sign = export_long.negative ? MPD_NEG : MPD_POS;
Py_ssize_t len = export_long.ndigits;

if (layout->digit_size == 4) {
mpd_qimport_u32(MPD(dec), export_long.digits, len, sign,
base, ctx, status);
Expand Down Expand Up @@ -3642,13 +3643,6 @@ dec_format(PyObject *dec, PyObject *args)
static PyObject *
dec_as_long(PyObject *dec, PyObject *context, int round)
{
PyLongObject *pylong;
digit *ob_digit;
size_t n;
mpd_t *x;
mpd_context_t workctx;
uint32_t status = 0;

if (mpd_isspecial(MPD(dec))) {
if (mpd_isnan(MPD(dec))) {
PyErr_SetString(PyExc_ValueError,
Expand All @@ -3661,12 +3655,16 @@ dec_as_long(PyObject *dec, PyObject *context, int round)
return NULL;
}

x = mpd_qnew();
mpd_t *x = mpd_qnew();

if (x == NULL) {
PyErr_NoMemory();
return NULL;
}
workctx = *CTX(context);

mpd_context_t workctx = *CTX(context);
uint32_t status = 0;

workctx.round = round;
mpd_qround_to_int(x, MPD(dec), &workctx, &status);
if (dec_addstatus(context, status)) {
Expand All @@ -3675,34 +3673,54 @@ dec_as_long(PyObject *dec, PyObject *context, int round)
}

status = 0;
ob_digit = NULL;
#if PYLONG_BITS_IN_DIGIT == 30
n = mpd_qexport_u32(&ob_digit, 0, PyLong_BASE, x, &status);
#elif PYLONG_BITS_IN_DIGIT == 15
n = mpd_qexport_u16(&ob_digit, 0, PyLong_BASE, x, &status);
#else
#error "PYLONG_BITS_IN_DIGIT should be 15 or 30"
#endif
int64_t val = mpd_qget_i64(x, &status);

if (n == SIZE_MAX) {
PyErr_NoMemory();
if (!status) {
mpd_del(x);
return PyLong_FromInt64(val);
}
assert(!mpd_iszero(x));

const PyLongLayout *layout = PyLong_GetNativeLayout();

assert(layout->bits_per_digit < 32);
picnixz marked this conversation as resolved.
Show resolved Hide resolved
assert(layout->digits_order == -1);
assert(layout->digit_endianness == (PY_LITTLE_ENDIAN ? -1 : 1));
assert(layout->digit_size == 2 || layout->digit_size == 4);

uint32_t base = (uint32_t)1 << layout->bits_per_digit;
size_t n, len = mpd_sizeinbase(x, base);
Copy link
Member

@picnixz picnixz Jan 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
size_t n, len = mpd_sizeinbase(x, base);
size_t len = mpd_sizeinbase(x, base);

Do you want maybe to throw a comment saying that the result won't be underestimated + a link to the GH comment you made (this one: #127925 (comment))?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment like this right below, you edited. Do we need something more? Do we need move it to a different place?

I would prefer to keep n declaration here, it's not too far from actual usage.

void *digits;
PyLongWriter *writer = PyLongWriter_Create(mpd_isnegative(x), len, &digits);

if (writer == NULL) {
mpd_del(x);
return NULL;
}

if (n == 1) {
sdigit val = mpd_arith_sign(x) * ob_digit[0];
mpd_free(ob_digit);
status = 0;
/* The mpd_qexport_*() functions used here assume that no resizing occurs,
that is, 'len' was obtained via mpd_sizeinbase(). We also fill 'digits'
with zeros first since 'len' can be overestimated. */
if (layout->digit_size == 4) {
skirpichev marked this conversation as resolved.
Show resolved Hide resolved
memset(digits, 0, 4*len);
n = mpd_qexport_u32((uint32_t **)&digits, len, base, x, &status);
}
else {
memset(digits, 0, 2*len);
n = mpd_qexport_u16((uint16_t **)&digits, len, base, x, &status);
}

if (n == SIZE_MAX) {
PyErr_NoMemory();
PyLongWriter_Discard(writer);
mpd_del(x);
return PyLong_FromLong(val);
return NULL;
}

assert(n > 0);
assert(!mpd_iszero(x));
pylong = _PyLong_FromDigits(mpd_isnegative(x), n, ob_digit);
mpd_free(ob_digit);
assert(n <= len);
mpd_del(x);
return (PyObject *) pylong;
return PyLongWriter_Finish(writer);
}

/* Convert a Decimal to its exact integer ratio representation. */
Expand Down
Loading