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/export API for ints (PEP 757) #127925

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 18 commits
Commits
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). That offer some speedup, if the integer part of the
:class:`~decimal.Decimal` instance is small. Patch by Sergey B Kirpichev.
skirpichev marked this conversation as resolved.
Show resolved Hide resolved
106 changes: 62 additions & 44 deletions Modules/_decimal/_decimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -2323,38 +2322,46 @@
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;
}

uint8_t sign = _PyLong_IsNegative(l) ? MPD_NEG : MPD_POS;
PyLongExport export_long;

if (_PyLong_IsCompact(l)) {
_dec_settriple(dec, sign, l->long_value.ob_digit[0], 0);
mpd_qfinalize(MPD(dec), ctx, status);
return dec;
if (PyLong_Export(v, &export_long) == -1) {
Py_DECREF(dec);
return NULL;
}
size_t len = _PyLong_DigitCount(l);
if (export_long.digits) {
const PyLongLayout *layout = PyLong_GetNativeLayout();
const uint32_t base = 1 << layout->bits_per_digit;
skirpichev marked this conversation as resolved.
Show resolved Hide resolved
const uint8_t sign = export_long.negative ? MPD_NEG : MPD_POS;
const Py_ssize_t len = export_long.ndigits;
skirpichev marked this conversation as resolved.
Show resolved Hide resolved

#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
if (base > UINT16_MAX) {
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);
}
else {
const int64_t value = export_long.value;

if (-UINT32_MAX <= value && value <= UINT32_MAX) {

Check warning on line 2356 in Modules/_decimal/_decimal.c

View workflow job for this annotation

GitHub Actions / Windows / build and test (arm64)

unary minus operator applied to unsigned type, result still unsigned [D:\a\cpython\cpython\PCbuild\_decimal.vcxproj]

Check warning on line 2356 in Modules/_decimal/_decimal.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build and test (arm64)

unary minus operator applied to unsigned type, result still unsigned [D:\a\cpython\cpython\PCbuild\_decimal.vcxproj]

Check warning on line 2356 in Modules/_decimal/_decimal.c

View workflow job for this annotation

GitHub Actions / Windows / build and test (x64)

unary minus operator applied to unsigned type, result still unsigned [D:\a\cpython\cpython\PCbuild\_decimal.vcxproj]

Check warning on line 2356 in Modules/_decimal/_decimal.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build and test (x64)

unary minus operator applied to unsigned type, result still unsigned [D:\a\cpython\cpython\PCbuild\_decimal.vcxproj]
skirpichev marked this conversation as resolved.
Show resolved Hide resolved
_dec_settriple(dec, value < 0 ? MPD_NEG : MPD_POS,
(uint32_t)Py_ABS(value), 0);
mpd_qfinalize(MPD(dec), ctx, status);
}
else {
mpd_qset_i64(MPD(dec), value, ctx, status);
}
}
return dec;
}

Expand Down Expand Up @@ -3639,8 +3646,7 @@
static PyObject *
dec_as_long(PyObject *dec, PyObject *context, int round)
{
PyLongObject *pylong;
digit *ob_digit;
void *digits;
size_t n;
mpd_t *x;
mpd_context_t workctx;
Expand Down Expand Up @@ -3672,34 +3678,46 @@
}

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

if (n == SIZE_MAX) {
PyErr_NoMemory();
int64_t val = mpd_qget_i64(x, &status);
if (!status) {
mpd_del(x);
return PyLong_FromInt64(val);
}

const PyLongLayout *layout = PyLong_GetNativeLayout();
const uint32_t base = 1 << layout->bits_per_digit;
skirpichev marked this conversation as resolved.
Show resolved Hide resolved
size_t len = mpd_sizeinbase(x, base);
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;
/* mpd_sizeinbase can overestimate size by 1 digit, set it first to zero. */
if (base > UINT16_MAX) {
((uint32_t *)digits)[len - 1] = 0;
n = mpd_qexport_u32((uint32_t **)&digits, len, base, x, &status);
}
else {
((uint16_t *)digits)[len - 1] = 0;
n = mpd_qexport_u16((uint16_t **)&digits, len, base, x, &status);
}
/* mpd_qexport_*() functions above used with assumption, that no
resizing occur, i.e. len was obtained by a call to mpd_sizeinbase. */
assert(n == len || n == len - 1);
skirpichev marked this conversation as resolved.
Show resolved Hide resolved

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

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);
mpd_del(x);
return (PyObject *) pylong;
return PyLongWriter_Finish(writer);
}

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