Skip to content

[mypyc] Optimize classmethod calls via cls#14789

Merged
JukkaL merged 6 commits into
masterfrom
mypyc-classmethod
Mar 2, 2023
Merged

[mypyc] Optimize classmethod calls via cls#14789
JukkaL merged 6 commits into
masterfrom
mypyc-classmethod

Conversation

@JukkaL

@JukkaL JukkaL commented Feb 26, 2023

Copy link
Copy Markdown
Collaborator

If the class has no subclasses, we can statically bind the call target:

class C:
    @classmethod
    def f(cls) -> int:
        return cls.g()  # This can be statically bound, same as C.g()

    @classmethod
    def g(cls) -> int:
        return 1

For this to be safe, also reject assignments to the "cls" argument in
classmethods in compiled code.

This makes the deltablue benchmark about 11% faster.

If the class has no subclasses, we can statically bind the call target.

For this to be safe, also reject assignments to the "cls" argument in
classmethods in compiled code.

This makes the deltablue benchmark about 11% faster.
@JukkaL
JukkaL requested a review from msullivan February 26, 2023 17:33
@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@JukkaL

JukkaL commented Feb 26, 2023

Copy link
Copy Markdown
Collaborator Author

Note that we can't easily use vtables to call class methods, since vtables are accessible through instances, not type objects.

@JukkaL
JukkaL merged commit 43883fa into master Mar 2, 2023
@JukkaL
JukkaL deleted the mypyc-classmethod branch March 2, 2023 10:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants