KEMBAR78
[mypyc] Optimize calls to final classes by jairov4 · Pull Request #17886 · python/mypy · GitHub
Skip to content

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.

@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