# Benchmarks and native JIT

Guillotine ships **three benchmark suites** directly inside `bench/`:

1. **100-block Ethereum mainnet replay (`bench/blocks/`)**: offline,
   pre-state-cached replay of **100 contiguous real Ethereum mainnet blocks**
   (`26,498,000`–`26,498,099`, Osaka / BPO2, `29,560` transactions, `2.973B`
   gas, `5,030` unique contracts), verified **100.00% (`29,560 / 29,560`, 0 gas
   diff)** against mainnet receipts and compared head-to-head against **`revm`**.
2. **Cross-EVM apples-to-apples suite (`bench/compare/`)**: in-process,
   differentially verified comparison against **`alloy-rs/evm2`**,
   **`bluealloy/revm`**, **`ipsilon/evmone`** (Advanced & Baseline),
   **`lambdaclass/ethrex`** (LEVM), **`Giulio2002/gevm`**,
   **`ethereum/go-ethereum`** (`geth`), and Tier 2 native JIT compilers
   (**`evm2-jit`** LLVM 23, **`revmc`** LLVM 22, **`guillotine-jit`**).
3. **In-repo ReleaseFast runner (`zig build bench`)**: self-contained micro- and
   contract-benchmark suite for tracking opcode, precompile, and contract
   regressions without external checkouts.

The repository also contains a zero-dependency ARM64 JIT library under
`src/jit/`.

***

## 100-block Ethereum mainnet replay (`bench/blocks/`)

Single-transaction contract microbenchmarks can hide real-world node bottlenecks:
per-transaction setup, account/storage state lookups across tens of thousands of
touched slots, EIP-4788/2935/7002/7251 system calls, EIP-7702 authorizations,
precompiles, and multi-contract bytecode analysis. `bench/blocks/` caches the
exact pre-state needed once from an Ethereum archive RPC and replays **100
contiguous real mainnet blocks completely offline**.

### Pinned dataset & methodology

* **Pinned range (`bench/blocks/manifest.json`):** Ethereum Mainnet blocks
  `26,498,000`–`26,498,099` (Osaka / BPO2, `29,560` transactions,
  `2,973,053,521` total gas, `23,932` true pre-start accounts filtered from
  `25,924` touched accounts, `64,755` pre-start storage slots, `5,030` unique
  contract bytecodes, and `256` ancestor block hashes).
* **Offline pre-state cache:** `bench/blocks/fetch.mjs` fetches blocks, receipts,
  and `debug_traceBlockByNumber` (`prestateTracer`) once, plus exact
  `start - 1` state for coinbases, withdrawal recipients, EIP-4788/2935/7002/7251
  system contracts (including queue metadata slots), EIP-7702 delegation targets,
  and accounts whose code appears mid-window, writing a single offline JSON
  bundle (`sha256: 0d5f37acd48cef25...`).
* **Full block execution lifecycle:** Before timing begins, an untimed discovery
  pass isolates the exact pre-block-`26,498,000` state so timed runs load the
  entire pre-state up front with zero lazy bundle reads. Each timed iteration
  starts from a fresh in-memory database and executes every block end-to-end:
  EIP-4788 beacon-root & EIP-2935 history pre-block system calls, all legacy /
  EIP-2930 / EIP-1559 / EIP-4844 / EIP-7702 transactions, EIP-7002 withdrawal &
  EIP-7251 consolidation post-block system calls, and EIP-4895 withdrawals.
* **0-tolerance receipt verification:** Every transaction on both engines must
  match mainnet's `status` and exact `gasUsed`. Both **Guillotine** and **`revm`**
  achieve **`29,560 / 29,560` (`100.00%`)** exact status + gas parity (`0` gas
  difference across `2,973,053,521` total gas).

### Head-to-head results (Guillotine vs `revm`)

Measured single-threaded on **Apple M3 Max** (16 cores, 64 GB RAM, macOS arm64),
**Zig `0.17.1`** (`ReleaseFast`, 8,192-entry / 512 MiB `DispatchCache`,
`preserve_none` + fusions) vs **`revm`** (`61eb0f231227`, Rust `opt-level=3`,
`lto=thin`, `codegen-units=1`, `panic=abort`, `-C target-cpu=native`), 1 cold
warmup + 5 timed warm iterations:

| Engine | Mode | Total (100 blocks) | Per block | Throughput | Tx rate | vs `revm` | Receipt parity |
| :--- | :--- | ---: | ---: | ---: | ---: | ---: | ---: |
| **`revm`** | Warm bytecode cache (median, 5 runs) | **1.69 s** | **16.89 ms** | **1,760.8 MGas/s** | **17,506 tx/s** | 1.00× | `29,560 / 29,560` (`100.00%`) |
| **`revm`** | Cold bytecode cache | **1.69 s** | **16.90 ms** | **1,759.4 MGas/s** | **17,493 tx/s** | 1.00× | `29,560 / 29,560` (`100.00%`) |
| **Guillotine** | Warm bytecode cache (median, 5 runs) | `3.38 s` | `33.83 ms` | `878.8 MGas/s` | `8,737 tx/s` | `0.50×` | `29,560 / 29,560` (`100.00%`) |
| **Guillotine** | Cold bytecode cache | `6.47 s` | `64.65 ms` | `459.9 MGas/s` | `4,572 tx/s` | `0.26×` | `29,560 / 29,560` (`100.00%`) |

### What the 100-block replay reveals

Unlike compute-heavy single-tx benchmarks where Guillotine beats `revm` by
`1.47×–3.37×`, full mainnet block replay exposes where real-world execution time
actually goes today:

* **Cold dispatch-schedule construction (`+3.08 s`):** Analyzing `4,906`
  executed contracts into `421.8 MiB` of fused dispatch schedules takes `3.08 s`
  on the first pass (`6.47 s` cold → `3.38 s` warm), whereas `revm`'s linear
  jumpdest bitmap scan takes only a few milliseconds total.
* **Warm steady-state gap (`2.00×` vs `revm`):** With the dispatch cache warm
  (`1,280,084` hits, `0` evictions), Guillotine runs `100` real mainnet blocks in
  `3.38 s` (`878.8 MGas/s`, `8,737 tx/s`) vs `revm`'s `1.69 s` (`1,760.8 MGas/s`,
  `17,506 tx/s`). The remaining gap is driven by non-interpreter costs across
  `29,560` short transactions: pure-Zig `ECRECOVER`, in-memory `Database` /
  journal / `AccessList` lookups, and per-tx / nested-call frame setup.

### Reproducing the 100-block mainnet replay

```bash
# 1. Fetch & cache the pinned 100-block pre-state bundle once (requires archive RPC)
ETHEREUM_RPC_URL=https://... node bench/blocks/fetch.mjs --start 26498000 --count 100

# 2. Run Guillotine offline in ReleaseFast (1 cold warmup + 5 warm runs)
zig build bench-blocks -- --iterations 5 --warmup 1

# 3. Build & run the matching revm comparison runner on the exact same cached bundle
RUSTFLAGS="-C target-cpu=native" cargo build --release --locked --manifest-path bench/blocks/runners/revm/Cargo.toml
./bench/blocks/runners/revm/target/release/blocks-revm --iterations 5 --warmup 1
```

See `bench/blocks/README.md` and `bench/blocks/manifest.json` for the pinned
block hashes, pre-state counts, and bundle SHA-256.

***

## Cross-EVM apples-to-apples benchmarks (`bench/compare/`)

Legacy cross-EVM benchmarks often suffer from CLI process-spawn overhead (e.g.
wrapping 1 transaction in `hyperfine`), unverified outputs, mixed warm/cold
caching, or cherry-picked synthetic loops. `bench/compare/` is engineered so
every number is directly auditable and reproducible from this repository.

