Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Merged
carljm merged 23 commits intopython:mainfromcarljm:superopt
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
Show all changes
23 commits
Select commitHold shift + click to select a range
0a0ebe2
gh-87729: add instruction for faster zero-arg super()
carljmApr 10, 2023
f14a3bf
add news entry
carljmApr 13, 2023
5625e73
document LOAD_ZERO_SUPER_ATTR
carljmApr 13, 2023
0775efe
fix gdb test_wrapper_call
carljmApr 13, 2023
f229b5b
optimize 2-arg super also
carljmApr 13, 2023
626999d
fix incompatible assignment
carljmApr 13, 2023
9384106
Merge branch 'main' into superopt
carljmApr 13, 2023
92c943b
fix bad first arg
carljmApr 13, 2023
775ed0f
Apply suggestions from code review
carljmApr 14, 2023
2077f1a
don't unnecessarily re-find args in error case
carljmApr 14, 2023
64da49f
Merge branch 'main' into superopt
carljmApr 14, 2023
4759ad9
update generated cases
carljmApr 14, 2023
94399c2
fix incompatible types
carljmApr 14, 2023
5136459
Merge branch 'main' into superopt
carljmApr 17, 2023
82945b2
review comments
carljmApr 17, 2023
3a3cb74
update generated cases with new comment
carljmApr 17, 2023
e4466a7
simplify oparg & 2 handling
carljmApr 19, 2023
5c0a21c
Merge branch 'main' into superopt
carljmApr 19, 2023
f161268
cleanup and clarification
carljmApr 20, 2023
df442c0
move __class__ special case out of the fast path
carljmApr 20, 2023
19b8025
Merge branch 'main' into superopt
carljmApr 24, 2023
0de5bc6
Merge branch 'main' into superopt
carljmApr 24, 2023
dbe1665
Merge branch 'main' into superopt
carljmApr 24, 2023
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
PrevPrevious commit
NextNext commit
move __class__ special case out of the fast path
  • Loading branch information
@carljm
carljm committedApr 20, 2023
commitdf442c0deb2f3dec8bfbd01fd97a5e00080b4096
17 changes: 17 additions & 0 deletionsLib/test/test_super.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -393,6 +393,23 @@ def method(self):
with self.assertRaisesRegex(TypeError, "argument 1 must be a type"):
C().method()

def test_super___class__(self):
class C:
def method(self):
return super().__class__

self.assertEqual(C().method(), super)

def test_super_subclass___class__(self):
class mysuper(super):
pass

class C:
def method(self):
return mysuper(C, self).__class__

self.assertEqual(C().method(), mysuper)


if __name__ == "__main__":
unittest.main()
15 changes: 8 additions & 7 deletionsObjects/typeobject.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -9370,13 +9370,6 @@ do_super_lookup(superobject *su, PyTypeObject *su_type, PyObject *su_obj,
if (su_obj_type == NULL)
goto skip;

/* We want __class__ to return the class of the super object
(i.e. super, or a subclass), not the class of su->obj. */
if (PyUnicode_Check(name) &&
PyUnicode_GET_LENGTH(name) == 9 &&
_PyUnicode_Equal(name, &_Py_ID(__class__)))
goto skip;

mro = su_obj_type->tp_mro;
if (mro == NULL)
goto skip;
Expand DownExpand Up@@ -9452,6 +9445,14 @@ static PyObject *
super_getattro(PyObject *self, PyObject *name)
{
superobject *su = (superobject *)self;

/* We want __class__ to return the class of the super object
(i.e. super, or a subclass), not the class of su->obj. */
if (PyUnicode_Check(name) &&
PyUnicode_GET_LENGTH(name) == 9 &&
_PyUnicode_Equal(name, &_Py_ID(__class__)))
return PyObject_GenericGetAttr(self, name);

return do_super_lookup(su, su->type, su->obj, su->obj_type, name, NULL);
}

Expand Down
10 changes: 6 additions & 4 deletionsPython/compile.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -4237,18 +4237,20 @@ is_import_originated(struct compiler *c, expr_ty e)
}

static int
can_optimize_super_call(struct compiler *c, expr_tye)
can_optimize_super_call(struct compiler *c, expr_tyattr)
{
expr_ty e = attr->v.Attribute.value;
if (e->kind != Call_kind ||
e->v.Call.func->kind != Name_kind ||
!_PyUnicode_EqualToASCIIString(e->v.Call.func->v.Name.id, "super") ||
_PyUnicode_EqualToASCIIString(attr->v.Attribute.attr, "__class__") ||
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 todetect statically-visible shadowing of 'super' name
// detect statically-visible shadowing of 'super' name
int scope = _PyST_GetScope(c->u->u_ste, super_name);
if (scope != GLOBAL_IMPLICIT) {
return 0;
Expand DownExpand Up@@ -4388,7 +4390,7 @@ 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)) {
if (can_optimize_super_call(c, meth)) {
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;
Expand DownExpand Up@@ -5406,7 +5408,7 @@ 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)) {
if (e->v.Attribute.ctx == Load && can_optimize_super_call(c, e)) {
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;
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp