Skip to content

Dual-stack prototype: optimisations, benchmarks and profiler experiments - #9

Draft
maurycy wants to merge 47 commits into
dual-stack-protofrom
dual-stack
Draft

maurycy wants to merge 47 commits into
dual-stack-protofrom
dual-stack

Conversation

@maurycy

@maurycy maurycy commented Oct 24, 2026 •

Copy link
Copy Markdown
Owner

This is 95% AI prototype, where I drove the AI w/ lots of random ideas and asked it to rebenchmark over and over.

Interesting specific ideas / optimizations:


This PR builds on Pablo's branch, the original dual-stack prototype. Pablo's main is the main commit it started from.

In these microbenchmarks, our branch runs about as fast as Pablo's main. It is about 4% slower than Pablo's main + selected optimisations.

Changes worth extracting?

  • idea(direct-argument-binding): exact positional vectorcall binding reduces instructions on Pablo's main by about 18% for sort_key_callback.
  • idea(direct-init-binding): exact positional __init__ binding reduces instructions on Pablo's main by about 9% for init_shim. Both binding ideas require review of argument-shape guards and reference ownership.
  • idea(data-stack-chunk-cache): retaining up to four freed chunks reduces recursion_depth600 time on Pablo's main by about 68%, with unchanged instructions. Avoids repeated mappings but retains memory.
  • idea(skip-discarded-none): skip RESUME_CHECK and POP_TOP when the value is discarded. Review eval-breaker, instrumentation and thread-local bytecode guards.
  • idea(create-generators-in-place): create generators in place.
  • idea(follow-delegation-on-resume): follow delegation at resume. These two ideas were measured only on our branch; separate ports to Pablo's main are needed to establish their independent benefit.

Interpreter cost

Linux, AMD Zen 4, GCC 13, PGO+LTO. Each column is relative to Pablo's main; lower is better.

Build Runtime Instructions
Pablo's main 1.000 1.000
Pablo's main + selected optimisations 0.958 0.982
Our branch 0.999 1.040

These are geomeans: runtime covers 24 common workloads; instructions cover 31. Against Pablo's main + selected optimisations, our branch takes 4.3% more time and executes 5.9% more instructions.

The selected changes are idea(skip-discarded-none), idea(direct-argument-binding), idea(direct-init-binding), and idea(return-resume-pair). This comparison does not fully isolate the split: that build lacks generator creation in place, delegation following and the data-stack chunk cache. Deep recursion includes the independent chunk-cache gain.

A separate asyncio task-churn comparison reports 4.5% more time per completed task against the same build. This is not a general application slowdown. Free-threaded performance is unmeasured.

