10000 gh-114682: Improve performance of module state access in `_decimal` by neonene · Pull Request #115401 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-114682: Improve performance of module state access in _decimal #115401

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

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
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
Prev Previous commit
Do not carry module state by Decimal
  • Loading branch information
neonene committed Feb 17, 2024
commit c829ce6dcf92b7b70f29588cd0d532ffbce3286c
69 changes: 31 additions & 38 deletions Modules/_decimal/_decimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ typedef struct {
Py_hash_t hash;
mpd_t dec;
mpd_uint_t data[_Py_DEC_MINALLOC];
decimal_state *mstate;
} PyDecObject;

typedef struct {
Expand Down Expand Up @@ -223,14 +222,6 @@ typedef struct {
#define CTX(v) (&((PyDecContextObject *)v)->ctx)
#define CtxCaps(v) (((PyDecContextObject *)v)->capitals)

static inline decimal_state *
dec_get_module_state(PyObject *v) {
decimal_state *state = ((PyDecObject *)v)->mstate;
assert(state != NULL);
assert(PyDec_Check(state, v));
return state;
}

static inline decimal_state *
ctx_get_module_state(PyObject *v) {
decimal_state *state = ((PyDecContextObject *)v)->mstate;
Expand Down Expand Up @@ -2060,7 +2051,6 @@ PyDecType_New(decimal_state *state, PyTypeObject *type)
}

dec->hash = -1;
dec->mstate = state;

MPD(dec)->flags = MPD_STATIC|MPD_STATIC_DATA;
MPD(dec)->exp = 0;
Expand Down Expand Up @@ -3304,7 +3294,7 @@ dec_str(PyObject *dec)
mpd_ssize_t size;
char *cp;

decimal_state *state = dec_get_module_state(dec);
decimal_state *state = get_module_state_by_def(Py_TYPE(dec));
CURRENT_CONTEXT(state, context);
size = mpd_to_sci_size(&cp, MPD(dec), CtxCaps(context));
if (size < 0) {
Expand All @@ -3323,7 +3313,7 @@ dec_repr(PyObject *dec)
{
PyObject *res, *context;
char *cp;
decimal_state *state = dec_get_module_state(dec);
decimal_state *state = get_module_state_by_def(Py_TYPE(dec));
CURRENT_CONTEXT(state, context);
cp = mpd_to_sci(MPD(dec), CtxCaps(context));
if (cp == NULL) {
Expand Down Expand Up @@ -3472,7 +3462,7 @@ dec_format(PyObject *dec, PyObject *args)
Py_ssize_t size;


decimal_state *state = dec_get_module_state(dec);
decimal_state *state = get_module_state_by_def(Py_TYPE(dec));
CURRENT_CONTEXT(state, context);
if (!PyArg_ParseTuple(args, "O|O", &fmtarg, &override)) {
return NULL;
Expand Down Expand Up @@ -3690,7 +3680,7 @@ dec_as_integer_ratio(PyObject *self, PyObject *args UNUSED)
return NULL;
}

decimal_state *state = dec_get_module_state(self);
decimal_state *state = get_module_state_by_def(Py_TYPE(self));
CURRENT_CONTEXT(state, context);

tmp = dec_alloc(state);
Expand Down Expand Up @@ -3783,7 +3773,7 @@ PyDec_ToIntegralValue(PyObject *dec, PyObject *args, PyObject *kwds)
&rounding, &context)) {
return NULL;
}
decimal_state *state = dec_get_module_state(dec);
decimal_state *state = get_module_state_by_def(Py_TYPE(dec));
CONTEXT_CHECK_VA(state, context);

workctx = *CTX(context);
Expand Down Expand Up @@ -3825,7 +3815,7 @@ PyDec_ToIntegralExact(PyObject *dec, PyObject *args, PyObject *kwds)
&rounding, &context)) {
return NULL;
}
decimal_state *state = dec_get_module_state(dec);
decimal_state *state = get_module_state_by_def(Py_TYPE(dec));
CONTEXT_CHECK_VA(state, context);

workctx = *CTX(context);
Expand Down Expand Up @@ -3893,7 +3883,7 @@ PyDec_Round(PyObject *dec, PyObject *args)
uint32_t status = 0;
PyObject *context;

decimal_state *state = dec_get_module_state(dec);
decimal_state *state = get_module_state_by_def(Py_TYPE(dec));
CURRENT_CONTEXT(state, context);
if (!PyArg_ParseTuple(args, "|O", &x)) {
return NULL;
Expand Down Expand Up @@ -4015,7 +4005,7 @@ PyDec_AsTuple(PyObject *dec, PyObject *dummy UNUSED)
}
}

