Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
bpo-46564: Optimize super().meth() calls via adaptive superinstructions#30992
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
3b12d1b5b7a98defb0ae3e037a569d64819ac19aa11f9bb6cf4cd3f919880a9696a0e801202eeFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
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.
Uh oh!
There was an error while loading. Please reload this page.
| 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,31 @@ super_getattro(PyObject *self, PyObject *name) | ||||||
| Py_DECREF(mro); | ||||||
| skip: | ||||||
| return PyObject_GenericGetAttr(self, name); | ||||||
| /* only happens when using manual _PySuper_Lookup, never happens in super_getattro */ | ||||||
| if (su == NULL) { | ||||||
| PyErr_BadInternalCall(); | ||||||
| return 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 +9045,8 @@ super_descr_get(PyObject *self, PyObject *obj, PyObject *type) | ||||||
| } | ||||||
| } | ||||||
| static int | ||||||
| super_init_without_args(InterpreterFrame *cframe, PyCodeObject *co, | ||||||
| int | ||||||
| _PySuper_GetTypeArgs(InterpreterFrame *cframe, PyCodeObject *co, | ||||||
| PyTypeObject **type_p, PyObject **obj_p) | ||||||
Member 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) { | ||||||
| @@ -9102,7 +9136,8 @@ super_init(PyObject *self, PyObject *args, PyObject *kwds) | ||||||
| "super(): no current frame"); | ||||||
| return -1; | ||||||
| } | ||||||
| int res = super_init_without_args(cframe, cframe->f_code, &type, &obj); | ||||||
| int res = _PySuper_GetTypeArgs(cframe, cframe->f_code, &type, &obj); | ||||||
| if (res < 0) { | ||||||
| return -1; | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1361,6 +1361,7 @@ eval_frame_handle_pending(PyThreadState *tstate) | ||
| /* The integer overflow is checked by an assertion below. */ | ||
| #define INSTR_OFFSET() ((int)(next_instr - first_instr)) | ||
| #define NEXT_INSTR_OFFSET() ((int)(next_instr+1 - first_instr)) | ||
| #define NEXTOPARG() do { \ | ||
| _Py_CODEUNIT word = *next_instr; \ | ||
| opcode = _Py_OPCODE(word); \ | ||
| @@ -1486,6 +1487,9 @@ eval_frame_handle_pending(PyThreadState *tstate) | ||
| #define GET_CACHE() \ | ||
| _GetSpecializedCacheEntryForInstruction(first_instr, INSTR_OFFSET(), oparg) | ||
| # define GET_NEXT_INSTR_CACHE() \ | ||
| _GetSpecializedCacheEntryForInstruction(first_instr, NEXT_INSTR_OFFSET(), \ | ||
| _Py_OPARG(*next_instr)) | ||
| #define DEOPT_IF(cond, instname) if (cond) { goto instname ## _miss; } | ||
| @@ -4633,7 +4637,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; | ||
| } | ||
| @@ -5070,6 +5075,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.
Uh oh!There was an error while loading. Please reload this page. | ||
| assert(lm_adaptive == &GET_NEXT_INSTR_CACHE()[0].adaptive); | ||
| assert(call_shape.total_args == 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) { | ||
Member 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); | ||
Member 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?
| ||
| 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; | ||
| assert(lm_adaptive == &GET_NEXT_INSTR_CACHE()[0].adaptive); | ||
| assert(call_shape.total_args == 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.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
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_BinaryOpright below.