Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

gh-124688: _decimal: Get a module state from ctx objects for performance #124691

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 6 commits into from
Sep 28, 2024
Merged
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
Next Next commit
PyDecType_New, dec_alloc, dec_from_long
  • Loading branch information
neonene committed Sep 27, 2024
commit 00d23222336ad12c193e4fe02adf8ab154195df0
35 changes: 20 additions & 15 deletions 35 Modules/_decimal/_decimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -2034,11 +2034,10 @@ static PyType_Spec ctxmanager_spec = {
/******************************************************************************/

static PyObject *
PyDecType_New(PyTypeObject *type)
PyDecType_New(decimal_state *state, PyTypeObject *type)
{
PyDecObject *dec;

decimal_state *state = get_module_state_by_def(type);
if (type == state->PyDec_Type) {
dec = PyObject_GC_New(PyDecObject, state->PyDec_Type);
}
Expand All @@ -2064,7 +2063,7 @@ PyDecType_New(PyTypeObject *type)
assert(PyObject_GC_IsTracked((PyObject *)dec));
return (PyObject *)dec;
}
#define dec_alloc(st) PyDecType_New((st)->PyDec_Type)
#define dec_alloc(st) PyDecType_New(st, (st)->PyDec_Type)

static int
dec_traverse(PyObject *dec, visitproc visit, void *arg)
Expand Down Expand Up @@ -2167,7 +2166,8 @@ PyDecType_FromCString(PyTypeObject *type, const char *s,
PyObject *dec;
uint32_t status = 0;

dec = PyDecType_New(type);
decimal_state *state = get_module_state_from_ctx(context);
dec = PyDecType_New(state, type);
if (dec == NULL) {
return NULL;
}
Expand All @@ -2191,7 +2191,8 @@ PyDecType_FromCStringExact(PyTypeObject *type, const char *s,
uint32_t status = 0;
mpd_context_t maxctx;

dec = PyDecType_New(type);
decimal_state *state = get_module_state_from_ctx(context);
dec = PyDecType_New(state, type);
if (dec == NULL) {
return NULL;
}
Expand Down Expand Up @@ -2278,7 +2279,8 @@ PyDecType_FromSsize(PyTypeObject *type, mpd_ssize_t v, PyObject *context)
PyObject *dec;
uint32_t status = 0;

dec = PyDecType_New(type);
decimal_state *state = get_module_state_from_ctx(context);
dec = PyDecType_New(state, type);
if (dec == NULL) {
return NULL;
}
Expand All @@ -2299,7 +2301,8 @@ PyDecType_FromSsizeExact(PyTypeObject *type, mpd_ssize_t v, PyObject *context)
uint32_t status = 0;
mpd_context_t maxctx;

dec = PyDecType_New(type);
decimal_state *state = get_module_state_from_ctx(context);
dec = PyDecType_New(state, type);
if (dec == NULL) {
return NULL;
}
Expand All @@ -2317,13 +2320,13 @@ PyDecType_FromSsizeExact(PyTypeObject *type, mpd_ssize_t v, PyObject *context)
/* Convert from a PyLongObject. The context is not modified; flags set
during conversion are accumulated in the status parameter. */
static PyObject *
dec_from_long(PyTypeObject *type, PyObject *v,
dec_from_long(decimal_state *state, PyTypeObject *type, PyObject *v,
const mpd_context_t *ctx, uint32_t *status)
{
PyObject *dec;
PyLongObject *l = (PyLongObject *)v;

dec = PyDecType_New(type);
dec = PyDecType_New(state, type);
if (dec == NULL) {
return NULL;
}
Expand Down Expand Up @@ -2368,7 +2371,8 @@ PyDecType_FromLong(PyTypeObject *type, PyObject *v, PyObject *context)
return NULL;
}

dec = dec_from_long(type, v, CTX(context), &status);
decimal_state *state = get_module_state_from_ctx(context);
dec = dec_from_long(state, type, v, CTX(context), &status);
if (dec == NULL) {
return NULL;
}
Expand Down Expand Up @@ -2397,7 +2401,8 @@ PyDecType_FromLongExact(PyTypeObject *type, PyObject *v,
}

mpd_maxcontext(&maxctx);
dec = dec_from_long(type, v, &maxctx, &status);
decimal_state *state = get_module_state_from_ctx(context);
dec = dec_from_long(state, type, v, &maxctx, &status);
if (dec == NULL) {
return NULL;
}
Expand Down Expand Up @@ -2429,7 +2434,7 @@ PyDecType_FromFloatExact(PyTypeObject *type, PyObject *v,
mpd_t *d1, *d2;
uint32_t status = 0;
mpd_context_t maxctx;
decimal_state *state = get_module_state_by_def(type);
decimal_state *state = get_module_state_from_ctx(context);

#ifdef Py_DEBUG
assert(PyType_IsSubtype(type, state->PyDec_Type));
Expand All @@ -2450,7 +2455,7 @@ PyDecType_FromFloatExact(PyTypeObject *type, PyObject *v,
sign = (copysign(1.0, x) == 1.0) ? 0 : 1;

if (isnan(x) || isinf(x)) {
dec = PyDecType_New(type);
dec = PyDecType_New(state, type);
if (dec == NULL) {
return NULL;
}
Expand Down Expand Up @@ -2567,12 +2572,12 @@ PyDecType_FromDecimalExact(PyTypeObject *type, PyObject *v, PyObject *context)
PyObject *dec;
uint32_t status = 0;

decimal_state *state = get_module_state_by_def(type);
decimal_state *state = get_module_state_from_ctx(context);
if (type == state->PyDec_Type && PyDec_CheckExact(state, v)) {
return Py_NewRef(v);
}

dec = PyDecType_New(type);
dec = PyDecType_New(state, type);
if (dec == NULL) {
return NULL;
}
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.