Skip to content
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

[mypyc] Optimize calls to final classes #17886

Merged
merged 15 commits into from
Oct 14, 2024
Merged

Conversation

jairov4
Copy link
Contributor

@jairov4 jairov4 commented Oct 6, 2024

Fixes #9612

This change allows to gain more efficiency where classes are annotated with @final bypassing entirely the vtable for method calls and property accessors.

For example:
In

@final
class Vector:
    __slots__ = ("_x", "_y")
    def __init__(self, x: i32, y: i32) -> None:
        self._x = x
        self._y = y

    @property
    def y(self) -> i32:
        return self._y

def test_vector() -> None:
    v3 = Vector(1, 2)
    assert v3.y == 2

The call will produce:

...
cpy_r_r6 = CPyDef_Vector___y(cpy_r_r0);
...

Instead of:

...
cpy_r_r1 = CPY_GET_ATTR(cpy_r_r0, CPyType_Vector, 2, farm_rush___engine___vectors2___VectorObject, int32_t); /* y */
...

(which uses vtable)

@jairov4
Copy link
Contributor Author

jairov4 commented Oct 7, 2024

hello @JukkaL
can you review this PR?

Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! Looks good, just left a few minor comments.

mypyc/irbuild/classdef.py Outdated Show resolved Hide resolved
mypyc/codegen/emitfunc.py Outdated Show resolved Hide resolved
mypyc/test-data/run-classes.test Show resolved Hide resolved
@jairov4
Copy link
Contributor Author

jairov4 commented Oct 14, 2024

@JukkaL Hi, I have addressed your comments

Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@JukkaL JukkaL merged commit cbd96f9 into python:master Oct 14, 2024
13 checks passed
@jairov4 jairov4 deleted the mypyc-opt-final branch October 15, 2024 21:42
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.

[mypyc] support of @final types
2 participants