idea(control-stack-cache) brings thread creation and join from 63.0 us to 46.9 us (Pablo's main: 47.1 us). idea(smaller-control-stack-reservation) drops the reservation from 1 GiB to 64 MiB, reducing virtual-address-space pressure; this is not resident memory per thread.

Profiler benefit

At 1 kHz, bulk reads use roughly 15–20% less sampler CPU than individual reads on the same build of our branch. The target mixes recursive calls and asyncio task churn, using the default thread-stack reader.

There is no demonstrated reduction in combined target + sampler CPU against Pablo's main + selected optimisations. At 10 kHz, both samplers use roughly a core, with no total CPU advantage. The bulk read covers control records; code objects and other metadata still need separate reads.

Returned stacks have not been validated against an oracle. Both bulk and individual reads report similarly low error rates, below the comparison build's, so batching alone does not explain the difference. One read is not an atomic snapshot.

What needs to happen next

  • Fix the bulk reader's double free on a failed remote read. Validate state filtering, offsets, native anchors and cache reuse; async-aware inspection and deep-chain handling remain incomplete.
  • Investigate the final-GC gen_traverse segfault and reconcile the generator-size expectation with its allocation size.
  • Extract independent optimisations. Move diagnostic switches to experiment patches. idea(return-resume-pair) has no demonstrated benefit; drop it unless otherwise justified.

The debug build and verify.sh quick pass; the full suite has five failing files, detailed below.

Then compare target + sampler CPU for fixed useful work, at matched sampling rate and validated stack quality, against Pablo's main with the same generic optimisations. That comparison remains unmeasured.

Revisions, measurements, validation and experiment reports

Revisions and method

Revision Role
aed4ad8c439 Pablo's main
48370ac99b4 Pablo's branch; PR base
b43d645655e Pablo's main + selected optimisations
67cbf691a41 Profiler build
0e82de9b119 Final code, including Idea 19 (finish-without-saving-continuation)
fabb6188a9d Comments only

Instructions use perf stat; pyperf uses 10 processes × 3 values. Asyncio uses 12 interleaved processes, one warmed event loop each. Earlier macOS and final Linux results are separate. Absolute pyperf times in the reports are per run(1000) batch, not per individual operation.

Split-specific fixes remove redundant recursion-limit adjustments, reserve C-entry and callee records together, and preserve frame lifetime across pops and finalizers. They also reduce the control-stack reservation and cache released stacks.

Profiler measurements

Four interleaved repetitions; fixed target work; 1 kHz requested and achieved; CPU timed without strace.

Build / reader Sampler CPU Target + sampler CPU Reported sample errors
Pablo's main + selected optimisations 0.26–0.27 s 2.54–2.63 s 13.1–14.6%
Our branch, bulk 0.21–0.22 s 2.59–2.66 s 0.00–0.04%
Same build, individual records 0.25–0.26 s 2.65–2.78 s 0.00–0.04%

Separate strace runs measured about 5.5 reads/sample bulk, 18–20 individual; excluded from timing. The closing report's equal-valid-rate result is extrapolated, not measured at matched quality.

Validation

At fabb6188a9d, the macOS debug rebuild and Tools/dualstack/verify.sh quick passed, including recursion comparisons and golden monitoring traces. Regeneration produced no changes; a lexer comparison confirmed unchanged non-comment C tokens. Benchmarks were not rerun for this comment-only commit.

With the checker in default window mode, 461 test files passed; five failed: test_sys (generator size 192 vs 184), spawn/forkserver multiprocessing process tests (subprocess segfault in gen_traverse during final GC), and undiagnosed test_kqueue/test_socket failures.

Experiment reports

Negative results: spilled aggregate copies, stack pointer in the data frame, entry data off the C stack, and forced scalar loads to reduce forwarding interlocks. Implicit entry and persistent Task boundaries remain unimplemented. Earlier layout diagnoses and an asyncio measurement were withdrawn after instrument corrections.

Previous labels and idea names

Earlier result files and discussions use the labels below. Literal paths, branch names and commands retain their original spelling.

Previous label Idea
Round 1, idea 1 Idea 1 (pop-before-clear)
Round 1, idea 2 Idea 2 (aggregate-restore)
Round 1, idea 3 Idea 3 (byte-state-and-adjacent-caller)
Round 2, local sp Idea (restore-with-local-sp)
Round 2, point 1 Idea 1 (publish-active-once)
Round 2, point 2 Idea 2 (combine-stack-bounds)
Round 2, point 3 Idea 3 (reserve-entry-and-generator)
Round 2, point 4 Idea 4 (reload-data-pointer)
Round 2, point 5 Idea 5 (data-stack-chunk-cache)
exp1 / experiment1 Idea 1 (skip-discarded-none)
exp2 / experiment2 Idea 2 (create-generators-in-place)
exp3 / experiment3 Idea 3 (direct-argument-binding)
exp4 / experiment4 Idea 4 (tail-call-interpreter)
exp5 / experiment5 Idea 5 (follow-delegation-on-resume)
exp5b / experiment5b Idea 5b (share-resume-opcode-loads)
exp6 / experiment6 Idea 6 (snapshot-before-restore)
exp6b / experiment6b Idea 6b (resume-loads-before-stores)
exp7 / experiment7 Idea 7 (yield-loads-before-stores)
exp8 / experiment8 Idea 8 (take-ownership-before-pop)
exp9 / experiment9 Idea 9 (single-dispatch-init-exit)
exp10 / experiment10 Idea 10 (direct-init-binding)
exp11 / experiment11 Idea 11 (reserve-entry-and-callee)
exp12 / experiment12 Idea 12 (entry-data-on-data-stack)
exp12b / experiment12b Idea 12b (entry-data-with-frame-anchor)
exp13 / experiment13 Idea 13 (write-resume-record-once)
exp14 / experiment14 Idea 14 (smaller-control-stack-reservation)
exp15 / experiment15 Idea 15 (return-resume-pair)
exp16 / experiment16 Idea 16 (control-stack-cache)
exp17 / experiment17 Idea 17 (stack-pointer-in-data-frame)
exp18 / experiment18 Idea 18 (scalar-generator-loads)
exp19 / experiment19 Idea 19 (finish-without-saving-continuation)

maurycy and others added 3 commits October 24, 2026 22:01
RETURN_GENERATOR popped the control record while tstate->current_frame
still pointed at it. Follow the order used by _RETURN_VALUE (pythonGH-99729):
unlink first, then pop.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
_PyThreadState_CheckControlStack() verifies the control stack header,
the base record, current_frame placement, record states and owners,
data pointers (data stack chunks, generators, C stack entries), frame
object links, stack pointers, controlstack_extra accounting and the
poison left above top. Debug builds run a windowed check on every push
and pop (PYTHON_CONTROLSTACK_CHECK=window|full|off) and poison popped
records. _testinternalcapi exposes check_control_stack() and
control_stack_dump().

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Recursion depth table, golden sys.monitoring traces over a control-flow
corpus, per-operation instruction counts, oracle quick mode, refleak
runner and a pyperf milestone script, with goldens recorded from the
prototype's parent commit (aed4ad8).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@maurycy

maurycy commented Oct 24, 2026 •

Copy link
Copy Markdown
Owner Author

Verifiers: Pablo's main and our branch

The 15 monitoring-trace programs agree. Two recursion scenarios differ, and the generator-size test fails with or without the checker. The first PGO+LTO comparison puts the prototype at 1.17x the runtime of Pablo's main.

These are the initial results, before the optimisation rounds. Pablo's main is aed4ad8c439; Pablo's branch is the unmodified prototype. The debug comparison uses our branch at 5b7ae0da9c3, --with-pydebug, macOS arm64.

Build strings, retained as recorded:

Pablo's main:

3.16.0a1+dev (heads/main-131-gaed4ad8c439:aed4ad8c439, Oct 24 2026, 21:58:18) [Clang 21.0.0 (clang-2100.3.34.2)]

Our branch, same configure options:

3.16.0a1+dev (heads/dual-stack-dirty:48370ac99b4, Oct 24 2026, 21:51:46) [Clang 21.0.0 (clang-2100.3.34.2)]

Recursion limits

recursion_table.py --compare golden/recursion_baseline.json:

limit=1000 python=/Users/maurycy/work/cpython-dual-stack/python.exe
plain                                        998  baseline                      998
method                                       997  baseline                      997
yield_from_chain                             997  baseline                      997
generator_for_loop                           997  baseline                      997
next_from_c                                  997  baseline                      997
gen_send_from_c                              997  baseline                      997
await_chain                                  997  baseline                      997
init_shim                                    503  baseline                      503
init_shim_super                              333  baseline                      334  <-- CHANGED
property_getter                              997  baseline                     1085  <-- CHANGED
getattr_hook                                 997  baseline                      997
dunder_call                                  997  baseline                      997
eval_entry                                   499  baseline                      499
exec_entry                                   499  baseline                      499
map_callback                                 998  baseline                      998
sort_key_callback                            998  baseline                      998
gen_throw_chain                              997  baseline                      997
throw_into_chain_depth                      4000  baseline                     4000
close_chain_depth                           4000  baseline                     4000
del_during_unwind                            998  baseline                      998
limit_raised_midway                         1998  baseline                     1998
limit_lowered_midway                         498  baseline                      498
handler_recursion                            998  baseline                      998
thread_plain                                 996  baseline                      996
2 scenario(s) changed
  • property_getter is under-counted on Pablo's main; the control stack counts each level. The original notes also report 1087 levels at sys.setrecursionlimit(1000), while the pasted harness output says 1085. Those recorded counts are preserved; their discrepancy is not resolved here.
  • init_shim_super fits one fewer C(d) -> shim -> C.__init__ -> B.__init__ level.

Both differences originate in Pablo's branch.

Monitoring traces

frame_trace.py compare golden/, using goldens from Pablo's main:

ok       async_gen.py             81 events
ok       asyncio_tasks.py         120 events
ok       calls.py                 56 events
ok       cext_callbacks.py        145 events
ok       coroutines.py            189 events
ok       eval_exec.py             20 events
ok       exceptions.py            172 events
ok       finalizers.py            121 events
ok       frames.py                107 events
ok       gen_throw.py             184 events
ok       generators.py            170 events
ok       init_shim.py             81 events
ok       recursion.py             8366 events
ok       threads.py               2002 events
ok       tracing.py               33 events
0 failure(s)

All 15 programs match in events, stdout and probe() stacks. Events cover PY_START/RESUME/RETURN/YIELD/THROW/UNWIND, RAISE, RERAISE, EXCEPTION_HANDLED and STOP_ITERATION.

Checker overhead

Our branch only: Pablo's main has no control stack. Eighteen frame-heavy test files, -j8, sequential runs on an idle machine:

PYTHON_CONTROLSTACK_CHECK wall user
off 18.4 s 35.6 s
window 19.0 s 37.1 s
full 18.7 s 38.5 s

All three modes give the same test results. Only test_sys.test_objecttypes fails: generator sizeof 192 vs 184, including with the checker off.

Initial runtime comparison

Both builds use ./configure --enable-optimizations --with-lto, clang 21, macOS arm64. bench_pyperf.py -p 10 -n 3 -w 1 runs Pablo's main then the prototype on an idle machine. There is one build each; PGO build-to-build variance was not yet measured.

Absolute pyperf times are per run(1000) batch, not per individual operation.

Interleaved reruns reproduce the direction for call_return and gen_for_loop: 12.2 vs 14.5 us and 10.8 vs 13.0 us per 1000 iterations, respectively. These reruns are separate from the table below.

Workload Pablo's main Pablo's branch
dualstack.call_return 10.1 us 12.6 us: 1.24x slower
dualstack.call_return_kwargs 16.1 us 17.9 us: 1.11x slower
dualstack.gen_for_loop 9.32 us 11.3 us: 1.21x slower
dualstack.gen_send 18.9 us 21.3 us: 1.13x slower
dualstack.next_from_c 18.4 us 21.1 us: 1.15x slower
dualstack.yield_from_depth5 49.0 us 63.3 us: 1.29x slower
dualstack.await_chain 136 us 168 us: 1.23x slower
dualstack.asyncio_pingpong 26.0 ms 26.7 ms: 1.03x slower
dualstack.recursion_depth200 2.39 ms 2.89 ms: 1.21x slower
dualstack.exception_unwind10 594 us 640 us: 1.08x slower
dualstack.init_shim 35.9 us 42.6 us: 1.19x slower
dualstack.property_get 9.21 us 11.6 us: 1.26x slower
dualstack.sort_key_callback 23.1 us 26.0 us: 1.13x slower
dualstack.del_finalizer 45.4 us 52.1 us: 1.15x slower
dualstack.frame_object_access 45.5 us 49.5 us: 1.09x slower
dualstack.gen_close_finally 143 us 173 us: 1.21x slower
Geometric mean (ref) 1.17x slower

The regression also affects plain calls and property access. Generator/coroutine ratios span 1.13x–1.29x; asyncio_pingpong, dominated by the event loop, moves 3%. Later rounds measure PGO variance and optimise these paths.

Not run at this stage

  • instr_count.py: requires Linux perf or valgrind.
  • oracle_quick.py: the remote reader is not ported.
  • refleak.sh: not yet run.

maurycy and others added 4 commits October 24, 2026 22:30
clear_thread_frame and clear_gen_frame bumped controlstack_extra and the
limit around _PyFrame_ClearExceptCode so finalizers run by the clear
were not charged for the dying frame: four read-modify-writes and two
branches on every return. Instead, take what the clear needs from the
record, pop it, then clear the data frame and pop it. Finalizers push
over the freed slot. The rare path with a live frame object keeps the
old order so take_ownership can copy the record.

Generators save their state into gi_iframe (via _PyGen_PopFrame) first
and are cleared through that saved record, so DETACHED records no
longer appear on the control stack. T2 in the checker now allows the
dying frame's data to outlive its record.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
A two-bit bitfield cannot be exposed through offsetof() for out-of-process
readers and costs an extra mask on every _PyFrame_GetCaller. The record
stays 40 bytes. GetCaller now tests the adjacent record first and only
loops over RESERVED records in the rare cases (a shim before activation,
a frame whose initialization failed).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
gi_iframe is a complete DETACHED record: executable, data pointer, owner
and the saved instruction and stack pointers. Linking a generator frame
is now a struct copy plus the state byte instead of eight field stores,
and unlinking saves only the instruction and stack pointers;
return_offset of a suspended generator is dead (the caller sets it on
the next call). Matches what the free-threaded path already does.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@maurycy

maurycy commented Oct 24, 2026 •

Copy link
Copy Markdown
Owner Author

Round 1: return bookkeeping, caller lookup and generator restore

Popping the control record before clearing the frame gives the largest gain. A byte-sized state and adjacent-caller check improve it further. The aggregate generator copy regresses and is reverted.

Pablo's main is aed4ad8 (base in the raw tables); Pablo's branch is dual-stack-proto, 48370ac (proto). Ratios compare against Pablo's main. These are PGO+LTO pyperf results, one build per commit; later rounds retrain profiles and revisit the noise floor.

commit what geomean vs Pablo's main
48370ac Pablo's branch, unmodified 1.17x slower
cd267e6 Idea 1 (pop-before-clear): pop the control record before clearing the dying frame (no AdjustControlLimit pair on every return) 1.11x
38f105e Idea 3 (byte-state-and-adjacent-caller): state as a whole byte, _PyFrame_GetCaller tests the adjacent record first 1.09x
5d9fb6e Idea 2 (aggregate-restore): resume a generator with one struct copy instead of eight stores 1.10x, reverted in 773455f

Mechanisms and lifetime constraints

idea(pop-before-clear), cd267e67700: pop before clearing. Pablo's branch clears locals while their control record is still on the stack. Because a __del__ can push frames during clearing, it marks the record RESERVED and calls _PyThreadState_AdjustControlLimit(+1) and (-1) on every return. The change pops the record first, then clears its data. The materialised-frame-object path initially keeps the old order.

The data frame must outlive its record until clearing finishes; the checker's T2 invariant was relaxed accordingly. The first attempt omitted PyStackRef_CLEAR(fp->f_funcobj) and leaked every function object. Finalizer-dependent tests caught the leak; C2/T2 checks caught two ordering errors before benchmarking.

idea(byte-state-and-adjacent-caller), 38f105e8975: byte state and adjacent caller. The two-bit state shared storage with visited, requiring a mask after the load. A uint8_t makes it directly readable, including through offsetof(). _PyFrame_GetCaller first tests frame - 1; an ACTIVE adjacent record is the common case. Only an inactive record requires the downward walk.

idea(aggregate-restore), 5d9fb6ea78d: aggregate restore. Replacing eight field stores with *frame = gen->gi_iframe changes gen_for_loop from 1.11 to 1.22 (about 10% slower) and yield_from_depth5 from 1.23 to 1.28, with no gain elsewhere. Reverted in 773455ffa72.

Round 2's local-stack-pointer variant (bcd6f40) is neutral. Round 3 later tests layout-sensitive copy costs on x86-64 and adds explicit register words (21e3701); that does not by itself establish the cause of the original arm64 regression.

Full measurements

Times are per run(1000) batch; ratios compare against Pablo's main:

Workload Pablo's main Pablo's branch Idea 1 (pop-before-clear) Idea 3 (byte-state-and-adjacent-caller) Idea 2 (aggregate-restore)
dualstack.call_return 10.1 us 12.6 us 1.24 11.1 us 1.10 10.8 us 1.06 11.1 us 1.09
dualstack.call_return_kwargs 16.1 us 17.9 us 1.11 17.3 us 1.08 18.1 us 1.12 17.6 us 1.10
dualstack.gen_for_loop 9.32 us 11.3 us 1.21 10.8 us 1.16 10.3 us 1.11 11.4 us 1.22
dualstack.gen_send 18.9 us 21.3 us 1.13 20.8 us 1.10 19.7 us 1.04 19.8 us 1.05
dualstack.next_from_c 18.4 us 21.1 us 1.15 21.0 us 1.15 19.8 us 1.08 20.0 us 1.09
dualstack.yield_from_depth5 49.0 us 63.3 us 1.29 59.6 us 1.22 60.3 us 1.23 62.6 us 1.28
dualstack.await_chain 136 us 168 us 1.23 161 us 1.18 156 us 1.15 162 us 1.19
dualstack.asyncio_pingpong 26.0 ms 26.7 ms 1.03 26.8 ms 1.03 26.5 ms 1.02 26.6 ms 1.02
dualstack.recursion_depth200 2.39 ms 2.89 ms 1.21 2.74 ms 1.15 2.78 ms 1.16 2.72 ms 1.14
dualstack.exception_unwind10 594 us 640 us 1.08 617 us 1.04 not significant not significant
dualstack.init_shim 35.9 us 42.6 us 1.19 39.1 us 1.09 39.3 us 1.10 38.0 us 1.06
dualstack.property_get 9.21 us 11.6 us 1.26 9.46 us 1.03 9.75 us 1.06 9.81 us 1.07
dualstack.sort_key_callback 23.1 us 26.0 us 1.13 24.4 us 1.06 24.2 us 1.05 24.4 us 1.06
dualstack.del_finalizer 45.4 us 52.1 us 1.15 50.9 us 1.12 50.7 us 1.12 51.3 us 1.13
dualstack.frame_object_access 45.5 us 49.5 us 1.09 49.8 us 1.09 48.8 us 1.07 48.5 us 1.07
dualstack.gen_close_finally 143 us 173 us 1.21 170 us 1.19 160 us 1.12 166 us 1.16
Geometric mean (ref) 1.17 1.11 1.09 1.10

A/A and measurement limits

Two PGO builds of 38f105e8975, measured consecutively, agree within 1% except gen_send (1.04 vs 1.11). This supports the observed generator-copy regression in these builds; it is not a general bound on fresh-profile variance. Round 2 finds larger single-row shifts after retraining.

-O3 -S output contains no memcpy call for the 40-byte copy. The free-threaded prototype's gen_copy_frame uses the same aggregate operation, but was not measured here.

Workload Pablo's main Idea 3 (byte-state-and-adjacent-caller) Idea 3 (byte-state-and-adjacent-caller) A/A
dualstack.call_return 10.1 us 10.8 us 1.06 10.8 us 1.07
dualstack.call_return_kwargs 16.1 us 18.1 us 1.12 18.2 us 1.13
dualstack.gen_for_loop 9.32 us 10.3 us 1.11 10.4 us 1.12
dualstack.gen_send 18.9 us 19.7 us 1.04 21.0 us 1.11
dualstack.next_from_c 18.4 us 19.8 us 1.08 19.7 us 1.07
dualstack.yield_from_depth5 49.0 us 60.3 us 1.23 60.1 us 1.23
dualstack.await_chain 136 us 156 us 1.15 156 us 1.14
dualstack.asyncio_pingpong 26.0 ms 26.5 ms 1.02 26.8 ms 1.03
dualstack.recursion_depth200 2.39 ms 2.78 ms 1.16 2.79 ms 1.17
dualstack.init_shim 35.9 us 39.3 us 1.10 39.5 us 1.10
dualstack.property_get 9.21 us 9.75 us 1.06 9.77 us 1.06
dualstack.sort_key_callback 23.1 us 24.2 us 1.05 24.4 us 1.06
dualstack.del_finalizer 45.4 us 50.7 us 1.12 50.7 us 1.12
dualstack.frame_object_access 45.5 us 48.8 us 1.07 48.5 us 1.06
dualstack.gen_close_finally 143 us 160 us 1.12 160 us 1.12
Geometric mean (ref) 1.09 1.10

Benchmark hidden because not significant (1): dualstack.exception_unwind10

Verification

Each step passed verify.sh quick: checker-enabled core tests, unchanged recursion table against Pablo's branch, and 15/15 golden traces.

The full checker run at cd267e67700 covered 548 files with four failures: test_external_inspection (reader not ported), test_kqueue and test_socket (attributed to the environment in this run), and test_multiprocessing_spawn.test_processes. The last contains a flaky gen_traverse segfault during subprocess final GC, also reproduced on Pablo's unmodified branch in 2/8 runs.

@maurycy

maurycy commented Oct 24, 2026 •

Copy link
Copy Markdown
Owner Author

Research notes after round 1

State: 773455ffa72, with verifiers and idea(pop-before-clear) and idea(byte-state-and-adjacent-caller); idea(aggregate-restore) is reverted. Runtime measurements use PGO+LTO, macOS arm64 and bench_pyperf.py, relative to Pablo's main (aed4ad8c439). There is no perf on this machine: the operation counts below come from reading the diff, not hardware attribution.

Work introduced by the prototype

  • Return: clear_thread_frame and clear_gen_frame bracket clearing with AdjustControlLimit(+1)/(-1): four read-modify-writes across controlstack_extra and controlstack_limit, and two limit < end branches. There is also a RESERVED state store, caller-state scanning through a two-bit field instead of loading previous, and an fp = frame->data reload.
  • Call: a second bound (HasControlSpace(2) alongside data space), RESERVED then ACTIVE stores, one data-pointer store and one fp load. The split removes the recursion-counter decrement and _CHECK_RECURSION_REMAINING.
  • Generator transition: _PyGen_LinkFrame copies about eight saved fields to a slot; _PyGen_UnlinkFrame writes five back. Both check frame_obj. C-side next()/send() through gen_send_ex2 push entry and generator records with two adjustment pairs. Free-threaded resume and yield also take a generator critical section.
  • Registers: frame and fp remain live across the interpreter loop. Only arm64 has been measured at this stage; x86-64 register pressure is open.

Results available then

idea change geomean call_return gen_for_loop yield_from_depth5 verdict
prototype 1.17x 1.24x 1.21x 1.29x
Idea 1 (pop-before-clear) cd267e67700 take what the clear needs from the record, pop it, then clear the data frame; generators save state first and clear through gi_iframe; the frame-object path keeps the old order 1.11x 1.10x 1.16x 1.22x keep
Idea 3 (byte-state-and-adjacent-caller) 38f105e8975 state as a whole byte; GetCaller tests frame - 1 first 1.09x 1.06x 1.11x 1.23x keep
Idea 2 (aggregate-restore) 5d9fb6ea78d resume = *frame = gen->gi_iframe + state byte; unlink saves only instr_ptr/stackpointer 1.10x 1.09x 1.22x 1.28x reverted

The A/A builds of 38f105e8975 agree within 1% except gen_send (1.04 vs 1.11). idea(aggregate-restore)'s regression is larger than this observed spread. No memcpy call appears for the 40-byte copy in ceval.c at -O3 -S; the cause was unresolved here. Later rounds refine both the copy diagnosis and the PGO noise estimate.

idea(pop-before-clear) changes lifetime: dying data outlives its control record, requiring the T2 relaxation. DETACHED records no longer remain on the control stack, enabling the adjacent-caller fast path.

Bugs caught before further benchmarking

  • C1: RETURN_GENERATOR popped a record still referenced by current_frame.
  • Missing PyStackRef_CLEAR(fp->f_funcobj): every function object leaked. Four finalizer-dependent test groups caught it; the microbenchmarks would not.
  • C2: the frame-object path must retain the record during take_ownership.
  • Already present on Pablo's branch: final-GC gen_traverse segfault in a multiprocessing-spawn subprocess (2/8 runs), and generator sizeof 192 vs 184 in test_sys.

Questions at this revision

  • Plain calls still cost 6–10% in time. Bounds, state stores, fp reloads and register pressure are candidates, not measured individual contributions. Linux Tools/dualstack/instr_count.py with perf stat is needed to separate them.
  • Generators/coroutines are at 1.11–1.23x. Suspended generators have no control slot, so state must survive elsewhere. The proposed target of fewer than two stores each way was a design question, not a proved lower bound; the aggregate-copy result only rejects that tested implementation.
  • recursion_depth200 is 1.16x versus call_return 1.06x. The depth-dependent difference is unexplained at this point.
  • Folding controlstack_extra into controlstack_limit was estimated to save three instructions per entry push/pop and under 2% time. This was an unmeasured proposal.
  • A shared reservation growing control and data toward each other, like Ruby's cfp/sp, could replace two bounds checks with one and remove chunks. It requires rewriting datastack and overlaps the resizable-data-stack proposal.

maurycy and others added 7 commits October 24, 2026 23:55
…n a register

Diagnosis of the reverted struct-copy variant: the compiler reloaded
stackpointer from the bytes the vector copy had just written before
pushing the sent value (store-to-load dependency). Keep the pointer
from gi_iframe in a register across the copy. Unlink is unchanged.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Both checks are cheap and almost always true, so computing them
together lets the compiler use a conditional compare on arm64 instead
of two branches on every call.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Inlined Python-to-Python calls and generator resumes push the record
ACTIVE directly and _PUSH_FRAME only asserts it; interpreter entries,
trampolines and frames evaluated from C still push RESERVED and are
activated by the evaluator. _PyEvalFramePushAndInit activates the
record after binding and keeps the AdjustControlLimit pair only for
keyword binding, the one path that can call back into Python (str
subclass keys). PyEval_EvalFrameEx accepts an already active frame.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
gen_send_ex2 made two bounds checks and two pushes for every next() or
send() from C. _PyGen_PushEntryAndFrame checks for two slots once and
falls back to the separate pushes only when the committed region must
grow. Accounting and the entry anchor are unchanged.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…keeping it live

fp becomes a macro for frame->data inside the two interpreter loops, so
the data frame pointer is not a loop-carried value. Sites that clear a
dying frame after switching to its caller pass dying->data explicitly.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
pop_data_stack cached a single freed chunk, so a descent that needs
three chunks mapped and unmapped the third one on every repetition.
The cache is now a short list linked through ->previous. Adds a
600-deep recursion workload and a DUALSTACK_ONLY filter to the
benchmark script.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@maurycy

maurycy commented Oct 24, 2026 •

Copy link
Copy Markdown
Owner Author

Round 2: paired reservations and data-chunk reuse

Reserving the C-entry and generator records together is the only step that clearly moves the common-workload geomean. A larger freed-chunk cache helps deep recursion independently of the split.

Pablo's main is aed4ad8; Pablo's branch is 48370ac (proto/prototype below). Runtime ratios are against Pablo's main; lower is better.

Fresh profiles change the noise estimate

make alone reuses a PGO tree's profile. Every build here uses make clean && make and fresh training. Identical source can move individual rows by up to 10% either way, while the geomean moves about 1%. Judge the common-workload geomean, not isolated timing rows.

build geomean over the 15 workloads every build has
Pablo's branch, unmodified 1.162x
773455f (Idea 1 (pop-before-clear) + Idea 3 (byte-state-and-adjacent-caller)) 1.082x
bcd6f40 Idea (restore-with-local-sp) 1.074x
4480e93 Idea 2 (combine-stack-bounds) 1.085x
7226e07 Idea 1 (publish-active-once) 1.075x
4b3adbe Idea 3 (reserve-entry-and-generator) 1.053x
a1d2c2a Idea 4 (reload-data-pointer) 1.056x
8313b2f Idea 5 (data-stack-chunk-cache) 1.063x
148a0f5 final (Idea 4 (reload-data-pointer) reverted) 1.066x

Changes and decisions

  • idea(restore-with-local-sp), bcd6f408298: neutral, kept. Revisit round 1's aggregate restore by loading the saved stack pointer before the 40-byte copy, pushing through the register, and storing it last. This removes the immediate reload from copied bytes.
  • idea(combine-stack-bounds), 4480e93dac4: neutral, kept. _PyThreadState_HasStackSpace evaluates both control and data bounds, combining booleans with & instead of short-circuit &&. This lets the compiler combine the branch conditions.
  • idea(publish-active-once), 7226e07f834: neutral, kept. Inlined calls and generator resumes publish ACTIVE once, instead of RESERVED then ACTIVE; these paths do not re-enter Python before _PUSH_FRAME. _PyEvalFramePushAndInit retains the adjustment pair only for non-NULL kwnames: matching keyword strings can invoke a subclass's __eq__.
  • idea(reserve-entry-and-generator), 4b3adbe8783: clear win, kept. gen_send_ex2, used by C-side next(), send() and throw(), separately reserved entry and generator records. _PyGen_PushEntryAndFrame checks for two slots once and moves the top once. Growth falls back to separate pushes. Common-workload geomean: 1.075x → 1.053x.
  • idea(reload-data-pointer), a1d2c2a47f7: neutral on arm64, reverted in 148a0f5b6ae. Replace the live fp register with a macro loading frame->data. The intended x86-64 register-pressure benefit was not tested in this round. The checker immediately caught three dying-frame clears receiving the caller's data frame as frame changed; the macro was scoped, then dropped. Better generator rows and worse call rows remain within training noise.
  • idea(data-stack-chunk-cache), 8313b2feec6: kept. pop_data_stack cached one freed chunk, remapping the third chunk on repeated deep descents. Cache up to four through ->previous. Neutral on the 15 common workloads; recursion_depth600 moves from 0.88x to 0.61x on macOS. A later x86-64 result is 0.31x. Pablo's main has the same mapping behaviour, so this change is independent of dual-stack.

The neutral changes above were retained after passing the verifier. Their intended bookkeeping savings did not produce a measured runtime gain.

All workload ratios

The following table also includes workloads absent from some builds. Its geomeans therefore differ from the fixed set of 15 above; use the first table for the cumulative verdict. A dash means no result, not zero cost.

workload proto head 773455f Idea (restore-with-local-sp) bcd6f40 Idea 2 (combine-stack-bounds) 4480e93 Idea 1 (publish-active-once) 7226e07 Idea 3 (reserve-entry-and-generator) 4b3adbe Idea 4 (reload-data-pointer) a1d2c2a Idea 5 (data-stack-chunk-cache) 8313b2f final 148a0f5
call_return 1.24 1.12 1.03 1.12 1.08 1.02 1.15 1.11 1.05
call_return_kwargs 1.11 1.22 1.02 1.18 1.26 1.04 1.04 1.22 1.00
gen_for_loop 1.21 1.12 1.10 1.15 1.11 1.09 1.11 1.08 1.15
gen_send 1.13 1.04 1.07 1.08 1.08 1.01 0.98 1.06 1.05
next_from_c 1.15 1.08 1.12 1.04 1.06 1.00 1.06 1.03 1.12
yield_from_depth5 1.29 1.16 1.17 1.10 1.08 1.15 1.02 1.01 1.14
await_chain 1.23 1.12 1.14 1.14 1.11 1.10 1.05 1.05 1.10
asyncio_pingpong 1.03 1.02 1.03 1.02 1.01 1.02 1.01 1.01 1.01
recursion_depth200 1.21 1.04 1.03 1.03 1.03 1.02 1.07 1.05 1.04
exception_unwind10 1.08 1.01 1.00 0.98 1.00 1.04 1.01 1.01 1.03
init_shim 1.19 1.08 1.07 1.09 1.06 1.07 1.10 1.07 1.04
property_get 1.26 1.02 1.07 1.10 1.08 1.05 1.17 1.13 1.09
sort_key_callback 1.13 1.06 1.06 1.08 1.06 1.02 0.98 0.99 1.04
del_finalizer 1.15 1.13 1.14 1.12 1.10 1.10 1.06 1.06 1.09
frame_object_access 1.09 1.04 1.08 1.05 1.03 1.06 1.04 1.09 1.05
gen_close_finally 1.21 1.11 1.13 - 1.13 1.12 1.12 1.11 1.11
recursion_depth600 - - - 0.89 0.88 0.88 0.89 0.61 0.61
geomean 1.165 1.083 1.078 1.071 1.066 1.046 1.049 1.032 1.034

Outcome and verification

On the common suite, Pablo's branch is 1.162x and our final build 148a0f5 is 1.066x. Linux instruction counts are the next useful measurement for the remaining roughly 6% overhead.

Every step passed verify.sh quick. idea(reload-data-pointer)'s caller/data-frame mix-up was caught by the clear_thread_frame assertion on its first run.

maurycy and others added 6 commits October 25, 2026 07:19
A generator suspended at a bare yield resumes with RESUME_CHECK followed
by POP_TOP of the value it was sent. When that value is None and the eval
breaker matches the code's instrumentation version, the resume paths in
the interpreter (FOR_ITER_GEN, SEND_GEN, SEND) and in gen_send_ex2 skip
both instructions and do not push the value. Instrumented code, pending
eval-breaker work and non-None values take the ordinary path.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
_PyEval_Vector copied every argument into a temporary _PyStackRef array
and then let initialize_locals copy them again while checking for
varargs, keyword-only parameters and defaults. When the call has no
keywords and exactly as many positional arguments as the code expects,
push the frame and write the references into localsplus directly. Other
signatures keep the general path.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
A generator suspended inside a yield-from or await loop resumes with
RESUME_CHECK, JUMP_BACKWARD_NO_INTERRUPT and SEND_GEN, whose only effect
is to push the frame of the generator it delegates to. The resume paths
now recognise that shape, link each level's control record directly and
continue with the delegate, so a single dispatch reaches the innermost
frame. Every active generator still owns a control record, the exc_info
chain is linked in the same order, and instrumented code, pending
eval-breaker work, non-generator receivers and running delegates fall
back to the ordinary path.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Calling a generator function pushed a frame whose first instruction,
RETURN_GENERATOR, allocated the generator, copied the frame into it and
popped the frame again. A new CALL specialization, CALL_PY_GEN_EXACT_ARGS,
is chosen for exact positional calls of codes that start with
RETURN_GENERATOR and builds the saved record and data frame directly in
the generator. Calls from C through _PyEval_Vector take the same route.
Codes that start with MAKE_CELL or COPY_FREE_VARS, coroutines under
origin tracking and calls without stack space keep the general path, so
recursion limit behaviour is unchanged.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
The self reference stored into a method generator's frame must be made
heap safe, like the other locals, or the generator keeps a borrowed
reference. Calls from C also skip the in-place path while an eval frame
hook is installed, so the hook keeps seeing the RETURN_GENERATOR frame.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
The delegation chain and the discarded-value skip both start from the
same two opcode loads, so share them and load the eval breaker only
when one of the two shapes matches.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@maurycy

maurycy commented Oct 25, 2026 •

Copy link
Copy Markdown
Owner Author

Round 3: Linux instruction counts and generator restore

Plain Python calls are already near instruction parity after round 2. Generators and C entry retain extra work. Generic interpreter changes reduce some of that work; idea(resume-loads-before-stores) removes a layout-sensitive generator-resume stall in the measured x86-64 runs.

Linux: AMD Zen 4, GCC 13, PGO+LTO, perf stat and pyperf. Pablo's main is aed4ad8; Pablo's branch is 48370ac. Ratios use Pablo's main unless stated; lower is better.

  • 148a0f5: our branch after round 2, before these generic interpreter changes. It already includes the independent chunk cache, so it is not a completely isolated split comparison.
  • 4c5b8b6: after the interpreter changes below, through idea(share-resume-opcode-loads).
  • 21e3701: after idea(resume-loads-before-stores), the end of this round.

Starting point

Absolute instruction counts are for Pablo's main; other columns are ratios. This table shows 13 workloads; its geomean covers the full 17-workload suite shown later.

workload Pablo's main, instructions per iteration Pablo's branch our branch after round 2 (148a0f5)
call_return 453.8 1.104 0.996
call_return_kwargs 730.8 1.065 0.999
recursion_depth200 89538.8 1.208 1.032
property_get 378.8 1.094 1.095
init_shim 1197.9 1.181 1.096
sort_key_callback 500.5 1.299 1.236
del_finalizer 1509.6 1.109 1.054
gen_for_loop 313.6 1.174 1.141
gen_send 542.4 1.184 1.144
next_from_c 617.7 1.124 1.096
yield_from_depth5 1217.3 1.311 1.189
await_chain 3675.2 1.136 1.090
gen_close_finally 4053.6 1.064 1.034
geomean (17) 1.143 1.080

Generator resume/yield is +9–19%. sort_key_callback is +24%, about 118 instructions per C-to-Python call. Plain calls are at parity.

Ideas, in commit order

idea(skip-discarded-none), d2e1c340b2a: skip a discarded None. A bare yield resumes through RESUME_CHECK then POP_TOP. When the sent value is None and the eval breaker matches the code's instrumentation version, skip both instructions and the push. This covers FOR_ITER_GEN, SEND_GEN, generic SEND and gen_send_ex2. Instrumented code uses INSTRUMENTED_RESUME; pending work and non-None values keep the ordinary path.

idea(direct-argument-binding), c8c6e6fac0d: bind exact positional C calls directly. _PyEval_Vector previously copied arguments into a temporary _PyStackRef array, then initialize_locals copied them again while checking the signature. With no keywords and an exact positional argument count, write references directly into the new frame's localsplus. Other signatures retain the general path. This targets sort keys, finalizers, C-side __init__ calls and callbacks.

idea(follow-delegation-on-resume), b0b80072ef1: follow delegation at resume. Each yield-from/await level dispatches RESUME_CHECK, JUMP_BACKWARD_NO_INTERRUPT and SEND_GEN just to enter its delegate. _PyGen_PushFrameChain in ceval_macros.h recognises this shape, links one record per level with the correct instr_ptr and return_offset, and chains exc_info in bytecode order. One dispatch then reaches the innermost generator. Instrumentation, pending eval-breaker work, non-generator receivers and running delegates fall back. A clear_gen_frame assertion caught the first version's wrong exception-chain order.

idea(create-generators-in-place), a649beebb20 and 2faa9509f17: create generators in place. CALL_PY_GEN_EXACT_ARGS avoids pushing a frame only for RETURN_GENERATOR to copy and pop it. Exact positional calls whose first instruction is RETURN_GENERATOR initialise the saved record and data frame in the generator. _PyEval_Vector uses the same route for C calls such as __await__. MAKE_CELL/COPY_FREE_VARS prefixes, coroutine origin tracking and insufficient stack space keep the general path, preserving recursion semantics.

The full suite found two bugs missed by quick checks: borrowed method self needed PyStackRef_MakeHeapSafe (importlib.metadata segfault), and the C path bypassed the PEP 523 eval-frame hook. Both were fixed by the second commit.

idea(share-resume-opcode-loads), 4c5b8b6170c: share opcode loads. The delegation check and discarded-None check previously loaded the two resume opcodes twice. They now share those loads and read the eval breaker only if either shape matches.

idea(snapshot-before-restore), 81675e4aef7: read-before-store aggregate restore, reverted in 68227a0ab53. GCC spilled the struct snapshot to the C stack; the slow sweep depth moved from 139 to 70 and the baseline worsened.

idea(resume-loads-before-stores), 21e3701bcb8: five explicit register words. _PyGen_LinkFrame loads all 40 saved bytes into five 64-bit words, adjusts the stack-pointer word if needed, then stores the slot. No saved-record load follows a slot store. It adds instructions but fixes the measured resume stall.

idea(tail-call-interpreter) is the tail-call measurement below, not a code change.

Instructions

Each column adds to the previous build. Bold retains the original target-row markings.

workload Pablo's main, instructions per iteration our branch after round 2 (148a0f5) +Idea 1 (skip-discarded-none) +Idea 3 (direct-argument-binding) +Idea 5 (follow-delegation-on-resume) +Idea 2 (create-generators-in-place) +Idea 5b (share-resume-opcode-loads) (4c5b8b6) our branch now (+Idea 6b (resume-loads-before-stores), 21e3701)
call_return 453.8 0.996 1.009 1.007 1.007 1.007 1.007 1.009
call_return_kwargs 730.8 0.999 1.020 1.015 1.014 1.014 1.015 1.017
gen_for_loop 313.6 1.141 1.039 1.036 1.068 1.068 1.062 1.154
gen_send 542.4 1.144 1.152 1.150 1.150 1.150 1.151 1.154
next_from_c 617.7 1.096 1.036 1.035 1.034 1.034 1.034 1.088
yield_from_depth5 1217.3 1.189 1.214 1.217 1.102 1.102 1.100 1.147
await_chain 3675.2 1.090 1.088 1.058 1.073 1.015 1.009 1.006
asyncio_pingpong 55132.6 1.033 1.033 1.031 1.033 1.031 1.026 1.026
recursion_depth200 89538.8 1.032 1.029 1.029 1.029 1.029 1.029 1.029
recursion_depth600 67003.3 1.031 1.029 1.029 1.029 1.029 1.029 1.029
exception_unwind10 15875.6 1.055 1.055 1.055 1.047 1.049 1.057 1.057
init_shim 1197.9 1.096 1.101 1.099 1.095 1.101 1.100 1.100
property_get 378.8 1.095 1.038 1.035 1.035 1.035 1.035 1.038
sort_key_callback 500.5 1.236 1.166 0.948 0.948 0.984 0.984 0.985
del_finalizer 1509.6 1.054 1.048 0.972 0.976 0.987 0.987 0.988
frame_object_access 1362.6 1.067 1.067 1.060 1.077 1.066 1.064 1.066
gen_close_finally 4053.6 1.034 1.036 1.039 1.036 1.020 1.019 1.029
geomean 1.080 1.067 1.046 1.043 1.042 1.041 1.053

idea(skip-discarded-none) helps plain generator loops; idea(direct-argument-binding) helps C entry; idea(follow-delegation-on-resume) helps delegation but costs plain resumes, partly recovered by idea(share-resume-opcode-loads); idea(create-generators-in-place) helps short-lived coroutines. idea(resume-loads-before-stores) replaces vector moves with scalar words and must be judged by cycles/time as well as instruction count. These changes remain in 21e3701; idea(snapshot-before-restore) does not.

Runtime

pyperf -p 10 -n 3 -w 1, separately trained builds. Absolute times are per run(1000) batch, not per individual operation. The A/A column is a second Pablo's-main run. The original summary reports a 1.01x A/A geomean with 10 of 17 rows not significant; the lower-is-better table gives 0.99.

workload Pablo's main, time per run(1000) Pablo's main A/A our branch after round 2 (148a0f5) +Idea 1 (skip-discarded-none) +Idea 3 (direct-argument-binding) +Idea 5 (follow-delegation-on-resume) +Idea 2 (create-generators-in-place) +Idea 5b (share-resume-opcode-loads) (4c5b8b6) our branch now (+Idea 6b (resume-loads-before-stores), 21e3701)
call_return 18.4 us 0.97 0.94 0.95 0.93 0.92 0.96 0.98 0.90
call_return_kwargs 26.9 us 0.94 0.93 0.93 0.98 0.98 0.97 0.93 0.94
gen_for_loop 14.4 us 0.89 0.97 0.97 0.99 0.98 1.01 1.01 0.95
gen_send 25.6 us 0.99 1.74 1.74 1.77 1.74 1.79 1.82 1.03
next_from_c 24.9 us 1.01 1.02 0.96 0.99 0.96 1.00 0.96 1.04
yield_from_depth5 58.5 us 1.00 0.99 1.00 1.02 1.09 1.10 1.11 0.97
await_chain 163 us 0.98 1.08 1.10 1.05 1.14 1.10 1.10 1.04
asyncio_pingpong 4.10 ms 1.00 0.99 1.03 1.02 1.02 1.04 1.03 1.03
recursion_depth200 4.18 ms 1.03 0.98 1.01 0.95 1.00 1.02 1.00 1.01
recursion_depth600 9.88 ms 1.02 0.31 0.32 0.31 0.31 0.33 0.31 0.31
exception_unwind10 631 us 1.02 1.10 1.09 1.08 1.07 1.11 1.05 1.08
init_shim 45.7 us 1.00 1.11 1.13 1.15 1.11 1.17 1.13 1.12
property_get 13.7 us 1.02 1.00 0.99 1.00 1.00 1.05 1.01 1.00
sort_key_callback 19.1 us 1.00 1.13 1.17 0.98 0.94 0.99 0.97 0.97
del_finalizer 56.8 us 1.01 1.20 1.18 1.05 1.01 1.06 1.04 1.08
frame_object_access 53.8 us 1.01 1.06 1.07 1.08 1.06 1.09 1.13 1.08
gen_close_finally 155 us 1.02 1.21 1.30 1.24 1.21 1.14 1.09 1.06
geomean 0.99 1.00 1.01 0.99 0.99 1.01 1.00 0.95

At 4c5b8b6, the geomean matches Pablo's main within A/A noise, but deep recursion (0.31) and calls (0.93–0.98) offset gen_send 1.82, init_shim/frame access 1.13, delegation 1.11 and await 1.10. After idea(resume-loads-before-stores), the geomean is 0.95: 5% faster on this suite. Remaining rows above 1.05 are init_shim 1.12, unwind/finalizer/frame access 1.08, and generator close 1.06.

On macOS arm64, Apple clang 21, freshly trained builds: 4c5b8b6 is 1.02x faster than Pablo's main, and 148a0f5 is 1.00x. The earlier 1.066x for 148a0f5 used an older main profile; training contributes to that difference.

The generator-resume stall

Through 4c5b8b6, pyperf reports gen_send at 1.74–1.82x, while perf stat reports 1.12x cycles and a plain timing loop 1.16x.

  • It disappears with PYTHONMALLOC=malloc or under perf stat. In pyperf, registering three particular workloads before gen_send gives 45 us; registering the same three after it gives 26 us.
  • An initial profile blamed tuple_traverse, but no GC runs in the loop. LTO symbol attribution was misleading.
  • In one process, with one generator, a sweep of 0–239 extra Python frames gives 24 us at most depths, 32 us at 95, and 43 us at 139. Pablo's main gives 18–22 us throughout that run. The pyperf worker lands at a slow depth.
  • The proposed pair is the heap's gi_iframe and a control slot at base + depth * 40: a store followed by a load with matching low 12 address bits can stall on Zen 4. The original estimate of roughly one affected depth per hundred was a layout observation, not a guaranteed period.

With idea(resume-loads-before-stores) the measured sweep is 22–25 us throughout and pyperf moves from 1.82 to 1.03. The aggregate attempt instead spills and moves the slow depth to 70. This establishes the benefit in those runs; later rounds find other depth-dependent bands and correct broader layout claims. The effect did not appear on arm64. Task.__step uses this resume path, but that alone does not attribute the asyncio gap to aliasing.

Tail-call interpreter measurement

Only Apple clang 21 here accepts --with-tail-call-interp; Linux has clang 18. Pablo's main with tail calls and PGO+LTO is itself 1.15x slower: recursion_depth200 2.34x, yield_from_depth5 1.95x, exception_unwind10 1.58x slower; sort_key_callback 1.43x faster. This matrix cannot isolate dual-stack. The proposed follow-up requires clang 19 on Linux.

Verification and interpretation

Every step passed verify.sh quick. The full checker suite at 4c5b8b6 fails only the known generator-size test. At 21e3701, quick checks and generator tests with refleak checking pass; the generator refleak runs are clean.

idea(skip-discarded-none), idea(create-generators-in-place), idea(direct-argument-binding) and idea(follow-delegation-on-resume) remove work that Pablo's main also performs; independent parent-side gains still need measurement at this point. idea(resume-loads-before-stores) targets the split's transfer. The starting comparison shows plain calls near parity, generators +9–19%, and C entry +24%; after the generic changes, generator instructions are +3–15% and C entry is below parity. These comparisons do not prove that the split itself produced the generic gains.

maurycy and others added 3 commits October 25, 2026 09:53
When the saved record on the heap and the control slot share the low
12 bits of their addresses, every load from the saved record issued
after a store to the slot waits for that store to retire. On Zen 4 this
made gen_send 1.8x slower at one call depth in about a hundred, which
is the depth pyperf's worker happens to run at. Reading the whole record
into registers first removes the load-after-store pattern.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Second attempt at removing the load-after-store pattern between the
saved record on the heap and the control slot. The struct-copy version
made gcc spill the record to the C stack; five explicit 64-bit words
stay in registers. The stack pointer word is adjusted in a register
before the stores when a value is pushed.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@maurycy maurycy self-assigned this Oct 25, 2026
The yield side of the record transfer interleaved loads from the control
slot with stores into gi_iframe, the same load-after-store shape that
exp6b removed on the resume side. Load the fields first.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…per unwind

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@maurycy

maurycy commented Oct 25, 2026 •

Copy link
Copy Markdown
Owner Author

Round 4: frame ownership, binding and independent comparisons

Taking ownership before the record pop reduces frame-object and unwinding work. Combining the __init__ exit dispatch does little; binding its arguments directly helps. The data-chunk cache also speeds Pablo's main on its own.

Pablo's main is aed4ad8; ratios use it unless stated. The initial end-of-round build is 51c8b2a, on top of 21e3701; the binding addendum advances it to a62993e.

Two builds contain no split:

  • parentc5: Pablo's main plus only the four-chunk cache, ported by hand.
  • parentctl, branch parent-ctrl at a253819: Pablo's main plus idea(skip-discarded-none) (discarded-None resume) and idea(direct-argument-binding) (exact positional C calls).

The second comparison omits other generic changes on our branch. It measures the difference between these builds, not the split in isolation. The layout interpretation below was also superseded by the corrected round-6 diagnostic.

Changes

idea(yield-loads-before-stores), be617f51f87: load before saving at yield. _PyGen_UnlinkFrame first reads the live fields, then writes gi_iframe, mirroring idea(resume-loads-before-stores)'s resume ordering. Instruction counts do not change. The initial one-generator sweep is flat; a fresh-generator sweep retains one slow depth out of 240 at 1.39x, down from 1.8x before idea(resume-loads-before-stores).

idea(take-ownership-before-pop), f97f0763bc5: transfer ownership before the pop. A materialised frame previously retained its RESERVED record and adjustment pair through clearing, including traceback levels. _PyFrame_TakeOwnership now copies record and locals while they remain on the stacks, without running Python. Pop the record, then release our reference. A uniquely held frame object is released after the pop; its deallocator does not dereference a frame it does not own.

idea(single-dispatch-init-exit), 151b5e9d8f8: one dispatch for the __init__ exit. EXIT_INIT_CHECK combines _EXIT_INIT_CHECK + _MAKE_HEAP_SAFE + _RETURN_VALUE. The shim's RETURN_VALUE remains for the error path.

51c8b2af8d0 adds frame_noescape (sys._getframe() without retention), return_depth10 (the unwind chain returning normally), and exception_unwind20.

Each step passed quick verification plus generator/tracing tests. The full checker suite at 51c8b2a fails only the known test_sys size expectation.

Instructions

One x86-64 perf stat comparison of all builds. Pablo's main is absolute instructions/iteration; other columns are ratios.

workload Pablo's main, instr/iter + chunk cache + Idea 1 (skip-discarded-none) + Idea 3 (direct-argument-binding) our 21e3701 (Idea 6b (resume-loads-before-stores)) + Idea 7 (yield-loads-before-stores) + Idea 8 (take-ownership-before-pop) our branch now 51c8b2a (+ Idea 9 (single-dispatch-init-exit))
call_return 442.2 0.997 0.997 1.036 1.033 1.036 1.036
call_return_kwargs 703.9 0.999 0.996 1.055 1.054 1.054 1.056
gen_for_loop 310.0 0.997 0.936 1.168 1.165 1.164 1.165
gen_send 546.0 0.998 1.031 1.147 1.145 1.144 1.145
next_from_c 602.9 0.999 0.957 1.115 1.113 1.113 1.113
yield_from_depth5 1230.0 1.007 1.014 1.135 1.133 1.134 1.133
await_chain 3668.0 0.999 0.980 1.008 1.010 1.012 1.008
asyncio_pingpong 55183.9 1.003 0.999 1.027 1.026 1.032 1.026
recursion_depth200 89539.4 1.000 1.000 1.029 1.029 1.031 1.031
recursion_depth600 67065.1 0.999 1.000 1.028 1.028 1.030 1.030
exception_unwind10 15844.7 1.000 0.992 1.060 1.051 1.043 1.035
exception_unwind20 28150.6 0.998 0.998 1.059 1.060 1.043 1.045
init_shim 1216.1 1.000 0.997 1.085 1.084 1.079 1.077
property_get 373.1 0.997 0.997 1.053 1.051 1.053 1.054
sort_key_callback 507.8 1.000 0.822 0.970 0.970 0.971 0.972
del_finalizer 1505.0 1.001 0.940 0.991 0.991 0.991 0.988
frame_object_access 1375.0 1.011 1.002 1.064 1.054 1.040 1.041
frame_noescape 1018.9 1.001 1.001 1.060 1.059 0.986 0.986
return_depth10 4898.0 1.000 1.000 1.034 1.034 1.036 1.036
gen_close_finally 4040.3 1.003 1.002 1.031 1.033 1.036 1.035
geomean 1.000 0.982 1.057 1.055 1.050 1.049

idea(take-ownership-before-pop) moves its target rows: frame noescape 1.060 → 0.986, frame access 1.064 → 1.040, and unwind rows approximately 1.060 → 1.043. idea(single-dispatch-init-exit) is marginal (init 1.079 → 1.077); idea(yield-loads-before-stores) changes no instruction count. All were kept.

Runtime

Twenty workloads, x86-64 pyperf; A/A noise is about 3% per row. Absolute times are per run(1000) batch; ratios use Pablo's main.

workload Pablo's main + chunk cache + Idea 1 (skip-discarded-none) + Idea 3 (direct-argument-binding) our branch now 51c8b2a
call_return 18.2 us 0.94 0.96 0.97
call_return_kwargs 25.9 us 0.97 0.95 1.06
gen_for_loop 14.2 us 1.00 0.83 0.92
gen_send 24.8 us 1.00 1.00 1.05
next_from_c 25.4 us 1.00 0.89 1.00
yield_from_depth5 58.0 us 0.93 0.91 0.93
await_chain 159 us 1.00 0.96 1.00
asyncio_pingpong 4.12 ms 1.00 1.01 1.03
recursion_depth200 4.26 ms 1.00 1.00 1.03
recursion_depth600 9.92 ms 0.32 1.03 0.32
exception_unwind10 658 us 1.00 1.00 1.04
exception_unwind20 1.17 ms 1.06 1.05 1.06
init_shim 46.1 us 1.00 1.03 1.12
property_get 14.2 us 0.98 0.99 1.00
sort_key_callback 18.9 us 1.00 0.81 1.00
del_finalizer 56.8 us 1.04 0.96 1.05
frame_object_access 53.2 us 1.03 1.04 1.07
frame_noescape 35.7 us 1.02 1.03 1.03
return_depth10 227 us 1.03 0.98 1.06
gen_close_finally 153 us 1.02 1.00 1.07
geomean 0.95 0.97 0.97

pyperf describes 0.95 as “1.05x faster” and 0.97 as “1.03x faster”. Round 3's 0.95 geomean used 17 workloads; this run uses 20.

What the comparisons establish

  • Chunk reuse is independent of dual-stack. Pablo's main plus that change has unchanged instruction counts, recursion_depth600 at 0.32, and other rows within noise: roughly a 3x deep-recursion improvement.
  • Generic changes matter. Pablo's main plus idea(skip-discarded-none) + idea(direct-argument-binding) has a 0.97 runtime geomean, the same rounded value as our branch. Its target rows are gen_for_loop 0.83, sort_key_callback 0.81 and next_from_c 0.89.
  • Our branch / that comparison build, instructions: geomean 1.07; generator loop 1.24, sort callback 1.18, C next 1.16, delegation 1.12, send 1.11, init 1.08, plain calls 1.04, deep recursion 1.03. Runtime ratios for generator loop, C next, sort callback and init are 1.11, 1.12, 1.23 and 1.09.

Correction: the original text gave runtime geomean 1.00 “once the chunk cache row is excluded”. Recomputing from the displayed, rounded rows gives 0.997 for all 20 rows, but 1.061 for the 19 rows excluding recursion_depth600. The measured table is unchanged; the exclusion claim was wrong. This arithmetic is not a new benchmark run.

The comparison's instruction overhead is concentrated in generator transitions and C entry, but the builds differ in generic optimisations too. It should not be called an isolated 7% split cost. macOS arm64, Apple clang 21, PGO+LTO remains 1.02x faster than Pablo's main, unchanged from round 3.

Depth sweeps: observations retained, attribution superseded

At this stage, the remaining 1.39x depth suggested transfer-adjacent load/store pairs. Follow-up sweeps used 200k sends per depth, 240 depths, warm-up, two passes and the best of five repetitions:

build one long-lived generator new generator per depth
Pablo's main none (one depth at 1.15x in one pass only) depths 156, 157 at 2.2x to 2.45x, both passes
our branch at 4c5b8b6 (before Idea 6b (resume-loads-before-stores)) 33 depths above 1.15x, worst 1.85x and 1.91x depths 79 (1.6x), 96, 170
our branch now one depth at 1.24x to 1.33x one depth at 1.17x to 1.2x

Our slow depth moves from 32 to 90 across processes with the same generator address. ls_bad_status2.stli_other rises from 100M to 140M and from 125M to 189M for 20M sends. Pablo's main shows 98M vs 61M interlocks at its recorded slow depths. Our residual is one depth in 240 at roughly 1.2–1.3x.

The 2.2–2.45x band at depths 156/157 appeared in one two-pass run of Pablo's main, then failed to repeat in a later process using the same binary. The claimed 15% baseline shift with C-stack placement came from the flawed diagnostic corrected in round 6. These observations do not identify the C-stack entry object as the partner, and the counter increase alone does not name the pair. Round 5's entry-data move tested that hypothesis; the corrected round-6 sweeps supersede its attribution.

Before the following addenda, pyperformance, free-threaded performance, the 32-byte record and a comparison build also carrying idea(create-generators-in-place) and idea(follow-delegation-on-resume) were unmeasured.

pyperformance: 37 benchmarks

x86-64, PGO+LTO, defaults. The subset targets frames, generators, coroutines and calls; benchmarks requiring potentially incompatible C-extension dependencies on 3.16a1 were excluded. Values retain pyperf's wording; parent2 means a second Pablo's-main run. n.s. means not significant.

Pablo's main A/A Pablo's main + Idea 1 (skip-discarded-none) + Idea 3 (direct-argument-binding) our branch 51c8b2a
geomean 1.00x 1.01x faster 1.01x slower
generators 1.02x faster 1.04x faster 1.15x faster
async_generators 1.01x slower 1.01x faster 1.06x slower
async_tree_none / io / memoization / cpu_io_mixed n.s. n.s. (none 1.03x slower) 1.05x / 1.04x / 1.03x / 1.03x slower
coroutines 1.03x slower 1.01x faster 1.02x slower
pprint_pformat / pprint_safe_repr n.s. / 1.01x faster n.s. 1.04x / 1.03x slower
go 1.01x slower 1.01x faster 1.03x slower
richards / richards_super n.s. 1.02x faster / n.s. n.s. / 1.02x slower
raytrace, nbody, fannkuch, spectral_norm n.s. 1.05x, 1.05x faster, n.s., n.s. 1.02x faster each (fannkuch, spectral_norm too)
logging_silent 1.02x faster 1.07x faster 1.02x faster
regex_v8, unpack_sequence n.s. 1.06x, 1.04x faster n.s.
xml_etree_*, json_loads, tomli_loads n.s. mixed, within 5% 1.01x to 1.03x slower

Our branch is about 1% slower than Pablo's main and 2% slower than Pablo's main plus idea(skip-discarded-none) + idea(direct-argument-binding) on this subset. Asyncio-heavy rows are 3–6% slower; generators is 15% faster. The results are consistent with coroutine transitions through Task.__step being relevant, but do not attribute the entire gap to one path. They quantify the previously reported small pyperformance regression at 51c8b2a.

idea(direct-init-binding), a62993e8874: exact positional __init__ binding

CALL_ALLOC_AND_ENTER_INIT used general _PyEvalFramePushAndInit binding even for exact positional signatures. It now writes self and arguments directly, as idea(direct-argument-binding) does for C calls. The unnecessary adjustment pair around positional binding is also removed: that binding cannot run Python. Direct binding is generic; removing the control-limit adjustment is split-specific.

instr, Idea 9 (single-dispatch-init-exit) instr, Idea 10 (direct-init-binding) time, Idea 9 (single-dispatch-init-exit) time, Idea 10 (direct-init-binding)
init_shim 1.077 0.970 1.12x slower not significant
geomean (20) 1.049 1.042 1.03x faster 1.04x faster

At a62993e, quick verification and class, descriptor, opcache, C-API, tracing and traceback tests pass under the checker.

maurycy and others added 3 commits October 25, 2026 11:54
CALL_ALLOC_AND_ENTER_INIT pushed the __init__ frame through the general
_PyEvalFramePushAndInit path even when the signature is exactly positional,
and adjusted the recursion limit around it although positional binding
runs no Python code. Bind self and the arguments directly in that case,
and drop the adjustment pair in the other.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…s from C

_PyEval_Vector pushed the interpreter entry record and the callee's frame
with two bounds checks and two pushes. When one check shows room for
both records and the data frame, push them unchecked; the boundary case
keeps the separate pushes. Also adds four asyncio workloads that separate
task churn, suspension and plain callbacks.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…IFT)

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
maurycy and others added 5 commits October 25, 2026 13:27
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
The interpreter entry record pointed its data at a struct in the eval
loop's C frame. A diagnostic build that moved that struct in 64-byte
steps moved both the depth-dependent stalls of generator resume and the
baseline time by up to 15%, so the C stack layout was deciding
performance. The entry now owns six slots on the data stack (header plus
the result slot), pushed with the record; its address follows the stack
depth like every other frame. The unused f_locals field keeps a C stack
anchor for out-of-process readers, and invariant E1 now checks the data
is in a chunk and the anchor is in the C stack. The merged reservations
for calls from C and generator resume account for the extra slots.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Taking the address of next_instr pinned the interpreter's instruction
pointer to memory for the whole eval loop, which cost 4 to 8 percent of
instructions on every workload.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@maurycy

