Repository navigation
Conversation
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>
Verifiers: Pablo's main and our branchThe 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 Build strings, retained as recorded: Pablo's main: Our branch, same configure options: Recursion limits
Both differences originate in Pablo's branch. Monitoring traces
All 15 programs match in events, stdout and Checker overheadOur branch only: Pablo's main has no control stack. Eighteen frame-heavy test files,
All three modes give the same test results. Only Initial runtime comparisonBoth builds use Absolute pyperf times are per Interleaved reruns reproduce the direction for
The regression also affects plain calls and property access. Generator/coroutine ratios span 1.13x–1.29x; Not run at this stage
|
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>
This reverts commit 5d9fb6e.
Round 1: return bookkeeping, caller lookup and generator restorePopping 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
Mechanisms and lifetime constraints
The data frame must outlive its record until clearing finishes; the checker's T2 invariant was relaxed accordingly. The first attempt omitted
Round 2's local-stack-pointer variant ( Full measurementsTimes are per
A/A and measurement limitsTwo PGO builds of
Benchmark hidden because not significant (1): dualstack.exception_unwind10 VerificationEach step passed The full checker run at |
Research notes after round 1State: Work introduced by the prototype
Results available then
The A/A builds of
Bugs caught before further benchmarking
Questions at this revision
|
…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>
…tead of keeping it live" This reverts commit a1d2c2a.
Round 2: paired reservations and data-chunk reuseReserving 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 Fresh profiles change the noise estimate
Changes and decisions
The neutral changes above were retained after passing the verifier. Their intended bookkeeping savings did not produce a measured runtime gain. All workload ratiosThe 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.
Outcome and verificationOn the common suite, Pablo's branch is 1.162x and our final build Every step passed |
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>
Round 3: Linux instruction counts and generator restorePlain 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; Linux: AMD Zen 4, GCC 13, PGO+LTO,
Starting pointAbsolute 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.
Generator resume/yield is +9–19%. Ideas, in commit order
The full suite found two bugs missed by quick checks: borrowed method
InstructionsEach column adds to the previous build. Bold retains the original target-row markings.
Runtimepyperf
At On macOS arm64, Apple clang 21, freshly trained builds: The generator-resume stallThrough
With Tail-call interpreter measurementOnly Apple clang 21 here accepts Verification and interpretationEvery step passed
|
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>
…ntrol slot" This reverts commit 81675e4.
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>
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>
Round 4: frame ownership, binding and independent comparisonsTaking ownership before the record pop reduces frame-object and unwinding work. Combining the Pablo's main is Two builds contain no split:
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
Each step passed quick verification plus generator/tracing tests. The full checker suite at InstructionsOne x86-64
RuntimeTwenty workloads, x86-64 pyperf; A/A noise is about 3% per row. Absolute times are per
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
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 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 supersededAt 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:
Our slow depth moves from 32 to 90 across processes with the same generator address. 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 pyperformance: 37 benchmarksx86-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;
Our branch is about 1% slower than Pablo's main and 2% slower than Pablo's main plus
|
| 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.
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>
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>
… next_instr" This reverts commit a6530e0.
… C stack" This reverts commit 9537df8.
Round 5: reservation size, paired C entry and a flawed layout diagnosticThe durable results are the smaller reservation's deployment benefits and Pablo's main: Reservation and thread creationLinux x86-64: 2000 sequential thread start/join pairs, 200 live threads at a barrier, then thread creation under
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 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.
|
| 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_instras the anchor forces the interpreter instruction pointer into memory. Instruction geomean rises 1.038 → 1.069;call_returnrises 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>
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>
…toring it" This reverts commit 4c4d74e.
Round 6: corrected diagnostics, thread-stack reuse and negative resultsThe 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: Linux, AMD Zen 4, GCC 13, PGO+LTO. Instructions use Corrected entry placement
In-process placement sweepsOne depth, 2M sends per series, four shuffled repetitions, Table-label correction: the original diagnostic tables called
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. IdeasThe series builds on
Initially, send regresses 1.146 → 1.179 and C next 1.112 → 1.141.
Disassembly confirms
Thread tests use
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 Frame escape and generator finishSix new workloads supplement the existing rows below. Instructions compare
Frame-object overhead appears on escape: ownership transfer costs about 48–59 instructions per escaping frame; reading Normal generator exhaustion has a larger ratio than Depth sweepDepths 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
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
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
The working budget was roughly 50 extra instructions per Task step and 85 per C-side resume: entry bookkeeping, record transfers, 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 |
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>
Round 7: generator finish, a partial reader and profiler measurementsThe 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 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: Interpreter measurements before
|
| 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.
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% forsort_key_callback.idea(direct-init-binding): exact positional__init__binding reduces instructions on Pablo's main by about 9% forinit_shim. Both binding ideas require review of argument-shape guards and reference ownership.idea(data-stack-chunk-cache): retaining up to four freed chunks reducesrecursion_depth600time on Pablo's main by about 68%, with unchanged instructions. Avoids repeated mappings but retains memory.idea(skip-discarded-none): skipRESUME_CHECKandPOP_TOPwhen 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.
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), andidea(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
gen_traversesegfault and reconcile the generator-size expectation with its allocation size.idea(return-resume-pair)has no demonstrated benefit; drop it unless otherwise justified.The debug build and
verify.sh quickpass; 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
aed4ad8c43948370ac99b4b43d645655e67cbf691a410e82de9b119fabb6188a9dInstructions 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 perrun(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.Separate
straceruns 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 andTools/dualstack/verify.sh quickpassed, 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
windowmode, 461 test files passed; five failed:test_sys(generator size 192 vs 184), spawn/forkserver multiprocessing process tests (subprocess segfault ingen_traverseduring final GC), and undiagnosedtest_kqueue/test_socketfailures.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.
exp1/experiment1exp2/experiment2exp3/experiment3exp4/experiment4exp5/experiment5exp5b/experiment5bexp6/experiment6exp6b/experiment6bexp7/experiment7exp8/experiment8exp9/experiment9exp10/experiment10exp11/experiment11exp12/experiment12exp12b/experiment12bexp13/experiment13exp14/experiment14exp15/experiment15exp16/experiment16exp17/experiment17exp18/experiment18exp19/experiment19