-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
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
bpo-46564: Optimize super().meth()
calls via adaptive superinstructions
#30992
Changes from 8 commits
3b12d1b
5b7a98d
efb0ae3
e037a56
9d64819
ac19aa1
1f9bb6c
f4cd3f9
19880a9
696a0e8
01202ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Method calls on :class:`super` are sped up. The 2-argument form, | ||
``super(type, obj).meth()`` is now nearly as fast as an equivalent | ||
``self.meth()`` call. The 0-argument form, while still slower, is still | ||
faster than in previous versions of CPython. Patch by Ken Jin, with | ||
additional contributions by Vladimir Matveev. |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -8846,16 +8846,18 @@ super_repr(PyObject *self) | |||||
"<super: <class '%s'>, NULL>", | ||||||
su->type ? su->type->tp_name : "NULL"); | ||||||
} | ||||||
/* Forward */ | ||||||
static PyTypeObject *supercheck(PyTypeObject *type, PyObject *obj); | ||||||
|
||||||
static PyObject * | ||||||
super_getattro(PyObject *self, PyObject *name) | ||||||
do_super_lookup(superobject *su, PyTypeObject *su_type, PyObject *su_obj, | ||||||
PyTypeObject *su_obj_type, PyObject *name, int *meth_found) | ||||||
{ | ||||||
superobject *su = (superobject *)self; | ||||||
PyTypeObject *starttype; | ||||||
PyObject *mro; | ||||||
Py_ssize_t i, n; | ||||||
|
||||||
starttype = su->obj_type; | ||||||
starttype = su_obj_type; | ||||||
if (starttype == NULL) | ||||||
goto skip; | ||||||
|
||||||
|
@@ -8875,7 +8877,7 @@ super_getattro(PyObject *self, PyObject *name) | |||||
|
||||||
/* No need to check the last one: it's gonna be skipped anyway. */ | ||||||
for (i = 0; i+1 < n; i++) { | ||||||
if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i)) | ||||||
if ((PyObject *)(su_type) == PyTuple_GET_ITEM(mro, i)) | ||||||
break; | ||||||
} | ||||||
i++; /* skip su->type (if any) */ | ||||||
|
@@ -8893,17 +8895,25 @@ super_getattro(PyObject *self, PyObject *name) | |||||
PyObject *res = PyDict_GetItemWithError(dict, name); | ||||||
if (res != NULL) { | ||||||
Py_INCREF(res); | ||||||
|
||||||
descrgetfunc f = Py_TYPE(res)->tp_descr_get; | ||||||
if (f != NULL) { | ||||||
PyObject *res2; | ||||||
res2 = f(res, | ||||||
/* Only pass 'obj' param if this is instance-mode super | ||||||
(See SF ID #743627) */ | ||||||
(su->obj == (PyObject *)starttype) ? NULL : su->obj, | ||||||
(PyObject *)starttype); | ||||||
Py_DECREF(res); | ||||||
res = res2; | ||||||
if (meth_found && | ||||||
_PyType_HasFeature(Py_TYPE(res), Py_TPFLAGS_METHOD_DESCRIPTOR)) { | ||||||
*meth_found = 1; | ||||||
} | ||||||
else { | ||||||
if (meth_found) { | ||||||
*meth_found = 0; | ||||||
} | ||||||
descrgetfunc f = Py_TYPE(res)->tp_descr_get; | ||||||
if (f != NULL) { | ||||||
PyObject *res2; | ||||||
res2 = f(res, | ||||||
/* Only pass 'obj' param if this is instance-mode super | ||||||
(See SF ID #743627) */ | ||||||
(su_obj == (PyObject *)starttype) ? NULL : su_obj, | ||||||
(PyObject *)starttype); | ||||||
Py_DECREF(res); | ||||||
res = res2; | ||||||
} | ||||||
} | ||||||
|
||||||
Py_DECREF(mro); | ||||||
|
@@ -8919,7 +8929,27 @@ super_getattro(PyObject *self, PyObject *name) | |||||
Py_DECREF(mro); | ||||||
|
||||||
skip: | ||||||
return PyObject_GenericGetAttr(self, name); | ||||||
assert(su != NULL); | ||||||
return PyObject_GenericGetAttr((PyObject *)su, name); | ||||||
} | ||||||
|
||||||
static PyObject * | ||||||
super_getattro(PyObject *self, PyObject *name) | ||||||
{ | ||||||
superobject *su = (superobject *)self; | ||||||
return do_super_lookup(su, su->type, su->obj, su->obj_type, name, NULL); | ||||||
} | ||||||
|
||||||
PyObject * | ||||||
_PySuper_Lookup(PyTypeObject *su_type, PyObject *su_obj, PyObject *name, int *meth_found) | ||||||
{ | ||||||
PyTypeObject *starttype = supercheck(su_type, su_obj); | ||||||
if (starttype == NULL) { | ||||||
return NULL; | ||||||
} | ||||||
PyObject *res = do_super_lookup(NULL, su_type, su_obj, starttype, name, meth_found); | ||||||
Py_DECREF(starttype); | ||||||
return res; | ||||||
} | ||||||
|
||||||
static PyTypeObject * | ||||||
|
@@ -9011,8 +9041,8 @@ super_descr_get(PyObject *self, PyObject *obj, PyObject *type) | |||||
} | ||||||
} | ||||||
|
||||||
static int | ||||||
super_init_without_args(PyFrameObject *f, PyCodeObject *co, | ||||||
int | ||||||
_PySuper_GetTypeArgs(InterpreterFrame *f, PyCodeObject *co, | ||||||
PyTypeObject **type_p, PyObject **obj_p) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
The line was aligned with an opening parenthesis of a parameter list. |
||||||
{ | ||||||
if (co->co_argcount == 0) { | ||||||
|
@@ -9021,13 +9051,13 @@ super_init_without_args(PyFrameObject *f, PyCodeObject *co, | |||||
return -1; | ||||||
} | ||||||
|
||||||
assert(f->f_frame->f_code->co_nlocalsplus > 0); | ||||||
PyObject *firstarg = _PyFrame_GetLocalsArray(f->f_frame)[0]; | ||||||
assert(f->f_code->co_nlocalsplus > 0); | ||||||
PyObject *firstarg = _PyFrame_GetLocalsArray(f)[0]; | ||||||
// The first argument might be a cell. | ||||||
if (firstarg != NULL && (_PyLocals_GetKind(co->co_localspluskinds, 0) & CO_FAST_CELL)) { | ||||||
// "firstarg" is a cell here unless (very unlikely) super() | ||||||
// was called from the C-API before the first MAKE_CELL op. | ||||||
if (f->f_frame->f_lasti >= 0) { | ||||||
if (f->f_lasti >= 0) { | ||||||
assert(_Py_OPCODE(*co->co_firstinstr) == MAKE_CELL || _Py_OPCODE(*co->co_firstinstr) == COPY_FREE_VARS); | ||||||
assert(PyCell_Check(firstarg)); | ||||||
firstarg = PyCell_GET(firstarg); | ||||||
|
@@ -9047,7 +9077,7 @@ super_init_without_args(PyFrameObject *f, PyCodeObject *co, | |||||
PyObject *name = PyTuple_GET_ITEM(co->co_localsplusnames, i); | ||||||
assert(PyUnicode_Check(name)); | ||||||
if (_PyUnicode_EqualToASCIIId(name, &PyId___class__)) { | ||||||
PyObject *cell = _PyFrame_GetLocalsArray(f->f_frame)[i]; | ||||||
PyObject *cell = _PyFrame_GetLocalsArray(f)[i]; | ||||||
if (cell == NULL || !PyCell_Check(cell)) { | ||||||
PyErr_SetString(PyExc_RuntimeError, | ||||||
"super(): bad __class__ cell"); | ||||||
|
@@ -9096,17 +9126,14 @@ super_init(PyObject *self, PyObject *args, PyObject *kwds) | |||||
/* Call super(), without args -- fill in from __class__ | ||||||
and first local variable on the stack. */ | ||||||
PyThreadState *tstate = _PyThreadState_GET(); | ||||||
PyFrameObject *frame = PyThreadState_GetFrame(tstate); | ||||||
InterpreterFrame *frame = tstate->cframe->current_frame; | ||||||
if (frame == NULL) { | ||||||
PyErr_SetString(PyExc_RuntimeError, | ||||||
"super(): no current frame"); | ||||||
return -1; | ||||||
} | ||||||
|
||||||
PyCodeObject *code = PyFrame_GetCode(frame); | ||||||
int res = super_init_without_args(frame, code, &type, &obj); | ||||||
Py_DECREF(frame); | ||||||
Py_DECREF(code); | ||||||
int res = _PySuper_GetTypeArgs(frame, frame->f_code, &type, &obj); | ||||||
|
||||||
if (res < 0) { | ||||||
return -1; | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4633,7 +4633,8 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr | |
int nargs = call_shape.total_args; | ||
int err = _Py_Specialize_CallNoKw( | ||
call_shape.callable, next_instr, nargs, | ||
call_shape.kwnames, cache, BUILTINS()); | ||
call_shape.kwnames, cache, BUILTINS(), | ||
stack_pointer, frame, names); | ||
if (err < 0) { | ||
goto error; | ||
} | ||
|
@@ -5069,6 +5070,86 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr | |
DISPATCH(); | ||
} | ||
|
||
TARGET(CALL_NO_KW_SUPER_0__LOAD_METHOD_CACHED) { | ||
/* super().meth */ | ||
assert(_Py_OPCODE(next_instr[0]) == LOAD_METHOD_ADAPTIVE); | ||
assert(_Py_OPCODE(next_instr[-2]) != PRECALL_METHOD); | ||
SpecializedCacheEntry *caches = GET_CACHE(); | ||
_PyAdaptiveEntry *cache0 = &caches[0].adaptive; | ||
_PyObjectCache *cache1 = &caches[-1].obj; | ||
_PyAdaptiveEntry *lm_adaptive = &caches[-2].adaptive; | ||
Fidget-Spinner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
int nargs = call_shape.total_args; | ||
assert(nargs == 0); | ||
|
||
/* CALL_NO_KW_SUPER */ | ||
PyObject *su_obj; | ||
PyTypeObject *su_type; | ||
PyObject *meth; | ||
PyObject *super_callable = TOP(); | ||
|
||
DEOPT_IF(_PyType_CAST(super_callable) != &PySuper_Type, CALL); | ||
/* super() - zero argument form */ | ||
if (_PySuper_GetTypeArgs(frame, frame->f_code, &su_type, &su_obj) < 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't we do this at specialization time? The number of locals, the index of "self", and whether it is a cell are all known then. Likewise the nature of |
||
PyErr_Clear(); | ||
DEOPT_IF(1, CALL); | ||
} | ||
assert(su_obj != NULL); | ||
DEOPT_IF(lm_adaptive->version != Py_TYPE(su_obj)->tp_version_tag, CALL); | ||
DEOPT_IF(cache0->version != su_type->tp_version_tag, CALL); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When can this fail? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wanted assurance that |
||
STAT_INC(CALL, hit); | ||
|
||
/* LOAD_METHOD_CACHED */ | ||
meth = cache1->obj; | ||
assert(meth != NULL && _PyType_HasFeature(Py_TYPE(meth), Py_TPFLAGS_METHOD_DESCRIPTOR)); | ||
Py_INCREF(meth); | ||
SET_TOP(meth); | ||
Py_INCREF(su_obj); | ||
PUSH(su_obj); | ||
|
||
Py_DECREF(super_callable); | ||
next_instr++; | ||
DISPATCH(); | ||
} | ||
|
||
TARGET(CALL_NO_KW_SUPER_2__LOAD_METHOD_CACHED) { | ||
/* super(type, obj).meth */ | ||
assert(_Py_OPCODE(next_instr[0]) == LOAD_METHOD_ADAPTIVE); | ||
assert(_Py_OPCODE(next_instr[-2]) != PRECALL_METHOD); | ||
SpecializedCacheEntry *caches = GET_CACHE(); | ||
_PyAdaptiveEntry *cache0 = &caches[0].adaptive; | ||
_PyObjectCache *cache1 = &caches[-1].obj; | ||
_PyAdaptiveEntry *lm_adaptive = &caches[-2].adaptive; | ||
int nargs = call_shape.total_args; | ||
assert(nargs == 2); | ||
assert(call_shape.kwnames == NULL); | ||
|
||
/* CALL_NO_KW_SUPER */ | ||
/* super(type, obj) - two argument form */ | ||
PyObject *su_obj = TOP(); | ||
PyTypeObject *su_type = _PyType_CAST(SECOND()); | ||
PyObject *super_callable = THIRD(); | ||
PyObject *meth; | ||
|
||
DEOPT_IF(_PyType_CAST(super_callable) != &PySuper_Type, CALL); | ||
assert(su_obj != NULL); | ||
DEOPT_IF(lm_adaptive->version != Py_TYPE(su_obj)->tp_version_tag, CALL); | ||
DEOPT_IF(cache0->version != su_type->tp_version_tag, CALL); | ||
STAT_INC(CALL, hit); | ||
|
||
(void)(POP()); | ||
/* LOAD_METHOD_CACHED */ | ||
meth = cache1->obj; | ||
assert(meth != NULL && _PyType_HasFeature(Py_TYPE(meth), Py_TPFLAGS_METHOD_DESCRIPTOR)); | ||
Py_INCREF(meth); | ||
SET_SECOND(meth); | ||
SET_TOP(su_obj); | ||
|
||
Py_DECREF(super_callable); | ||
Py_DECREF(su_type); | ||
next_instr++; | ||
DISPATCH(); | ||
} | ||
|
||
TARGET(CALL_FUNCTION_EX) { | ||
PREDICTED(CALL_FUNCTION_EX); | ||
PyObject *func, *callargs, *kwargs = NULL, *result; | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as in a removed line, or even:
as in
_Py_Specialize_BinaryOp
right below.