[mypyc] feat: cache len for iterating over immutable types#19656
Merged
JukkaL merged 8 commits intopython:masterfrom Aug 14, 2025
Merged
[mypyc] feat: cache len for iterating over immutable types#19656JukkaL merged 8 commits intopython:masterfrom
JukkaL merged 8 commits intopython:masterfrom
Conversation
for more information, see https://pre-commit.ci
…/mypy into for-loop-immutable
for more information, see https://pre-commit.ci
BobTheBuidler
commented
Aug 14, 2025
| L0: | ||
| r0 = 'abc' | ||
| source = r0 | ||
| r1 = CPyStr_Size_size_t(source) |
Contributor
Author
There was a problem hiding this comment.
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
JukkaL
approved these changes
Aug 14, 2025
Collaborator
JukkaL
left a comment
There was a problem hiding this comment.
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).
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! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.