### Methodology & fairness guarantees

* **Zero CLI startup overhead:** Every engine runs **in-process** inside its own
  compiled native binary (`zig-out/bin/guillotine-compare`,
  `bench/compare/bin/compare-*`) with a monotonic clock wrapping only EVM
  transaction execution.
* **Public, non-cherry-picked fixtures:** 21 of 25 workloads are pulled verbatim
  from [`alloy-rs/evm2`](https://github.com/alloy-rs/evm2) (`e80df9c46a82`,
  classic compute benchmarks, real Mainnet DeFi contracts, and precompiles),
  plus 4 targeted hot-loop workloads (`loop_arithmetic`, `loop_memory`,
  `loop_storage`, `loop_staticcall`).
* **0-tolerance differential verification gate:** Every timed sample on every
  engine must pass `success == true`, **exact `gas_used`** (0-gas tolerance),
  and **byte-for-byte `output`** match against canonical consensus. Any engine
  that diverges is disqualified (`MISMATCH`) rather than timed.
* **Warm vs. Cold separation:**
  * **Warm steady-state:** Bytecode is pre-loaded/analyzed where supported; each
    measured transaction starts from an identical clean pre-state snapshot (no
    state accumulation across iterations).
  * **Cold first-run:** Every sample builds a fresh EVM from raw unanalyzed
    bytecode with empty jumpdest/dispatch caches.
* **Strict JIT tier separation:** Native LLVM/AArch64 compilers are reported in
  their own Tier 2 table showing **both** one-time compile latency and warm
  execution latency, and explicitly flagging interpreter fallbacks.
* **Comparable native optimization flags:** Zig `-OReleaseFast` (native CPU),
  Rust `opt-level=3`, `lto=thin`, `codegen-units=1`, `panic=abort`,
  `-C target-cpu=native`, C++ `evmone` `-O3 -mcpu=native` + `libsecp256k1`, Go
  1.26 default release builds.
* **Geometric means:** Category and overall ratios use geometric means of
  per-workload speedups so a single long contract (`snailtracer`) cannot swamp
  the rest of the suite.

### Warm steady-state interpreters (geometric mean)

Measured on **Apple M3 Max** (16 cores, 64 GB RAM, macOS arm64), **Zig `0.17.1`**
(`preserve_none` tail-call dispatch + fusions), Rust `1.100.0`, Apple Clang
`17.0.0`, Go `1.26.0`, gas accounting on and tracing off. All **25 / 25
workloads** passed 100% differential verification across all **12 engine
configurations** (0 mismatches).

Higher is faster (`> 1.00×` beats the baseline):

| Engine | Lang | Overall vs `revm` | Overall vs `evm2` | Classic (7) vs `revm` | Mainnet DeFi (9) vs `revm` | Precompiles (5) vs `revm` | Hot-Loops (4) vs `revm` |
| :--- | :--- | ---: | ---: | ---: | ---: | ---: | ---: |
| **evm2** | Rust | **1.12×** | 1.00× | 1.10× | 1.01× | 1.33× | 1.15× |
| **Guillotine** | Zig | **1.00×** | 0.90× | **1.43×** | 0.98× | 0.61× | 1.04× |
| **revm** | Rust | **1.00×** | 0.89× | 1.00× | 1.00× | 1.00× | 1.00× |
| **gevm** | Go | **0.99×** | 0.89× | 0.98× | 1.02× | 1.14× | 0.79× |
| **Guillotine (no fusion)** | Zig | **0.89×** | 0.79× | 1.21× | 0.81× | 0.61× | 1.00× |
| **ethrex (LEVM)** | Rust | **0.77×** | 0.69× | 0.84× | 0.67× | 0.89× | 0.76× |
| **evmone (Baseline)** | C++ | **0.76×** | 0.68× | 0.94× | 0.44× | 1.11× | 1.09× |
| **evmone (Advanced)** | C++ | **0.51×** | 0.46× | 0.82× | 0.18× | 1.11× | 0.84× |
| **geth** | Go | **0.31×** | 0.28× | 0.41× | 0.15× | 0.68× | 0.41× |

