Skip to content

Commit 983842c

Browse files
committed
gh-148269: speed up default __reduce_ex__ call by ~20%
1 parent 1f36a51 commit 983842c

1 file changed

Lines changed: 13 additions & 21 deletions

File tree

Objects/typeobject.c

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8202,31 +8202,23 @@ static PyObject *
82028202
object___reduce_ex___impl(PyObject *self, int protocol)
82038203
/*[clinic end generated code: output=2e157766f6b50094 input=f326b43fb8a4c5ff]*/
82048204
{
8205-
PyObject *reduce;
8206-
if (PyObject_GetOptionalAttr(self, &_Py_ID(__reduce__), &reduce) < 0) {
8207-
return NULL;
8208-
}
8209-
if (reduce != NULL) {
8210-
PyObject *cls, *clsreduce;
8211-
int override;
8212-
8213-
cls = (PyObject *) Py_TYPE(self);
8214-
clsreduce = PyObject_GetAttr(cls, &_Py_ID(__reduce__));
8215-
if (clsreduce == NULL) {
8216-
Py_DECREF(reduce);
8217-
return NULL;
8218-
}
8219-
8205+
PyObject *clsreduce = _PyType_LookupRef(Py_TYPE(self), &_Py_ID(__reduce__));
8206+
if (clsreduce != NULL) {
82208207
PyInterpreterState *interp = _PyInterpreterState_GET();
8221-
override = (clsreduce != _Py_INTERP_CACHED_OBJECT(interp, objreduce));
8208+
int override = (clsreduce != _Py_INTERP_CACHED_OBJECT(interp, objreduce));
82228209
Py_DECREF(clsreduce);
8210+
82238211
if (override) {
8224-
PyObject *res = _PyObject_CallNoArgs(reduce);
8225-
Py_DECREF(reduce);
8226-
return res;
8212+
PyObject *reduce;
8213+
if (PyObject_GetOptionalAttr(self, &_Py_ID(__reduce__), &reduce) < 0) {
8214+
return NULL;
8215+
}
8216+
if (reduce != NULL) {
8217+
PyObject *res = _PyObject_CallNoArgs(reduce);
8218+
Py_DECREF(reduce);
8219+
return res;
8220+
}
82278221
}
8228-
else
8229-
Py_DECREF(reduce);
82308222
}
82318223

82328224
return _common_reduce(self, protocol);

0 commit comments

Comments
 (0)