gh-149481: skip FOR_ITER inline specialization for Python __next__#149491
Open
NekoAsakura wants to merge 2 commits intopython:mainfrom
Open
gh-149481: skip FOR_ITER inline specialization for Python __next__#149491NekoAsakura wants to merge 2 commits intopython:mainfrom
FOR_ITER inline specialization for Python __next__#149491NekoAsakura wants to merge 2 commits intopython:mainfrom
Conversation
savannahostrowski
approved these changes
May 7, 2026
Member
savannahostrowski
left a comment
There was a problem hiding this comment.
Yep, this was pretty much the exact fix I had locally. Thanks for the quick turnaround on this @NekoAsakura ❤️
I'll wait to see if anyone else wants to have a look.
markshannon
approved these changes
May 7, 2026
Member
markshannon
left a comment
There was a problem hiding this comment.
Looks good. Thanks for doing this.
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.
Thanks for the careful bisect and the empirical fix from @savannahostrowski. ❤️
Benchmark
Root cause analyse (from opus 4.7)
When
__next__is Python-defined,tp_iternextis the genericslot_tp_iternextslot wrapper that re-enters the eval loop viavectorcall_method. Calling it through the new_ITER_NEXT_INLINE(which capturestp_iternextas a baked-in operand and lives in a tight_JUMP_TO_TOPloop) is correct functionally, but each new outer-iteration class causes_GUARD_TYPE_ITERto fail and warm a side-exit. After ~SIDE_EXIT_INITIAL_VALUE(4000) hits at each side-exit, a side trace forms there;_EXIT_TRACEjumps trace-to-trace viaTIER2_TO_TIER2, which doesn't re-check the eval-breaker. WithMAX_CHAIN_DEPTH=4traces possible, the chain accumulates ~4×4000+4002 ≈ 20k inner hits before the system spirals into a tier-2-only loop with no signal checkpoints — matching the observed exact threshold. The pre-PR_FOR_ITER_TIER_TWOpath goes through_PyForIter_VirtualIteratorNext, which uses an indirect dispatch onPy_TYPE(iter_o)->tp_iternextand does not produce a per-type guard, so no side-trace storm forms. Falling back to that path for slot iterators sidesteps the issue while keeping_ITER_NEXT_INLINEfor C-level iterators (dict, set, enumerate, zip, reversed, etc.), which the existingtest_for_iter_direct_*tests exercise.xml.etree.iterparse#149481