KEMBAR78
[mypyc] feat: cache len for iterating over immutable types by BobTheBuidler · Pull Request #19656 · python/mypy · GitHub
Skip to content

Conversation

BobTheBuidler
Copy link
Contributor

@BobTheBuidler BobTheBuidler commented Aug 14, 2025

Currently, if a user uses an immutable type as the sequence input for a for loop, the length is checked once at each iteration which, while necessary for some container types such as list and dictionaries, is not necessary for iterating over immutable types tuple, str, and bytes.

This PR modifies the codebase such that the length is only checked at the first iteration, and reused from there.

L0:
r0 = 'abc'
source = r0
r1 = CPyStr_Size_size_t(source)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this CPyStr_Size twice in 4 lines thing looks pretty ugly, but its better than what we had before. This will be optimized a bit better in my follow-up 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.

This looks reasonable. I suspect that this will rarely yield any significant performance improvement, but the implementation is simple enough and it could help in the future more once we optimize other things (almost all the change volume is in irbuild tests, which are easier to maintain since they can be updated automatically).

@JukkaL JukkaL merged commit c95d8ab into python:master Aug 14, 2025
13 checks passed
@BobTheBuidler
Copy link
Contributor Author

I did see a ~0.4% improvement when I benchmarked self-check, but that was on the larger PR with the other len-related change as well. In any case the IR looks prettier now!

@BobTheBuidler BobTheBuidler deleted the for-loop-immutable branch August 14, 2025 15:22
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