decimal_state *state = dec_get_module_state(dec);
decimal_state *state = get_module_state_by_def(Py_TYPE(dec));
result = PyObject_CallFunctionObjArgs((PyObject *)state->DecimalTuple,
sign, coeff, expt, NULL);

Expand All @@ -4042,7 +4032,7 @@ nm_##MPDFUNC(PyObject *self) \
PyObject *context; \
uint32_t status = 0; \
\
decimal_state *state = dec_get_module_state(self); \
decimal_state *state = get_module_state_by_def(Py_TYPE(self)); \
CURRENT_CONTEXT(state, context); \
if ((result = dec_alloc(state)) == NULL) { \
return NULL; \
Expand Down Expand Up @@ -4108,7 +4098,7 @@ dec_##MPDFUNC(PyObject *self, PyObject *args, PyObject *kwds) \
&context)) { \
return NULL; \
} \
decimal_state *state = dec_get_module_state(self); \
decimal_state *state = get_module_state_by_def(Py_TYPE(self)); \
CONTEXT_CHECK_VA(state, context); \
\
return MPDFUNC(MPD(self), CTX(context)) ? incr_true() : incr_false(); \
Expand All @@ -4128,7 +4118,8 @@ dec_##MPDFUNC(PyObject *self, PyObject *args, PyObject *kwds) \
&context)) { \
return NULL; \
} \
decimal_state *state = dec_get_module_state(self); \
decimal_state *state = \
get_module_state_by_def(Py_TYPE(self)); \
CONTEXT_CHECK_VA(state, context); \
\
if ((result = dec_alloc(state)) == NULL) { \
Expand Down Expand Up @@ -4160,7 +4151,8 @@ dec_##MPDFUNC(PyObject *self, PyObject *args, PyObject *kwds) \
&other, &context)) { \
return NULL; \
} \
decimal_state *state = dec_get_module_state(self); \
decimal_state *state = \
get_module_state_by_def(Py_TYPE(self)); \
CONTEXT_CHECK_VA(state, context); \
CONVERT_BINOP_RAISE(&a, &b, self, other, context); \
\
Expand Down Expand Up @@ -4198,7 +4190,8 @@ dec_##MPDFUNC(PyObject *self, PyObject *args, PyObject *kwds) \
&other, &context)) { \
return NULL; \
} \
decimal_state *state = dec_get_module_state(self); \
decimal_state *state = \
get_module_state_by_def(Py_TYPE(self)); \
CONTEXT_CHECK_VA(state, context); \
CONVERT_BINOP_RAISE(&a, &b, self, other, context); \
\
Expand Down Expand Up @@ -4231,7 +4224,7 @@ dec_##MPDFUNC(PyObject *self, PyObject *args, PyObject *kwds) \
&other, &third, &context)) { \
return NULL; \
} \
decimal_state *state = dec_get_module_state(self); \
decimal_state *state = get_module_state_by_def(Py_TYPE(self)); \
CONTEXT_CHECK_VA(state, context); \
CONVERT_TERNOP_RAISE(&a, &b, &c, self, other, third, context); \
\
Expand Down Expand Up @@ -4274,7 +4267,7 @@ static PyObject *
nm_dec_as_long(PyObject *dec)
{
PyObject *context;
decimal_state *state = dec_get_module_state(dec);
decimal_state *state = get_module_state_by_def(Py_TYPE(dec));
CURRENT_CONTEXT(state, context);
return dec_as_long(dec, context, MPD_ROUND_DOWN);
}
Expand Down Expand Up @@ -4460,7 +4453,7 @@ _dec_mpd_radix(decimal_state *state)
static PyObject *
dec_mpd_radix(PyObject *self, PyObject *dummy UNUSED)
{
decimal_state *state = dec_get_module_state(self);
decimal_state *state = get_module_state_by_def(Py_TYPE(self));
return _dec_mpd_radix(state);
}

Expand All @@ -4470,7 +4463,7 @@ dec_mpd_qcopy_abs(PyObject *self, PyObject *dummy UNUSED)
PyObject *result;
uint32_t status = 0;

decimal_state *state = dec_get_module_state(self);
decimal_state *state = get_module_state_by_def(Py_TYPE(self));
if ((result = dec_alloc(state)) == NULL) {
return NULL;
}
Expand All @@ -4491,7 +4484,7 @@ dec_mpd_qcopy_negate(PyObject *self, PyObject *dummy UNUSED)
PyObject *result;
uint32_t status = 0;