### Selected warm workload latencies (median per tx)

Lower is faster. Fastest verified interpreter per row is **bold**. Full 25-row
tables live in `bench/compare/RESULTS.md` and `bench/compare/report.html`.

| Workload | Cat | Guillotine | `evm2` | `revm` | `evmone` (Baseline) | `ethrex` | `gevm` | `geth` | Guillotine vs `revm` | Guillotine vs `evm2` |
| :--- | :--- | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: |
| `snailtracer` | classic | **55.53 ms** | 64.75 ms | 81.67 ms | 70.53 ms | 80.80 ms | 92.29 ms | 137.01 ms | 1.47× | 1.17× |
| `burntpix` | mainnet | **35.42 ms** | 53.65 ms | 65.83 ms | 63.54 ms | 68.33 ms | 77.51 ms | 121.02 ms | 1.86× | 1.51× |
| `hash_10k` | classic | **2.91 ms** | 3.86 ms | 4.40 ms | 3.40 ms | 3.73 ms | 3.94 ms | 7.54 ms | 1.51× | 1.33× |
| `fibonacci` | classic | **17.71 µs** | 51.04 µs | 58.71 µs | 55.67 µs | 58.58 µs | 60.38 µs | 89.88 µs | 3.32× | 2.88× |
| `factorial` | classic | **17.79 µs** | 49.29 µs | 60.02 µs | 55.69 µs | 61.42 µs | 63.35 µs | 90.13 µs | 3.37× | 2.77× |
| `uniswap_v3_pool` | mainnet | **11.40 µs** | 16.71 µs | 19.38 µs | 33.46 µs | 21.73 µs | 22.83 µs | 69.63 µs | 1.70× | 1.47× |
| `curve_stableswap` | mainnet | **83.83 µs** | 84.50 µs | 97.33 µs | 108.17 µs | 143.90 µs | 108.50 µs | 288.60 µs | 1.16× | 1.01× |
| `loop_memory` | opcode | **67.38 µs** | 109.04 µs | 120.42 µs | 124.50 µs | 135.58 µs | 156.46 µs | 264.17 µs | 1.79× | 1.62× |
| `erc20_transfer` | classic | 6.51 ms | **5.24 ms** | 5.80 ms | 9.43 ms | 7.27 ms | 6.91 ms | 15.46 ms | 0.89× | 0.81× |
| `seaport` | mainnet | 2.08 µs | 1.75 µs | 1.69 µs | 15.63 µs | 2.38 µs | **1.50 µs** | 24.29 µs | 0.81× | 0.84× |
| `precompile_modexp` | precompile | 1.81 ms | **1.05 ms** | 2.42 ms | 1.57 ms | 1.56 ms | 1.61 ms | 1.76 ms | 1.33× | 0.58× |
| `precompile_ecrecover` | precompile | 177.48 µs | 30.38 µs | 30.54 µs | 30.10 µs | 31.21 µs | 30.17 µs | 36.17 µs | 0.17× | 0.17× |

### Tradeoffs, cold analysis, and Tier 2 JITs

The full report intentionally exposes where Guillotine excels and where it still
trails:

* **Compute & opcode fusion wins:** Guillotine is the **fastest interpreter** in
  Classic compute (`1.43×` vs `revm`, `1.30×` vs `evm2`) and on heavy Mainnet
  compute (`burntpix` `1.86×` vs `revm`, `uniswap_v3_pool` `1.70×` vs `revm`,
  `curve_stableswap` `1.16×` vs `revm`). Turning fusion off drops overall
  geomean from `1.00×` to `0.89×` (`burntpix` slows `2.01×`, `snailtracer`
  slows `1.66×`).
