Skip to content

Key the generator resume cache on the generator's code first - #30

Draft
Ivo Horák (horakivo) wants to merge 1 commit into
developfrom
perf/generator-resume-cache
Draft

Ivo Horák (horakivo) wants to merge 1 commit into
developfrom
perf/generator-resume-cache

Conversation

@horakivo

@horakivo Ivo Horák (horakivo) commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

The problem

meso/euler31 settles 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:

Run Processes Steady state
default 8 of 13 2.44 s
default 5 of 13 3.58 s
--engine.BackgroundCompilation=false 3 of 3 1.53 s

In the default runs the compiled balance is 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 through ResumeGeneratorNode, whose cache is keyed on the continuation root to resume. That field of PGenerator is 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 _sum inlined into balance keeps the continuations of the generator expression in solve and everything inlined below them.

The change

An outer cache level keyed on PGenerator.getRootNode(), a final field; the existing continuation cache moves unchanged into ResumeContinuationNode. 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:

Function Before With the change
balance about 119,000 5,296
gen(resume_bci=20) about 41,000 about 16,000
gen(resume_bci=500) about 122,000 about 74,000
_sum about 142,000 about 101,000

Measured

Benchmark host, 30 iterations per process, median of the second half.

Ten separate processes of one build with this change, default options:

Row develop (13 processes of one build) This change (10 processes)
meso/euler31 steady state 2.44 s in 8, 3.58 s in 5 1.48 to 1.52 s in all 10
meso/euler31 first iteration 4.35 s or 3.7 s 3.47 to 3.52 s
interp/nbody3 0.228 to 0.240 s in all 10

The full benchmark run of this branch: euler31 1.52 s; suite medians of the ratio against develop pure Python 0.98, C API micro 1.00, real packages unchanged. In that run interp/nbody3 measured 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

  • Up to 4×4 inlined continuations per resume site instead of 4, so more compile work at sites that see several generators.
  • One more node and cache lookup per resume site in the interpreter.

Not covered

Whether the outer generator expression of solve reaches 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

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.
@horakivo Ivo Horák (horakivo) added the perf:bench Runs walltime bench on our infra label Nov 29, 2026
@codspeed

codspeed Bot commented Nov 29, 2026

Copy link
Copy Markdown

Merging this PR will regress 2 benchmarks

1 warning

⚠️ Walltime results may be unreliable

Some benchmarks used the Walltime instrument outside CodSpeed Macro Runners, which can make the results inconsistent.

Learn more about Macro Runners

See affected benchmarks

⚡ 3 improved benchmarks
❌ 2 regressed benchmarks
✅ 82 untouched benchmarks

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ interp/nbody3[30000] 220.2 ms 258 ms -14.65%
❌ memory/idle 24.3 ms 27.2 ms -10.79%
⚡ meso/euler31[200] 3.6 s 1.5 s ×2.4
⚡ meso/euler11[10000] 2 s 1.2 s +58.86%
⚡ micro-native/c-upcall-slot[1048576] 1.8 ms 1.5 ms +16.54%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing perf/generator-resume-cache (62896e7) with develop (7cbd6af)

Open in CodSpeed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

perf:bench Runs walltime bench on our infra

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant