# Optimization log Every change tried on the fast core, kept or not, with its measurement. The harness is `benchmarks/speed.py` (interleaved rounds, one interpreter per round, best of N; `--cores fast@REV,fast` compares against a revision). The machine is a shared i9-13900K; the load average is given because absolute numbers move with it and interleaved ratios do not. ## Kept | Commit | Change | CPython 3.14.4 | PyPy 7.3.20 | | --- | --- | --- | --- | | 5f6a881 | One generated function per opcode per page | 1.68x vs reference | 0.95-1.03x | | f4f3cd0 | `_attention` flag guards the lifecycle checks in `step()` | 1.98x | 1.36x | | (this) | `run(t_states)`: the step loop inside the core, for hosts that can batch | 2.11x zexall, 2.56x `base` (step: 1.93x, 2.30x), 228-T slices | 0.62x at 228-T slices; ~1.3x over `step()` from 100,000-T slices | `run()` on PyPy, zexall, fast core only, one process, 200M T-states (the in-process `step()` figure is depressed by the preceding `run()` rounds' JIT state and is not comparable): slice 228 T: 72 MHz; 5,000: 96 MHz; 100,000: 169 MHz; one call: 160 MHz. The same load in a fresh process through `step()` measured 128.6 MHz. Every call enters and leaves the compiled loop, which costs about as much as a few dozen instructions. ## Tried and removed | Change | Measured against | CPython | PyPy | Why it does not pay | | --- | --- | --- | --- | --- | | Lazy R: `_r7 += 1` per M1, `r` a property joining `_r_high` | f4f3cd0, zexall, 7 rounds CPython / 5 PyPy, load ~42 | 0.97x | 0.97x | The replaced masking is a few int ops on one local; the attribute store remains, and `r` reads became property calls. | | Flag lookup tables: `ADD_F`/`SUB_F` indexed `carry << 16 \| a << 8 \| v`, `CP_F`, `INC_F`/`DEC_F` | 6d4de11; zexall/zexdoc 7 rounds, `base`/`indexed_cb` 7 rounds (CPython), zexall 5 rounds (PyPy), load ~40 | 0.99-1.01x | 0.89x | CPython 3.14's specialized small-int ops are as cheap as the subscript they replace; PyPy's JIT keeps the arithmetic in registers but must load from a 128K-entry tuple. | | `__slots__` for the registers, bus callables and lifecycle state | 6d4de11; zexall and `base`, 7 rounds, CPython, load ~40 | 0.99-1.00x | not run | CPython 3.14 reads and writes inline instance values as fast as slots. | ## Recorded games `benchmarks/record_megadrive.py` records a Mega Drive sound driver from power-on (900 frames, megadrive-python ac4a78d); `benchmarks/speed.py --workload NAME` replays it. Results at 35c8b52, load average ~50: | Game | CPython ref / fast | ratio | PyPy ref / fast (steady state) | ratio | | --- | --- | --- | --- | --- | | Castlevania: Bloodlines | 3.825 / 2.053 s | 1.86x | 0.272 / 0.247 s | 1.10x | | Streets of Rage 2 | 3.750 / 1.862 s | 2.01x | 0.222 / 0.166 s | 1.34x | | Contra: Hard Corps | 4.237 / 2.119 s | 2.00x | 0.227 / 0.253 s | 0.90x | PyPy warm-up, Contra, one process, successive replays (s): reference 1.584, 0.303, 0.303, 0.245, 0.264, 0.233, 0.258, 0.256; fast 2.063, 0.597, 0.366, 0.308, 0.252, 0.274, 0.284, 0.285. With one warm-up replay the harness had measured Contra at 0.60x; speed.py now warms a replay up four times on PyPy. The hottest code in Streets of Rage 2 on CPython (py-spy): the driver's poll loop `LD A,(HL)` / `CP n` / `JR C` is 35% of samples, `step()` 31%, the board's `read_byte` 11%.