* **Precompile gap (`0.61×` vs `revm`):** Pure-Zig `ECRECOVER` (`177.48 µs` vs
  \~`30 µs` `libsecp256k1` in `evm2`/`revm`/`evmone`/`gevm`) and
  `BN254_PAIRING` (`1.22 ms` vs `628 µs` in `evm2`) are the main drag on
  overall geomean. `MODEXP` (`1.81 ms`) beats `revm` (`2.42 ms`) by `1.33×`.
* **Storage & call loops:** `loop_storage` (`113.73 µs` vs `70.58 µs` `evm2`)
  and `loop_staticcall` (`84.04 µs` vs `56.19 µs` `evm2`) show headroom in
  journal/state lookup and nested frame setup.
* **Cold first-run (`0.27×` vs `revm`):** Building a full dispatch schedule with
  fusion passes takes `300–800 µs` on large uncached Mainnet contracts
  (`seaport` `800.67 µs` cold → `2.08 µs` warm; `uniswap_v3_pool` `792.63 µs`
  cold → `11.40 µs` warm), whereas simple jumpdest-bitmap interpreters analyze
  in `1–25 µs`. On compute-heavy workloads like `snailtracer` (`56.94 ms` cold)
  and `burntpix` (`38.83 ms` cold), Guillotine still finishes #1 even cold.
* **Tier 2 LLVM JITs (`evm2-jit` LLVM 23 & `revmc` LLVM 22):** Paying a
  one-time compile cost (`10 ms–8.5 s` depending on contract size) brings
  `snailtracer` down to `19.31–20.31 ms` and `burntpix` down to `7.60–8.80 ms`.
  In `v0.1.0`, Guillotine's `src/jit/` is not yet wired into `Evm`, so
  `guillotine-jit` transparently executes on the interpreter (`interp fallback`).

### Reproducing the cross-EVM suite

```bash
# 1. Fetch pinned workloads & compile all native runners
./bench/compare/build-all.sh

# 2. Run warm + cold + jit modes sequentially on an idle machine
node bench/compare/run.mjs

# 3. Filter to specific workloads or engines
node bench/compare/run.mjs --filter snailtracer,burntpix --modes warm,jit
node bench/compare/run.mjs --modes warm --engines guillotine,evm2,revm,evmone-baseline
```

Outputs are written to `bench/compare/RESULTS.md` (Markdown report),
`bench/compare/report.html` (self-contained interactive HTML report), and
`bench/compare/results/latest.json` (gitignored full dataset with raw samples
and environment provenance). See `bench/compare/README.md` for pinned revisions
and platform notes (including why `category-labs/monad` requires Linux x86-64-v3).

***

## In-repo ReleaseFast runner (`zig build bench`)

For fast local regression checks without building external Rust/C++/Go engines,
Guillotine ships a self-contained ReleaseFast runner under `bench/`.

### v0.1.0 numbers (x86\_64 Linux)

Measured with the in-repo runner in ReleaseFast on x86\_64 Linux, stock Zig
`0.17.1`, gas accounting on and tracing off:

| Workload | Median | Comparison |
| --- | ---: | --- |
| `contract/snailtracer` | **37.86 ms** (6.21 GGas/s) | 1.25x faster than revm (47.21 ms) |
| `precompile/ECRECOVER` | **0.171 ms** | 182x faster than before the v0.1.0 rewrite |
| `precompile/MODEXP-256` | **2.82 µs** | |
| `precompile/BN254-mul` | **8.16 µs** | |

The x86\_64 revm figure above comes from `revme bench snailtracer` (a separate
harness on the same Linux host); use `bench/compare/` above for strict
apples-to-apples comparisons.

### Running the in-repo benchmarks

Run every command from the repository root with Zig 0.17.1. The runner builds
the EVM and its dependencies in ReleaseFast and refuses timing runs in any other
optimization mode.

