# pcengine-rust-fast The speed fork of pcengine-rust, the Rust PC Engine / TurboGrafx-16 board (itself transcribed from, and proven equivalent to, [pcengine-python](https://github.com/alewman/pcengine-python) `v0.1.0`), forked at `49e3d48` and **held in lockstep with it**: every change must leave this board's traces identical to the base board's on every case (and so to the reference's), and must measure faster, or it is not kept. The base board is the readable one; this one is for players, and above all for the wasm build on modest hardware. **The crate is still `pcengine_rust` and its public API is the base board's**: the socket traits, `BoardOptions`, `System`, `Frame`, `save_state`/`load_state`, `save_snapshot`/`load_snapshot`. An add-on crate (`supergrafx-rust`, `pcenginecd-rust`) or a page changes one line: ```toml pcengine_rust = { package = "pcengine-rust-fast", path = "../pcengine-rust-fast" } ``` ## Speed **x4.349 over the fork point natively, x4.04 under wasm**, measured directly (the fork point's build against this one, the same checksums of every picture, every sample and the final snapshot on both). Frames per second, one core, from power-on: | Title (600 frames, the picture gate's input) | fork point | now | | fork point, wasm | now, wasm | | | --- | ---: | ---: | ---: | ---: | ---: | ---: | | Blazing Lazers | 236.6 | 1045.4 | x4.418 | 150.5 | 600.9 | x3.99 | | Bonk's Adventure | 263.0 | 1245.4 | x4.735 | 166.8 | 751.0 | x4.50 | | R-Type | 258.0 | 1193.4 | x4.626 | 153.2 | 634.1 | x4.14 | | After Burner II | 253.9 | 1022.4 | x4.027 | 155.8 | 598.3 | x3.84 | | Street Fighter II' | 257.0 | 1026.3 | x3.993 | 154.3 | 580.3 | x3.76 | | all five (geometric mean) | | | **x4.349** | | | **x4.04** | Native: `pce-bench` by CPU time on one efficiency core of a shared, loaded machine (`scripts/ab.py`, the best of 12 runs a side); on a performance core of the same machine the five titles run 1166 to 1912 frames a second (350 to 397 at the fork point, x3.80), which is 19 to 32 times real time. Wasm: `wasm-bench/` under Node 22 on the same efficiency core (`scripts/wasm-bench.sh`, fat LTO as the pages build, no `wasm-opt`): 9.7 to 12.6 times real time, where the fork point has 2.5 to 2.8. With a device on the expansion port (the CD unit's place; here `--device lockstep-port`, the gate's test device) the board must call the device every cycle, and the chain is x1.713 (214.1 to 344.3 frames a second on Blazing Lazers). **supergrafx-rust on this crate** (a scratch copy of it at `1d0528a` with the dependency line changed and nothing else): it builds, its 52 tests pass, its own 22 lockstep cases are identical from its reference cache, and its bench goes from about 150 to about 625 frames a second on 1941 Counter Attack and from 160 to 700 on Aldynes (wall clock, the same efficiency core, its own profile without LTO). | Kept, in order | A/B against the build before it (both with fat LTO, one codegen unit) | | --- | ---: | | **The event horizon.** The board keeps the earliest master clock at which `run_to` has anything to do (the next line, the end of the row, a VDC event, the timer's next tick); a CPU cycle that ends before it only moves the clock, and a work RAM access then skips `finish_access`. The base board runs `run_to` and `deliver_lines` after every cycle | x1.719 | | **The VDC's next event by hand**, not three options folded through an iterator (it is asked after every card access) | x1.176 | | **This crate's own cards finish like work RAM**: `RomCard`, `Sf2Card`, `PopulousCard` and `TennokoeCard` use nothing of their pins, so no stretch, hold, line or horizon can follow an access to one | x1.479 | | **A captured row with no palette write inside it** is looked up in one pass, with no dot times | x1.052 | | **A tile line's eight pixels from a bitplane table** (four lookups ORed), drawn straight into the row | x1.038 | | **The line path without copies**: with the default pixel path the capture reads the VDC's row where it lies; the row, the sprite list and the event list reuse their buffers | x1.031 | | **With a port device, only its `catch_up` before the horizon** (it is promised one every cycle), not the whole of `run_to` | x1.183 with a port device | | **The bus callbacks' quiet case keeps no register across a call**; the taps, the events and the full access are out of line | x1.132 | | **A `RomCard`'s image on loan to the board while it runs**: the ROM read inline, not through the card's trait with a `BoardPins` built for it | x1.066 | | **Work RAM and plain-card accesses stay quiet beside a port device**, unless its `catch_up` left a hold | x1.231 with a port device | | **Fat LTO, one codegen unit** for the crate's own binaries: the CPU core and the 65xx frame under it are other crates | x1.078 | The last row is the final tree built without LTO against itself built with it; the fork point gains only x1.029 from the same two lines (its time was in the scheduler, inside this crate). A/A (the last build against itself, 1,500 frames): x0.987. Tried and not kept, with their numbers: [docs/not-kept.md](docs/not-kept.md) (two attempts at the sprites, a colour table by pixel value for the capture, thin LTO, one codegen unit alone). | `horizon-waits-for-the-underflow` | `board.rs` | the event horizon stops for the timer's underflow only, so the count is behind the clock between | `cargo test` (`every_device_is_caught_up_after_every_instruction`) | | `horizon-sleeps-through-a-dma` | `board.rs` | the event horizon does not stop for a running VRAM DMA: its words move late | `cargo test` (`a_port_device_that_reaches_a_vdc_from_catch_up_moves_the_horizon`) | | `access-keeps-the-old-horizon` | `board.rs` | what an access schedules (a VRAM commit, the timer's start) is not in the event horizon | `cargo test` (`every_device_is_caught_up_after_every_instruction`) | | `next-event-forgets-the-commit` | `vdc/chip.rs` | a VDC's next event leaves out a waiting VRAM access's slot | `cargo test` (`every_device_is_caught_up_after_every_instruction`) | | `any-card-is-plain` | `board.rs` | a card that is not this crate's own (it may hold the CPU, raise IRQ2) has its accesses cut short too | rung 5 (`sock-card-bonk`) | | `quiet-access-with-a-hold-waiting` | `board.rs` | a work RAM or card access is cut short with a hold from outside waiting | `cargo test` (`a_hold_from_a_port_devices_catch_up_stretches_that_ram_access`) | | `palette-write-waits-for-the-row-end` | `video.rs` | the capture's quick row is taken with palette writes inside the row: they show from the next row | rung 2 (`covell`) | | `tile-line-mirrored` | `vdc/background.rs` | the bitplane table puts a tile line's pixels in the other order | rung 2 (`covell`) | | `background-fine-scroll-dropped` | `vdc/background.rs` | the first tile of a background row is drawn from its left edge whatever BXR's low bits | rung 2 (`base-probe`) | | `any-path-is-direct` | `board.rs` | the capture reads the first VDC's row whatever pixel path is plugged in | rung 5 (`sock-path-rtype`) | | `row-buffer-not-cleared` | `vdc/chip.rs` | a line's row starts as the last line's pixels, not as overscan | rung 2 (`covell`) | | `sprite-list-not-emptied` | `vdc/sprites.rs` | a line's sprite scan adds to the last line's entries | rung 2 (`base-probe`) | | `line-events-not-emptied` | `vdc/sequencer.rs` | a line's events are added to the last line's stale ones, which Mednafen's HPhase overwrites | rung 4 (`lib-Great Fumo Binning, The (World) (Aftermarket) (Unl)`) | | `port-catch-up-keeps-the-old-horizon` | `board.rs` | what a port device's `catch_up` does to a VDC is not in the event horizon | `cargo test` (`a_port_device_that_reaches_a_vdc_from_catch_up_moves_the_horizon`) | | `quiet-idle-cycle-not-tapped` | `board.rs` | an idle cycle before the event horizon is left out of the bus log | rung 2 (`covell`) | | `full-read-skips-the-events` | `board.rs` | a read cycle that reaches the event horizon does not run the events | rung 2 (`base-probe`) | | `rom-lent-beside-a-port-device` | `board.rs` | a RomCard's image is taken by the board although a port device could look at the card | `cargo test` (`a_rom_card_has_its_image_whenever_a_port_device_looks`) | | `rom-not-given-back` | `board.rs` | a RomCard's image stays with the board when the host reaches the card | `cargo test` (`a_rom_card_has_its_image_whenever_the_host_looks`) | | `lent-rom-bank-offsets` | `cards.rs` | the board reads a lent image's banks from the wrong place | rung 2 (`covell`) | | `hold-from-catch-up-waits` | `board.rs` | a hold a port device leaves from `catch_up` is not applied by a work RAM or plain card access | `cargo test` (`a_hold_from_a_port_devices_catch_up_stretches_that_ram_access`) | {'tests': 9, 'rung 5': 2, 'rung 2': 8, 'rung 4': 1} 68