decimal_state *state = dec_get_module_state(self);
decimal_state *state = get_module_state_by_def(Py_TYPE(self));
if ((result = dec_alloc(state)) == NULL) {
return NULL;
}
Expand Down Expand Up @@ -4521,7 +4514,7 @@ dec_mpd_class(PyObject *self, PyObject *args, PyObject *kwds)
&context)) {
return NULL;
}
decimal_state *state = dec_get_module_state(self);
decimal_state *state = get_module_state_by_def(Py_TYPE(self));
CONTEXT_CHECK_VA(state, context);

cp = mpd_class(MPD(self), CTX(context));
Expand All @@ -4541,7 +4534,7 @@ dec_mpd_to_eng(PyObject *self, PyObject *args, PyObject *kwds)
&context)) {
return NULL;
}
decimal_state *state = dec_get_module_state(self);
decimal_state *state = get_module_state_by_def(Py_TYPE(self));
CONTEXT_CHECK_VA(state, context);

size = mpd_to_eng_size(&s, MPD(self), CtxCaps(context));
Expand Down Expand Up @@ -4574,7 +4567,7 @@ dec_mpd_qcopy_sign(PyObject *self, PyObject *args, PyObject *kwds)
&other, &context)) {
return NULL;
}
decimal_state *state = dec_get_module_state(self);
decimal_state *state = get_module_state_by_def(Py_TYPE(self));
CONTEXT_CHECK_VA(state, context);
CONVERT_BINOP_RAISE(&a, &b, self, other, context);

Expand Down Expand Up @@ -4609,7 +4602,7 @@ dec_mpd_same_quantum(PyObject *self, PyObject *args, PyObject *kwds)
&other, &context)) {
return NULL;
}
decimal_state *state = dec_get_module_state(self);
decimal_state *state = get_module_state_by_def(Py_TYPE(self));
CONTEXT_CHECK_VA(state, context);
CONVERT_BINOP_RAISE(&a, &b, self, other, context);

Expand Down Expand Up @@ -4644,7 +4637,7 @@ dec_mpd_qquantize(PyObject *v, PyObject *args, PyObject *kwds)
&w, &rounding, &context)) {
return NULL;
}
decimal_state *state = dec_get_module_state(v);
decimal_state *state = get_module_state_by_def(Py_TYPE(v));
CONTEXT_CHECK_VA(state, context);

workctx = *CTX(context);
Expand Down Expand Up @@ -4689,7 +4682,7 @@ dec_richcompare(PyObject *v, PyObject *w, int op)
int a_issnan, b_issnan;
int r;

decimal_state *state = dec_get_module_state(v);
decimal_state *state = get_module_state_by_def(Py_TYPE(v));
assert(PyDec_Check(state, v));

CURRENT_CONTEXT(state, context);
Expand Down Expand Up @@ -4743,7 +4736,7 @@ dec_ceil(PyObject *self, PyObject *dummy UNUSED)
{
PyObject *context;

decimal_state *state = dec_get_module_state(self);
decimal_state *state = get_module_state_by_def(Py_TYPE(self));
CURRENT_CONTEXT(state, context);
return dec_as_long(self, context, MPD_ROUND_CEILING);
}
Expand Down Expand Up @@ -4782,7 +4775,7 @@ dec_floor(PyObject *self, PyObject *dummy UNUSED)
{
PyObject *context;

decimal_state *state = dec_get_module_state(self);
decimal_state *state = get_module_state_by_def(Py_TYPE(self));
CURRENT_CONTEXT(state, context);
return dec_as_long(self, context, MPD_ROUND_FLOOR);
}
Expand Down Expand Up @@ -4947,7 +4940,7 @@ dec_trunc(PyObject *self, PyObject *dummy UNUSED)
{
PyObject *context;

decimal_state *state = dec_get_module_state(self);
decimal_state *state = get_module_state_by_def(Py_TYPE(self));
CURRENT_CONTEXT(state, context);
return dec_as_long(self, context, MPD_ROUND_DOWN);
}
Expand All @@ -4964,7 +4957,7 @@ dec_imag(PyObject *self UNUSED, void *closure UNUSED)
{
PyObject *result;

decimal_state *state = dec_get_module_state(self);
decimal_state *state = get_module_state_by_def(Py_TYPE(self));
result = dec_alloc(state);
if (result == NULL) {
return NULL;
Expand Down
0