```bash
# Full suite from the root build (Osaka, 16,777,216 gas budget)
zig build bench

# Filter and set sample and warmup counts
zig build bench -- --filter opcode/ADD --iterations 1000 --warmup 100

# Write JSON (default bench/results/latest.json)
zig build bench -- --json bench/results/latest.json

# Contract fixtures such as snailtracer need Prague's 1B off-chain budget;
# -Dbench-hardfork is an option of the standalone bench/build.zig
zig build --build-file bench/build.zig bench -Dbench-hardfork=PRAGUE -- --filter contract/

# Harness tests
zig build test-bench
```

`zig build bench-build` installs `zig-out/bin/guillotine-bench`;
`zig build bench-compare-build` installs `zig-out/bin/guillotine-compare`.

:::info[Hardfork and gas budget]
Benchmarks default to Osaka with EIP-7825's 16,777,216 transaction gas cap.
Snailtracer consumes about 235M gas per call, so contract runs use
`-Dbench-hardfork=PRAGUE` (1B off-chain budget). `CLZ` and `P256VERIFY` require
Osaka and are reported as unavailable on Prague.
:::

### What a sample measures

* Each sample times `evm.simulate()`: the ephemeral state overlay, dispatch
  analysis, execution, and owned result copying. Deployment, preflight
  validation, warmups and result cleanup are outside the timed window.
* Every sample must succeed and match the preflight gas and output. Failed or
  unavailable cases never receive timing numbers.
* `--compare` reports the loop-dispatch row as `unavailable`, because the engine
  always uses tail-call dispatch.

`bench/README.md` documents the workloads, the JSON schema and the statistics.

***

## Native ARM64 JIT library

`src/jit/` compiles side-effect-free EVM bytecode directly to AArch64 machine
code. It depends only on Zig `std` and `builtin`: no LLVM and no external
assembler at runtime.

:::warning[Not wired into `Evm`]
In v0.1.0 the JIT is a library with its own tests. `Evm` and `EvmConfig` do not
call it, so contracts always run on the dispatch-schedule interpreter. Run its
tests with `zig build test-jit`; native code executes only on `aarch64` (CI
runs the step on `macos-latest`).
:::

| File | Role |
| --- | --- |
| `src/jit/root.zig` | Public types (`JitContext`, `ExitCode`, `RuntimeHelpers`) and the test root |
| `src/jit/compiler.zig` | EVM-to-AArch64 compiler (`Program`, `Options`, `compile`) |
| `src/jit/arm64_emitter.zig` | AArch64 instruction encoder |
| `src/jit/x86_64_emitter.zig` | x86\_64 instruction encoder; `compile` returns `null` unless `builtin.cpu.arch == .aarch64` |
| `src/jit/jit_mem.zig` | Executable page mapping (`MAP_JIT` with per-thread `pthread_jit_write_protect_np` toggling on macOS) |
| `src/jit/jit_cache.zig` | Bounded LRU cache (`capacity = 64`, `hot_threshold = 2`) |

### Eligibility and fallback

* **Supported:** arithmetic, bitwise, comparison and stack operations, memory
  (`MLOAD`, `MSTORE`, `MSTORE8`, `MSIZE`, `MCOPY`), calldata reads, control flow
  (`JUMP`, `JUMPI`, `JUMPDEST`, `PC`, `GAS`), `KECCAK256`, `STOP`, `RETURN` and
  `REVERT`.
* **Ineligible:** storage, logs, calls and `CREATE`/`CREATE2`. `compile`
  returns `null` for such code.
* **Fallback:** a JIT run has no external side effects. When it returns
  `ExitCode.fallback`, or `compile` returns `null`, the host re-executes the
  whole call in the interpreter with identical results.
* **Safety:** compiled code checks stack bounds against a 1,024-word stack, and
  `JitContext.instruction_budget` defaults to 300,000,000.
