Skip to content
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
replace _PyType_GetModuleByDef2() in _decimal.c
This will not be inlined, but hopefully 4% faster on Windows PGO.  The forceinline version was less efficient (2% faster).
  • Loading branch information
neonene authored Sep 20, 2024
commit c93c010184d47f85fffd995a04f953e492b1eb9d
20 changes: 14 additions & 6 deletions Modules/_decimal/_decimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ get_module_state(PyObject *mod)
}

static struct PyModuleDef _decimal_module;
static PyType_Spec dec_spec;

static inline decimal_state *
get_module_state_by_def(PyTypeObject *tp)
Expand All @@ -134,10 +135,16 @@ get_module_state_by_def(PyTypeObject *tp)
static inline decimal_state *
find_state_left_or_right(PyObject *left, PyObject *right)
{
PyObject *mod = _PyType_GetModuleByDef2(Py_TYPE(left), Py_TYPE(right),
&_decimal_module);
assert(mod != NULL);
return get_module_state(mod);
PyTypeObject *base;
if (PyType_GetBaseByToken(Py_TYPE(left), &dec_spec, &base) != 1) {
assert(!PyErr_Occurred());
PyType_GetBaseByToken(Py_TYPE(right), &dec_spec, &base);
}
assert(base != NULL);
void *state = _PyType_GetModuleState(base);
assert(state != NULL);
Py_DECREF(base);
return (decimal_state *)state;
}


Expand Down Expand Up @@ -745,7 +752,7 @@ signaldict_richcompare(PyObject *v, PyObject *w, int op)
{
PyObject *res = Py_NotImplemented;

decimal_state *state = find_state_left_or_right(v, w);
decimal_state *state = get_module_state_by_def(Py_TYPE(v));
assert(PyDecSignalDict_Check(state, v));

if ((SdFlagAddr(v) == NULL) || (SdFlagAddr(w) == NULL)) {
Expand Down Expand Up @@ -4653,7 +4660,7 @@ dec_richcompare(PyObject *v, PyObject *w, int op)
uint32_t status = 0;
int a_issnan, b_issnan;
int r;
decimal_state *state = find_state_left_or_right(v, w);
decimal_state *state = get_module_state_by_def(Py_TYPE(v));

#ifdef Py_DEBUG
assert(PyDec_Check(state, v));
Expand Down Expand Up @@ -5041,6 +5048,7 @@ static PyMethodDef dec_methods [] =
};

static PyType_Slot dec_slots[] = {
{Py_tp_token, Py_TP_USE_SPEC},
{Py_tp_dealloc, dec_dealloc},
{Py_tp_getattro, PyObject_GenericGetAttr},
{Py_tp_traverse, dec_traverse},
Expand Down