Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
gh-87729: add LOAD_SUPER_ATTR instruction for faster super()#103497
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 ourterms of service andprivacy 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 from1 commit
0a0ebe2f14a3bf5625e730775efef229b5b626999d938410692c943b775ed0f2077f1a64da49f4759ad994399c2513645982945b23a3cb74e4466a75c0a21cf161268df442c019b80250de5bc6dbe1665File 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
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow 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 |
|---|---|---|
| @@ -1551,8 +1551,8 @@ dummy_func( | ||
| PREDICT(JUMP_BACKWARD); | ||
| } | ||
| inst(LOAD_SUPER_ATTR, (global_super, class, self -- res2 if (oparg & 1), res)) { | ||
| PyObject *name = GETITEM(frame->f_code->co_names, oparg >>2); | ||
| if (global_super == (PyObject *)&PySuper_Type) { | ||
| int meth_found = 0; | ||
| Py_DECREF(global_super); | ||
| @@ -1571,7 +1571,13 @@ dummy_func( | ||
| Py_DECREF(self); | ||
| } | ||
| } else { | ||
| PyObject *super; | ||
| if (oparg & 2) { | ||
| super = PyObject_Vectorcall(global_super, NULL, 0, NULL); | ||
carljm marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| } else { | ||
| PyObject *stack[] = {class, self}; | ||
| super = PyObject_Vectorcall(global_super, stack, 2, NULL); | ||
| } | ||
carljm marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| DECREF_INPUTS(); | ||
| ERROR_IF(super == NULL, error); | ||
| res = PyObject_GetAttr(super, name); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -829,7 +829,9 @@ stack_effect(int opcode, int oparg, int jump) | ||
| case LOAD_METHOD: | ||
| return 1; | ||
| case LOAD_SUPER_METHOD: | ||
| case LOAD_ZERO_SUPER_METHOD: | ||
| case LOAD_ZERO_SUPER_ATTR: | ||
| return -1; | ||
| default: | ||
| return PY_INVALID_STACK_EFFECT; | ||
| @@ -1041,19 +1043,32 @@ compiler_addop_name(struct compiler_unit *u, location loc, | ||
| if (arg < 0) { | ||
| return ERROR; | ||
| } | ||
| if (opcode == LOAD_ATTR) { | ||
| arg <<= 1; | ||
| } | ||
| if (opcode == LOAD_METHOD) { | ||
| opcode = LOAD_ATTR; | ||
| arg <<= 1; | ||
| arg |= 1; | ||
| } | ||
| if (opcode == LOAD_SUPER_ATTR) { | ||
| arg <<= 2; | ||
| } | ||
| if (opcode == LOAD_SUPER_METHOD) { | ||
| opcode = LOAD_SUPER_ATTR; | ||
| arg <<= 2; | ||
| arg |= 1; | ||
| } | ||
| if (opcode == LOAD_ZERO_SUPER_ATTR) { | ||
| opcode = LOAD_SUPER_ATTR; | ||
| arg <<= 2; | ||
| arg |= 2; | ||
| } | ||
| if (opcode == LOAD_ZERO_SUPER_METHOD) { | ||
| opcode = LOAD_SUPER_ATTR; | ||
| arg <<= 2; | ||
carljm marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| arg |= 3; | ||
| } | ||
| return codegen_addop_i(&u->u_instr_sequence, opcode, arg, loc); | ||
| } | ||
| @@ -4222,15 +4237,15 @@ is_import_originated(struct compiler *c, expr_ty e) | ||
| } | ||
| static int | ||
| can_optimize_super_call(struct compiler *c, expr_ty e) | ||
| { | ||
| if (e->kind != Call_kind || | ||
| e->v.Call.func->kind != Name_kind || | ||
| !_PyUnicode_EqualToASCIIString(e->v.Call.func->v.Name.id, "super") || | ||
| asdl_seq_LEN(e->v.Call.keywords) != 0) { | ||
| return 0; | ||
| } | ||
| Py_ssize_t num_args = asdl_seq_LEN(e->v.Call.args); | ||
| PyObject *super_name = e->v.Call.func->v.Name.id; | ||
| // try to detect statically-visible shadowing of 'super' name | ||
| @@ -4242,6 +4257,24 @@ is_zero_arg_super_call(struct compiler *c, expr_ty e) | ||
| if (scope != 0) { | ||
| return 0; | ||
| } | ||
| if (num_args == 2) { | ||
| for (Py_ssize_t i = 0; i < num_args; i++) { | ||
| expr_ty elt = asdl_seq_GET(e->v.Call.args, i); | ||
| if (elt->kind == Starred_kind) { | ||
| return 0; | ||
| } | ||
| } | ||
| // exactly two non-starred args; we can just load | ||
| // the provided args | ||
| return 1; | ||
| } | ||
| if (num_args != 0) { | ||
| return 0; | ||
| } | ||
| // we need the following for zero-arg super(): | ||
| // enclosing function should have at least one argument | ||
| if (c->u->u_metadata.u_argcount == 0 && | ||
| c->u->u_metadata.u_posonlyargcount == 0) { | ||
| @@ -4255,13 +4288,19 @@ is_zero_arg_super_call(struct compiler *c, expr_ty e) | ||
| } | ||
| static int | ||
| load_args_for_super(struct compiler *c, expr_ty e) { | ||
| location loc = LOC(e); | ||
| // load super() global | ||
| PyObject *super_name = e->v.Call.func->v.Name.id; | ||
| RETURN_IF_ERROR(compiler_nameop(c, loc, super_name, Load)); | ||
| if (asdl_seq_LEN(e->v.Call.args) == 2) { | ||
| VISIT(c, expr, asdl_seq_GET(e->v.Call.args, 0)); | ||
| VISIT(c, expr, asdl_seq_GET(e->v.Call.args, 1)); | ||
| return SUCCESS; | ||
| } | ||
| // load __class__ cell | ||
| PyObject *name = &_Py_ID(__class__); | ||
| assert(get_ref_type(c, name) == FREE); | ||
| @@ -4349,9 +4388,11 @@ maybe_optimize_method_call(struct compiler *c, expr_ty e) | ||
| /* Alright, we can optimize the code. */ | ||
| location loc = LOC(meth); | ||
| if (can_optimize_super_call(c, meth->v.Attribute.value)) { | ||
| RETURN_IF_ERROR(load_args_for_super(c, meth->v.Attribute.value)); | ||
| int opcode = asdl_seq_LEN(meth->v.Attribute.value->v.Call.args) ? | ||
| LOAD_SUPER_METHOD : LOAD_ZERO_SUPER_METHOD; | ||
| ADDOP_NAME(c, loc, opcode, meth->v.Attribute.attr, names); | ||
| } else { | ||
| VISIT(c, expr, meth->v.Attribute.value); | ||
| loc = update_start_location_to_match_attr(c, loc, meth); | ||
| @@ -5365,9 +5406,11 @@ compiler_visit_expr1(struct compiler *c, expr_ty e) | ||
| return compiler_formatted_value(c, e); | ||
| /* The following exprs can be assignment targets. */ | ||
| case Attribute_kind: | ||
| if (e->v.Attribute.ctx == Load && can_optimize_super_call(c, e->v.Attribute.value)) { | ||
| RETURN_IF_ERROR(load_args_for_super(c, e->v.Attribute.value)); | ||
| int opcode = asdl_seq_LEN(e->v.Attribute.value->v.Call.args) ? | ||
| LOAD_SUPER_ATTR : LOAD_ZERO_SUPER_ATTR; | ||
| ADDOP_NAME(c, loc, opcode, e->v.Attribute.attr, names); | ||
| return SUCCESS; | ||
| } | ||
| VISIT(c, expr, e->v.Attribute.value); | ||
Uh oh!
There was an error while loading.Please reload this page.