maurycy commented Oct 25, 2026 •

Copy link
Copy Markdown
Owner Author

Round 5: reservation size, paired C entry and a flawed layout diagnostic

The durable results are the smaller reservation's deployment benefits and idea(reserve-entry-and-callee)'s reduction in C-entry work. The entry-shift diagnostic was flawed; its raw observations are retained below, with the later correction. Moving entry data to the data stack added instructions without closing the asyncio gap and was reverted.

Pablo's main: aed4ad8. Our starting point: a62993e. Comparison builds are parentctl (Pablo's main + idea(skip-discarded-none) + idea(direct-argument-binding)) and parentc5 (Pablo's main + chunk cache). New commits: idea(reserve-entry-and-callee) 3e68b9b, diagnostic 10769dd, reservation constant 6d57731, idea(entry-data-on-data-stack) 9537df8 and idea(entry-data-with-frame-anchor) a6530e0 (both reverted).

Reservation and thread creation

Linux x86-64: 2000 sequential thread start/join pairs, 200 live threads at a barrier, then thread creation under RLIMIT_AS in child processes. _PY_CONTROLSTACK_RESERVE_SHIFT becomes configurable in 6d57731; this round leaves its default at 30.

Pablo's main 64 MiB 128 MiB 1 GiB (as shipped)
VmSize at startup 18 MB 82 MB 146 MB 1042 MB
thread create + join 49.4 us 66.4 us 66.4 us 72.2 us
200 threads alive, VmSize 13.8 GB 26.7 GB 39.6 GB 219.7 GB
200 threads alive, RSS 15 MB 16 MB 16 MB 16 MB
RLIMIT_AS at which thread creation works 256 MB 256 MB 512 MB 4096 MB
recursion to 1.4M frames (limit 1.5M) ok
pyperf call/recursion rows same as 1 GiB

The 1 GiB reservation prevents thread creation under a 2 GB address-space limit and adds 1 GiB of virtual address space per thread. At 64 MiB, the tested 1.4-million-frame recursion still works and call/recursion timing and instructions are unchanged. Its tested RLIMIT_AS threshold matches Pablo's main. RSS stays near the values shown above; reservation is not resident memory.

About 17 us per thread remain at 64 MiB. The proposed explanation is mapping/protection/teardown overhead; a released-control-stack cache is the next experiment. Round 6 measures that cache.

idea(reserve-entry-and-callee): reserve entry and callee together

_PyEval_Vector used two checks and pushes. At 3e68b9b, one successful capacity check permits both unchecked pushes; boundary cases retain the old path. Instructions: sort_key_callback 0.973 → 0.947, del_finalizer 0.995 → 0.983; other rows do not move. Kept.

Asyncio churn

The added workloads separate immediate task completion, suspension, tree shape and callbacks:

  • asyncio_short_tasks: gather tasks that return immediately.
  • asyncio_short_tasks_sleep0: one suspension each.
  • asyncio_tree_gather: depth-3, fan-out-3 tree, resembling pyperformance's async_tree.
  • asyncio_callbacks: call_soon callbacks without coroutines.
workload instructions vs Pablo's main time vs Pablo's main
asyncio_short_tasks 1.011 1.04x to 1.05x slower
asyncio_short_tasks_sleep0 1.015 1.05x slower
asyncio_tree_gather 1.012 1.03x to 1.04x slower
asyncio_callbacks 1.002 not significant

The original interpretation was “stalls, not work”: about 1–1.5% more instructions versus 4–5% more time, with callbacks at parity. The table supports investigating latency and measurement variance, not attributing the gap to C-stack placement. Later in-process measurements and the corrected cross-process task experiment supersede that attribution.

Entry-shift diagnostic: raw results, invalid causal conclusion

After idea(resume-loads-before-stores) and idea(yield-loads-before-stores), a send could remain 1.2–1.4x slower at one depth in 240, moving across processes. 10769dd, enabled with -DDUALSTACK_ENTRY_SHIFT, attempted to select among eight C-stack entry-data positions through an environment variable. Two-pass sweeps used one long-lived generator and 200k sends per depth.

Correction: the array, not each element, was 64-byte aligned. The nominal offsets below were not independent, equally aligned placements: our actual stride was 56 B, and each configuration used a new process. Round 6 fixes both problems. The table keeps the original observations and labels the shifts as nominal.

nominal entry shift (B; see correction) reported slow depths baseline ns/send
0 none 25.4
64 none 28.9
128 none 25.0
192 85 25.1
256 120 (one pass) 25.9
320 13, 14, 22 25.2
384 35, 60, 61, 97 28.9
448 none 25.0
reference, no diagnostic 60, 97, 171, 172 25.6

The original interpretation tied moving slow depths and the two 28.9 ns baselines to entry placement, claiming a 15% baseline effect. The parent-ctrl diagnostic, described then as Pablo's main, showed 22.0–25.4 ns/send and no repeated slow depth. The previously recorded 2.2–2.45x band did not recur in another run of the same binary.

These runs motivated the proposed pair of C-stack entry data and base + depth * 40. They do not establish it: alignment and process layout changed together. The claim that both builds have an entry-placement lottery is withdrawn.

idea(entry-data-on-data-stack) and idea(entry-data-with-frame-anchor): entry data on the data stack, reverted

The entry allocates six data-stack slots (header plus result), pushed and popped with its record. Its unused f_locals stores a native C-stack anchor. E1 checks chunk-resident data and the native anchor; paired reservations in idea(reserve-entry-and-callee) and generator resume account for the extra slots.

  • 9537df8: using &next_instr as the anchor forces the interpreter instruction pointer into memory. Instruction geomean rises 1.038 → 1.069; call_return rises 1.038 → 1.074.
  • a6530e0: __builtin_frame_address(0) restores the geomean to 1.040, but data push/pop adds C-entry work: send 1.147 → 1.172, C next 1.112 → 1.135, sort callback 0.947 → 0.967. Against that day's freshly measured Pablo's main, time is 1.13x slower for C next and 1.12x for send; short tasks/sleep0/tree are 1.06x/1.03x/1.02x slower, callbacks 1.05x faster. Asyncio does not improve. Fresh-generator sweeps become flat (idea(reserve-entry-and-callee) had depths 31/81); the long-lived-generator sweep retains two depths at 1.16x.

Correct comparison, added in round 6: those timing ratios were against a new Pablo's-main run, not idea(reserve-entry-and-callee). Direct pyperf compare_to of 3e68b9b and a6530e0 reports send, await, short tasks and sleep0 as not significant; C next 1.03x faster, sort callback 1.05x slower, finalizer 1.07x faster. The C-entry instruction increase remains; the earlier timing attribution was wrong.

Both commits stay reverted: more work, no closure of the asyncio gap. The sweep change removes one observed pattern, but the flawed shift diagnostic cannot identify its aliasing partner.

End of the round

Kept: idea(reserve-entry-and-callee), the asyncio workloads, the reservation constant with unchanged default, and the diagnostic option disabled by default. The branch returns to that state after reverting idea(entry-data-on-data-stack) and idea(entry-data-with-frame-anchor). The measurements support reducing the reservation to 64 MiB; the profiler benefit remains unmeasured because its reader is not ported. The claimed layout explanation and stall-only asyncio diagnosis are superseded by round 6.

… run time

_Alignas(64) on the array aligned only its first element, so the eight
positions were sizeof(entry data) = 56 bytes apart and the field
alignment changed with the shift. Each slot is now a 64-byte aligned
wrapper. The shift comes from the global _Py_dualstack_entry_shift,
which a script can set through ctypes, so positions can be compared
inside one process; the address used is published in
_Py_dualstack_entry_addr.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
maurycy and others added 8 commits October 25, 2026 15:59
frame_return_drop and frame_return_fback split frame_object_access into
escape without use and escape with f_back. gen_finish_next and
gen_finish_close compare normal exhaustion with close(); the _frame_held
and _frame_released variants keep or drop gi_frame around the finish.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…d is written

gen_send_ex2 restored the record into the slot, then read the slot to
decide whether RESUME_CHECK and POP_TOP can be skipped, then stored the
new instruction pointer or loaded the stack pointer back from the slot to
push the sent value. The decision is now made from the saved record in
the generator, and _PyGen_LinkFrame takes the final instruction pointer
so the slot is written once with its final contents. The inlined resume
and the yield-from chain use the same parameter instead of a second
store.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
64 MiB holds 1.6 million 40-byte records. On the Linux box the smaller
reservation kept the measured call and recursion performance, ran the
1.4-million-frame recursion test, created threads faster and let the
thread test run under a 256 MB address-space limit, which the 1 GiB
reservation failed. The constant stays overridable at build time.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
gen_send_ex2 and gen_send_ex now produce a PySendResultPair; the
PyObject ** forms are thin wrappers for the throw and close paths.
_PyGen_SendNone resumes an exact generator or coroutine with None and
returns the pair in registers. task_step_impl uses it for exact
coroutines and generators instead of PyIter_Send, which dispatched
through am_send and wrote the result through a pointer into the Task
step's C frame. The state checks and exc_info chaining are unchanged.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
A finished thread's control stack goes into a per-interpreter cache of
four entries, kept mapped with its committed pages, and the next thread
created in that interpreter takes it instead of calling mmap and
mprotect; munmap is skipped as long as the cache has room. Stacks that
committed more than 1 MiB are unmapped rather than trimmed. The cache is
emptied when the interpreter is freed and after fork in the child, where
the lock is also reinitialised. Debug builds poison the committed range
of a cached stack.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
With the extra parameter the compiler stopped inlining it in the PGO+LTO
build (a separate _PyGen_LinkFrame symbol with two call sites appeared),
which cost 18 more instructions per gen.send from C. The resume helpers
now have one call site each and the function is always inlined.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
In the PGO+LTO build the loads of the five saved words were merged into
16-byte vector loads (movdqu, movhps) in the out-of-line yield-from
chain helper, while the previous suspend had written the same fields
with 8-byte scalar stores. Such loads cannot be forwarded from the
store buffer: gen_for_loop showed 6.0 store-to-load interlocks per
iteration against 2.0 on Pablo's main, and 85% of the chain helper's
instruction samples sat on one of those loads. An empty asm statement
pins the words to general registers, and the chain helper is compiled
without SLP vectorisation like the eval loop.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@maurycy

maurycy commented Oct 25, 2026 •

Copy link
Copy Markdown
Owner Author

Round 6: corrected diagnostics, thread-stack reuse and negative results

The corrected in-process diagnostic does not reproduce round 5's entry-placement lottery. The smaller reservation and released-stack cache solve the measured address-space and thread-churn costs. Resume helper changes do not produce a demonstrated performance win; the 32-byte record and forced scalar loads lose in the tested forms.

Pablo's main: aed4ad8; Pablo's branch: 48370ac. Our branch starts at 2e3718c (idea(reserve-entry-and-callee), reservation constant and diagnostic option) and ends at 05ef739. The comparison build is Pablo's main + idea(skip-discarded-none) + idea(direct-argument-binding), on parent-ctrl.

Linux, AMD Zen 4, GCC 13, PGO+LTO. Instructions use perf stat; pyperf uses 10 processes × 3 values. Ratios are our branch / Pablo's main unless another reference is named.

Corrected entry placement

_Alignas(64) aligned only array element zero. The supposed 64-byte steps were 56 B on our branch (40-byte control record) and 96 B on the reference build. Field alignment changed, and each shift used a fresh process. That invalidates the earlier attribution of the 15% baseline swing to entry placement.

b774eed (ours) and d3f0452 (Pablo's main + idea(skip-discarded-none) + idea(direct-argument-binding)) use aligned slot wrappers, a stride _Static_assert, a ctypes-settable runtime global and publication of the address actually used.

perf stat -p PID -D -1 --control=fifo:ctl,ack attaches after warm-up; counters bracket each series without re-exec. perf opens both FIFOs read-write/non-blocking and returns ack\n\0; waiting for a response ending only in ack\n deadlocks.

In-process placement sweeps

One depth, 2M sends per series, four shuffled repetitions, taskset -c 5, perf attached per series. Shift mode holds one generator and selects eight aligned entry slots.

Table-label correction: the original diagnostic tables called d3f0452 “Pablo's main”. The build specification above identifies it as Pablo's main + idea(skip-discarded-none) + idea(direct-argument-binding). The labels below are corrected; measurements are unchanged.

build ns/send (min over reps) cycles/send instructions/send stli_other/send
ours 24.7 - 25.1 across all 8 shifts 118.6 - 120.0 562.0 4.7 - 5.5
Pablo's main + Idea 1 (skip-discarded-none) + Idea 3 (direct-argument-binding) 22.6 - 23.1 104.5 - 106.3 504.0 3.0 - 4.4

Bank mode fixes entry placement and selects among 32 live generators covering a 4 KiB page. Our timings are 24.8–25.9 ns without counters and 24.8–25.3 with perf; cycles are 118.6–120.3. The reference diagnostic gives 22.7–24.7 / 21.9–22.75 ns.

At this depth, moving the entry by 0–448 B or changing generator addresses does not reproduce the claimed effect. Our/reference ratios are 1.115 instructions, 1.13 cycles and 1.11 time; IPC is about 4.7 on both. Attaching perf after warm-up does not materially change the placement result. Round 5 mixed misalignment with process-to-process variation.

Ideas

The series builds on 2e3718c. Instruction ratios use 200k iterations.

idea(write-resume-record-once), da7e463, fixed in a728182: write final slot contents once. gen_send_ex2 previously restored the slot, read it to decide whether to skip RESUME_CHECK/POP_TOP, then rewrote IP or reloaded SP to push the value. Decide from the saved record first and pass the final IP to _PyGen_LinkFrame. Inlined resume and delegation also lose their second store.

Initially, send regresses 1.146 → 1.179 and C next 1.112 → 1.141. nm reveals that GCC stopped inlining the helper after its new parameter: two call sites and an outlined symbol, adding about 18 call/spill/reload instructions per resume. One call site per helper plus Py_ALWAYS_INLINE restores inlining. Final ratios: send 1.155, C next 1.124, finish-next 1.086. This is still about five instructions above idea(reserve-entry-and-callee) per C resume because the value is materialised before the push. The slot round-trip is gone; time is neutral. Check outlined helpers after building.

idea(smaller-control-stack-reservation), f0f9929: default to 64 MiB. The build-time override remains. Instruction/time rows match 1 GiB in this comparison; round 5 contains the deployment measurements.

idea(return-resume-pair), c22835b: return a pair from Task resume. gen_send_ex2/gen_send_ex produce PySendResultPair. _PyGen_SendNone returns it in registers; task_step_impl uses it for exact coroutines and generators instead of PyIter_Send's am_send dispatch and C-stack out-parameter. State checks and exception chaining remain.

Disassembly confirms call _PyGen_SendNone; mov %rax,...; mov %edx,%r14d. Asyncio instruction ratios are unchanged: short tasks 1.017, sleep0 1.020, tree 1.017, callbacks 1.000. Timing also moves on untouched rows: call_return 1.12x and recursion-200 1.18x faster. These examples exceed the original summary's “2–8%” range. The comparison does not establish a win from idea(return-resume-pair); it was retained only as the register-return form.

idea(control-stack-cache), b28927e: reuse finished control stacks. A per-interpreter cache keeps four mapped stacks with committed pages. It avoids mmap/mprotect on reuse and munmap while space remains. Stacks committed beyond 1 MiB are unmapped. Interpreter teardown empties the cache; a fork child clears it and reinitialises its lock. Debug builds poison the committed range.

Thread tests use taskset -c 4-7, minimum of 3 × 5000 threads:

Pablo's main Pablo's main + Idea 1 (skip-discarded-none) + Idea 3 (direct-argument-binding) our branch, 64 MiB, no cache (Idea 14 (smaller-control-stack-reservation)) with cache (Idea 16 (control-stack-cache))
serial 47.1 us 46.3 63.0 46.9
8 at a time 53.4 53.1 64.4 53.6
64 at a time 58.1 58.5 71.5 58.7
syscalls per 1000 threads (mmap / mprotect / munmap) 2023 / 1007 / 2007 1024 / 8 / 1007

The remaining syscalls correspond to glibc thread stacks, also present on Pablo's main. With 200 live threads, VmSize is 26.7 GB versus 13.8 GB; after joining, 12.6 versus 12.3 GB. The cache retains 4 × 64 MiB. Threading, fork and interpreter tests pass under the full checker; single-thread instruction rows are unchanged.

The combined build labelled head6 (a728182) has instruction geomean 1.042, equal to the round-5 head, and time 1.02x faster. Generator loop/send/C next/await are 1.08x/1.06x/1.06x/1.08x faster; asyncio is flat. idea(write-resume-record-once) and idea(return-resume-pair) have no demonstrated runtime benefit. idea(smaller-control-stack-reservation) and idea(control-stack-cache) have measured address-space and thread-churn benefits.

Frame escape and generator finish

Six new workloads supplement the existing rows below. Instructions compare 2e3718c with Pablo's main; head6 agrees within 0.01. Times are pyperf medians over 10 processes.

workload what it does Pablo's main instr/iter Pablo's main + Idea 1 (skip-discarded-none) + Idea 3 (direct-argument-binding) ours instr ours time
frame_noescape sys._getframe() created, not returned 1019 0.999 0.988 1.05
frame_return_drop returned, dropped by the caller 1102 1.000 1.044 1.07
frame_return_fback returned, f_back read 1371 1.004 1.043 1.06
frame_object_access (old row) same as fback 1375 1.008 1.040 1.05
gen_finish_next next(g) then next(g, sentinel) (normal exhaustion) 1734 0.987 1.076 1.16
gen_finish_close next(g) then g.close() 4060 1.003 1.038 1.09
gen_finish_next_frame_held as gen_finish_next with g.gi_frame held across the finish 2504 0.987 1.061 1.12
gen_finish_close_frame_held as gen_finish_close, frame held 4435 1.006 1.035 1.07
gen_finish_next_frame_released frame object created and dropped before the finish 2426 0.983 1.050 1.13

Frame-object overhead appears on escape: ownership transfer costs about 48–59 instructions per escaping frame; reading f_back adds no comparable split-specific cost. The noescape instruction row is near parity, although its timing row is 1.05.

Normal generator exhaustion has a larger ratio than close(), which adds exception work on both builds. Holding gi_frame adds little. gen_finish_next is about 1.08 instructions versus 1.16 time, with reproducible cycle ratios 1.14–1.16. This warrants investigating finish-path latency; the table does not identify a particular dependent chain.

Depth sweep

Depths 0–239 run in one process; each added Python frame moves our slot by 40 B. Five configurations: entry shifts 0/2/4 with generator 0, then shift 0 with generators 7/15. perf attaches per depth. The reference diagnostic is again d3f0452, with its table label corrected.

build slow depths (>1.10x of the sweep minimum, timed run) cycles/send at the slow depth vs median (separate perf-attached series)
ours 109 in all five configurations (1.12 - 1.13x) 146 vs 130 (1.12x)
Pablo's main + Idea 1 (skip-discarded-none) + Idea 3 (direct-argument-binding) 152 - 154 in all five configurations (1.13 - 1.25x), plus a dozen 1.10 - 1.11x depths with generator 7 173 - 225 vs 109 (1.6 - 2.1x)

The time and cycle columns come from different series: three timed repetitions followed by one perf-attached run. Reference cycles at the slow band are worse in the latter; the columns have different normalisations.

Neither entry movement by 0/128/256 B nor three heap positions moves the band. A 4 KiB coincidence involving stack regions is a hypothesis consistent with this result, not an identified load/store pair. Both builds show a band, larger in the reference. The earlier attribution to C-stack entry data is withdrawn.

Negative results

idea(stack-pointer-in-data-frame), branch dual-stack-exp17, 207b004: SP in the data frame, 32-byte record. One transfer word is saved, but external SP access becomes a dependent double load. Instruction geomean rises 1.042 → 1.048; calls add six instructions. Runtime is 1.03x slower than head6, with generator loop 1.12x and C next 1.10x. Rebased on idea(scalar-generator-loads) at 2a61b5e, instructions are 1.055 (loop 1.171, send 1.169), time matches idea(scalar-generator-loads) within noise. Reader failures include wrong owners/timeouts despite updated offsets. Not merged.

idea(scalar-generator-loads), 4c4d74e, reverted in 05ef739: force scalar saved-record loads. perf record places 85% of the delegation helper's instruction samples on movhps 0x58(%rax),%xmm1: an 8-byte upper-half load in a 16-byte copy sequence with movdqu 0x50/0x60. The prior suspend uses 8-byte stores. Interlocks are 6.0 per iteration versus Pablo's main's 2.0.

The producer/consumer widths were not fully analysed before the experiment, and a sample pile-up is not causal attribution. Forcing five general-register words removes vector loads and lowers interlocks to 3.0, but cycles rise 77 → 91 (Pablo's main: 90). Fewer interlocks do not make this implementation faster; the vector sequence wins in the measured loop.

Remaining cost and corrections

  • Suite: instructions 1.042, cycles 1.044–1.055 in single-process perf stat, time 1.03–1.05 in pyperf. A dominant suite-wide stall is not needed to explain the aggregate.
  • Individual C-entry/generator paths retain excess cycles: send 1.21–1.27 vs 1.15 instructions; finish-next 1.14–1.16 vs 1.08; close-finally 1.11–1.31 vs 1.04. Send need not terminate the generator.
  • Asyncio: instructions 1.017–1.024, cycles 0.99–1.07 in one process, pyperf 1.05–1.08. The earlier “400k-task gather” claim (+9% instructions, +8.7% cycles) is withdrawn: ast.py shadowed the standard-library module, so both builds measured failed imports. Round 7 replaces it with independent processes and attached counters. Layout/harness variance remains relevant; these rows alone do not allocate the remaining cost among entry, link/unlink and exception handling.
  • Threads reach parity with idea(control-stack-cache).

The working budget was roughly 50 extra instructions per Task step and 85 per C-side resume: entry bookkeeping, record transfers, gi_live_frame and frame-object redirection. The exception chain exists on Pablo's main too; only extra work counts. These are aggregate differences, not proof that every operation is unavoidable. Termination remains a concrete experiment; implicit entry is an unimplemented design affecting the reader. None of these micro-ratios is an application slowdown figure.

Profiler benefit is still unmeasured at this revision. The quoted roughly 4% instruction cost is against Pablo's main; the comparison build also lacks some generic changes.

Round-7 corrections: the generator range “1.11–1.19” omitted gen_for_loop, at 1.155 versus Pablo's main and 1.235 versus the comparison build (itself 0.935). The generator sizeof failure, 192 vs 184, requires reconciling the expectation with the added live-record pointer and saved record; it is not proof that allocation is wrong.

maurycy and others added 2 commits October 25, 2026 18:32
Records carry no previous pointer (that debug offset is 0 on this
branch), so the old chain walk read garbage. For a thread the records
form a contiguous array from the base record to the current one: read
that range with one process_vm_readv and parse the records downward.
Setting DUALSTACK_READER_INDIVIDUAL=1 reads each record separately, to
measure what the single read is worth. Async-aware paths are unchanged.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…tion

When a generator returns and no frame object exists, nothing will resume
it, so clear_gen_frame no longer writes the instruction pointer, return
offset and stack pointer into the saved record and then reads the stack
pointer back to clear the locals. The live stack pointer is read once
before the pop and used directly; the saved record keeps code, data,
owner, the DETACHED state and an in-range stack pointer for inspection
and GC. Escaping frame objects and free-threaded builds keep the old path.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@maurycy

maurycy commented Oct 25, 2026 •

Copy link
Copy Markdown
Owner Author

Round 7: generator finish, a partial reader and profiler measurements

The final branch matches Pablo's main in the micro-suite runtime geomean, but is slower than Pablo's main with selected independent changes. Bulk reads reduce sampler CPU on the tested target; a reduction in combined CPU at equivalent, validated sample quality has not been demonstrated.

Pablo's main is aed4ad8. The comparison build, parent-ctrl at b43d645, adds discarded-None resume, exact positional binding in vectorcall and __init__, and the pair-returning Task helper. Our branch advances from 05ef739 through reader 67cbf69 to generator-finish change 0e82de9.

The comparison build is incomplete: it lacks generator creation in place, delegation following and the data-chunk cache. The original “matched control” and “split alone” labels overstated what these comparisons isolate. Tables retain their measurements with corrected labels.

Linux, AMD Zen 4, GCC 13, PGO+LTO. Instructions: perf stat, 200k iterations. Time: pyperf medians, 10 processes × 3 values. Round-6 corrections are marked in that report.

Interpreter measurements before idea(finish-without-saving-continuation)

The first two columns use Pablo's main; the last divides our build before idea(finish-without-saving-continuation) by Pablo's main + selected optimisations.

Pablo's main + selected optimisations / Pablo's main before Idea 19 (finish-without-saving-continuation) / Pablo's main before Idea 19 (finish-without-saving-continuation) / main + selected optimisations
instructions, geomean of 31 rows 0.982 1.042 1.061
pyperf time, geomean of 24 rows 0.958 1.011 1.055 (direct compare: 1.06x slower)
cycles, single process 0.977 1.028 1.052

The selected changes reduce Pablo's-main runtime by 4.2% on this suite. Before idea(finish-without-saving-continuation), runtime pairs (our branch / comparison build, each relative to Pablo's main) are: finish-next 1.16 / 0.98, sleep0 tasks 1.07 / 1.00, short tasks 1.06 / 1.01, generator loop 0.94 / 0.81, send 1.04 / 0.99, C next 0.98 / 0.89, plain calls 0.95 / 0.95. These are microbenchmarks, not application slowdown estimates.

idea(finish-without-saving-continuation), 0e82de9: finish without saving a continuation

When a generator returns without a frame object, clear_gen_frame no longer saves IP, return offset and SP only to reload SP for clearing. It reads the live SP once before popping. The detached saved record retains code, data, owner and an in-range SP for inspection/GC. Escaping frame objects and free-threaded builds retain the old path.

gen(); next(g); next(g, sentinel), perf attached, three repetitions. head7 is before this change; head9 is after it.

Pablo's main Pablo's main + selected optimisations before (head7) after (head9)
instructions / finished generator 1711 1686 1864 1838
cycles / finished generator 362 - 365 359 - 360 412 - 425 405 - 415
store-to-load interlocks 13 - 17 15 - 18 15 - 20 13 - 15

Finish-next instructions move 1.090 → 1.074 and time 1.162 → 1.117 against Pablo's main (direct comparison: 1.05x faster). Suite geomeans move 1.042 → 1.040 instructions and 1.011 → 0.999 time (direct 1.01x faster; other rows remain inside noise). Kept.

Final ratios against Pablo's main + selected optimisations are 1.059 instructions and 1.043 time. Finish still costs roughly 40–50 extra cycles. Record pop, generator-state update and gi_live_frame reset remain, but the residual is not attributed to each operation separately.

Reader port: measured path works, correctness remains open

The prototype reader follows previous, whose offset here is zero. On a target combining depth-30 recursion and asyncio churn, all 300 attempts fail with process_vm_readv failed at address 0x2 (size 40): 100% reported errors. Shallow/chunk-path tests did not cover this walk.

67cbf69 bulk-reads the contiguous range from base to current record and parses downward using code/IP, skipping entry and base records. DUALSTACK_READER_INDIVIDUAL=1 selects separate record reads for comparison. Async-aware paths are unchanged.

On the same 300-sample attach check, Pablo's main gives 249 non-error samples / 51 errors (bad owner, unreadable address, incomplete chain); our branch returns 300/300 without errors in both modes. This is not oracle validation of the returned stacks. The reader remains incomplete, including async-aware paths and deep-chain limits. The current PR description also records a double free on bulk-read failure.

Profiler: fixed work, separate timing and syscall counts

The target executes 600 units. Each unit performs 40 recursions to depth 30, with 400 leaf calls, then 2000 asyncio tasks in batches of 100 under asyncio.run: about 2.3 s unprofiled CPU.

Each build uses its own python -m profiling.sampling run -r RATE --pstats. Target CPU is its own rusage; total is the driver's child rusage; sampler CPU is their difference. Four interleaved repetitions use taskset -c 10,11.

CPU timings below exclude strace -c. Separate syscall runs give reads/sample: Pablo's main 40–45; Pablo's main + selected optimisations 14–15; our bulk reader 5.5, including metadata; our individual reader 18–20. Tracing penalises additional calls, so those runs are not timing evidence.

unprofiled target CPU 1 kHz: sampler CPU 1 kHz: total CPU 1 kHz: achieved rate, error rate 10 kHz: sampler CPU 10 kHz: total CPU 10 kHz: error rate
Pablo's main 2.22 - 2.34 s 0.28 - 0.40 s 2.60 - 2.78 s 1000 Hz, 13.7 - 15.1% 2.35 - 2.40 s 4.67 - 4.75 s 13.1 - 14.5%
Pablo's main + selected optimisations 2.27 - 2.33 0.26 - 0.27 2.54 - 2.63 1000 Hz, 13.1 - 14.6% 2.35 - 2.43 4.66 - 4.83 13.8 - 14.5%
our branch, one read 2.35 - 2.37 0.21 - 0.22 2.59 - 2.66 1000 Hz, 0.00 - 0.04% 2.43 - 2.55 4.82 - 5.06 0.00%
our branch, per-record reads 0.25 - 0.26 2.65 - 2.78 1000 Hz, 0.00 - 0.04% 2.46 - 2.50 4.89 - 4.95 0.00%
  • At 1 kHz: total CPU ranges overlap, 2.59–2.66 s versus 2.54–2.63 s for the comparison build. The reported sampler saving is about 0.05 s; the target difference is about 0.08 s. This does not establish a combined CPU win or statistical equivalence. The lower total range relative to Pablo's main also includes generic optimisations.
  • Bulk versus individual reads on our build: sampler CPU drops 15–20%, from 0.25–0.26 s to 0.21–0.22 s. The original summary's “3–4 fewer reads/sample” was wrong: recorded counts are 18–20 versus 5.5, a difference of about 12.5–14.5. Counts and timing come from separate runs.
  • At 10 kHz: both samplers use about one core, with reported equal achieved rates. Our total CPU is about 3–5% higher. Roughly 24k versus 20k samples were returned without errors; their stack correctness was not independently validated.

The equal-quality argument was an extrapolation. Reported errors are 13–15% for the reference reader and 0.00–0.04% for ours, but non-error samples are not necessarily valid stacks. The earlier calculation gave about 1.09 ms versus 1.31 ms total CPU per non-error sample (17% less). It projected 1.16x the comparison build's sampling rate, another 0.04 s sampler CPU, and a 1–2% total advantage. These are calculations, not a measured matched-quality comparison; the projected advantage lies inside the repetition spread.

Bulk and individual modes have similar error rates. The difference therefore cannot be credited to batching alone, and one read is not an atomic snapshot. Scope remains one target shape, one sampler, default non-async-aware mode, and the reference reader shipped in the tree.

Asyncio across processes

Twelve independent processes per build, interleaved by block, one warmed event loop each. Attached perf (-D -1, control FIFO) brackets 20 rounds × 5000 tasks, using create_task/gather of 100 trivial coroutines, pinned with taskset -c 9.

build ns per task, median [min .. max over 12 processes] instructions per task, median cycles per task, median ratio to Pablo's main, median of per-block ratios [min .. max]
Pablo's main 1506.8 [1477.5 .. 1549.8] 31287 8053 1.000
Pablo's main + selected optimisations 1521.9 [1504.4 .. 1546.6] 31102 8165 1.011 [0.977 .. 1.030]
our branch 1578.2 [1564.1 .. 1636.6] 31935 8475 1.056 [1.017 .. 1.096]

The per-block comparison gives about 4.5% more time per completed task against Pablo's main + selected optimisations, with +2.7% instructions and +3.8% cycles. The 1.017–1.096 range is the range of ratios to Pablo's main across blocks, not the absolute speed of the fastest/slowest process. Its spread is comparable to the effect. Earlier 1.05–1.08 pyperf ratios lacked this between-process distribution. This still compares builds with some generic differences.

Designs and independent changes

Two designs remain unimplemented: implicit C entry would mark a boundary in the first Python record, add a native-anchor word and exit checks, and retain an anchor-only record while finalizers clear that frame. A persistent Task boundary would keep an evaluator activation across a batch of ready Tasks. Both change lifetime and reader rules; the measured profiler benefit does not yet justify that work.

Independent candidates are discarded-None resume, vectorcall binding (sort_key_callback instructions 0.82), __init__ binding (init_shim 0.91), in-place generator creation via CALL_PY_GEN_EXACT_ARGS, delegation following, and the data-stack chunk cache. Generator creation/delegation still need parent-side ports. The roughly 3x deep-recursion gain is the data-chunk cache's independent benefit. Released control stacks are specific to dual-stack; their cache brings thread churn to parity. The pair-returning Task helper has no measured benefit.

Outcome

At 0e82de9, our micro-suite ratios against the selected-optimisation build are 1.059 instructions and 1.043 time; the task comparison is about 1.045. Threads are at parity and reservation is 64 MiB. Initial generator regressions were around 1.2x; the deep-recursion gain should not be mistaken for removing a split-specific 3x regression. The measured suite-wide runtime improvement in this round is 1–2%; the finish-next row improves more.

The criterion remains a demonstrated reduction in total CPU for fixed useful work at equivalent, validated sample quality, with uncertainty excluding parity. This target does not meet it. Reported errors are lower, but “better samples” has not yet been established. Whether an interpreter difference of roughly 4–6% is acceptable needs a completed reader and application measurements.

Quick verification passes at 0e82de9; later full-suite status is in the PR description. The negative SP-layout variant is on dual-stack-exp17. Tools/dualstack contains the microbenchmark and verifier harnesses. Specialised depth-sweep, thread and profiler drivers, and raw result files, are not included in this PR; the reports preserve their protocols and measured results.

@maurycy maurycy changed the title Dual-stack verifiers: control stack invariant checker and Tools/dualstack Dual-stack performance investigation and prototype profiler reader Oct 25, 2026
@maurycy maurycy changed the title Dual-stack performance investigation and prototype profiler reader Dual-stack prototype: optimisations, benchmarks and profiler experiments Oct 25, 2026
@maurycy
maurycy marked this pull request as draft November 1, 2026 00:41
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.

1 participant