Repository navigation
Key the generator resume cache on the generator's code first - #30
Draft
Ivo Horák (horakivo) wants to merge 1 commit into
Draft
Ivo Horák (horakivo) wants to merge 1 commit into
Ivo Horák (horakivo) wants to merge 1 commit into
Conversation
ResumeGeneratorNode cached on the continuation root to resume. That field of PGenerator is rewritten on every resume, so when a function such as `_sum(iterable)` is shared by callers passing different generators, the compiled code of each caller keeps the continuations of the other callers' generators: the guard is on a loop phi and folds only if the compiler happens to duplicate the loop body (a profile-dependent DuplicationPhase decision). In euler31 this decided between a 5k and a 119k compiled `balance` depending on whether compilation ran in the background. Add an outer cache level keyed on PGenerator.getRootNode() (final), with the continuation cache moved into ResumeContinuationNode. The outer guard folds whenever the generator is allocated in the same compilation unit, independent of duplication heuristics.
Merging this PR will regress 2 benchmarks1 warning
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing |
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.
The problem
meso/euler31settles on one of three speeds, decided in the first iteration of a process and never changing afterwards. One binary on the benchmark host, 30 iterations per process, median of the second half:--engine.BackgroundCompilation=falseIn the default runs the compiled
balanceis about 111,000 bytes; with synchronous compilation it is about 6,000.The cause
_sum(iterable)is shared by callers that pass different generators. Its loop resumes them throughResumeGeneratorNode, whose cache is keyed on the continuation root to resume. That field ofPGeneratoris rewritten on every resume, so at the loop header it is a phi, and the guards against the other callers' continuations fold only if the compiler duplicates the loop body, which depends on profile-derived probabilities. When it does not, the_suminlined intobalancekeeps the continuations of the generator expression insolveand everything inlined below them.The change
An outer cache level keyed on
PGenerator.getRootNode(), a final field; the existing continuation cache moves unchanged intoResumeContinuationNode. The outer guard folds whenever the generator is allocated in the same compilation unit, independent of duplication.Evidence so far (compiler logs, not times)
Default mode, 6 of 6 runs on a native standalone (darwin-aarch64), final compilation of each function:
balancegen(resume_bci=20)gen(resume_bci=500)_sumMeasured
Benchmark host, 30 iterations per process, median of the second half.
Ten separate processes of one build with this change, default options:
develop(13 processes of one build)meso/euler31steady statemeso/euler31first iterationinterp/nbody3The full benchmark run of this branch:
euler311.52 s; suite medians of the ratio againstdeveloppure Python 0.98, C API micro 1.00, real packages unchanged. In that runinterp/nbody3measured 0.263 s against 0.222 to 0.237 s in earlier runs of other builds; the ten processes above, of another build of the same change, do not show it.Risks
Not covered
Whether the outer generator expression of
solvereaches the top tier still varies between runs; that is decided by the compilation queue (a stale task is dropped and not queued again).Testing
415 generator, coroutine, async generator and contextlib tests pass on the native build and on the JVM with assertions.
🤖 Generated with Claude Code