Skip to content

Conversation

@BobTheBuidler
Copy link
Contributor

When a native class defines __len__, we don't need to use python's dispatch for a vtable lookup. Instead, we can call the C function directly.

@BobTheBuidler BobTheBuidler marked this pull request as draft September 26, 2025 22:14
r1 :: bit
r2 :: bool
L0:
r0 = C.__len__(f)
Copy link
Contributor Author

@BobTheBuidler BobTheBuidler Sep 28, 2025

Choose a reason for hiding this comment

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

I think this line should be f.__len__() instead of C.__len__(f) because neither C nor C.__len__ nor F is final

But maybe its fine because F isn't decorated with mypyc_attr(allow_interpreted_subclasses=True)? Seeking feedback on this point

Copy link
Collaborator

Choose a reason for hiding this comment

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

Mypyc performs static analysis on class hierarchies, and if you aren't doing separate compilation (most tests don't use it), it can identify all possible subclasses and thus determine that F is effectively final, since there are no (can be no) subclasses.

@BobTheBuidler BobTheBuidler marked this pull request as ready for review September 28, 2025 18:48
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.

Have you measured that this improves performance? Method calls should already be optimized and call the native __len__ method if it's defined. The goal is that gen_method_call generates the fastest possible method call, so any further optimizations should likely go there instead of every place where we generate method calls.

r1 :: bit
r2 :: bool
L0:
r0 = C.__len__(f)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Mypyc performs static analysis on class hierarchies, and if you aren't doing separate compilation (most tests don't use it), it can identify all possible subclasses and thus determine that F is effectively final, since there are no (can be no) subclasses.

BobTheBuidler added a commit to BobTheBuidler/mypy that referenced this pull request Sep 29, 2025
r1 :: bit
r2 :: bool
L0:
r0 = c.__len__()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

In a perfect world, should gen_method_call output the IR on the left or on the right?

I'll try to reimplement the logic from this PR in there and see what happens

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think in this case they should do the same thing, but the original IR is more consistent with the intent of the code, since we aren't calling a static method, so the it would be better.

@BobTheBuidler
Copy link
Contributor Author

Okay, I misunderstood what the IR was representing and no longer think this PR is necessary. Closing so we can focus on the others.

@BobTheBuidler BobTheBuidler deleted the c-len branch September 30, 2025 11:08
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