# Three-tier adaptive execution

Every EVM implementation today forces a painful tradeoff between **cold-start latency** and **peak execution speed**:

| Architecture | Examples | Cold first-run | Warm steady-state | Real-world limitation |
| --- | --- | --- | --- | --- |
| **Bytecode interpreters** | `revm`, `evm2`, `evmone`, `gevm`, `geth` | Fast (`2–25 µs` JUMPDEST scan) | Capped (~`1–1.8 GGas/s`) | Leaves `2–6×` throughput on the table on compute and hot DeFi loops |
| **LLVM AOT / JIT compilers** | `evm2-jit` (LLVM 23), `revmc` (LLVM 22) | **Slow (`20 ms – 8.5 s` per contract)** | Fast (`3–8×` over interpreters) | `seaport` takes `2.4–8.5 s` to compile — unusable on unseen live contracts without offline pre-compilation |
| **Guillotine 3-tier engine** | Tier 0 → Tier 1 → Tier 2 | **Fast (`8.3 µs` cold `seaport`, `32.5 ms` cold `snailtracer`)** | **#1 (`1.78×` vs `revm`, `1.24 s` 100-block 1T, `4.29 µs` JIT `uniswap_v3_pool`)** | Adapts automatically to cold 1-shot txs, warm DeFi routers, and hot loops |

Real Ethereum mainnet blocks are a mixture of all three regimes: across 100 contiguous Osaka mainnet blocks (`26,498,000`–`26,498,099`, `29,560` transactions), **28.2%** of transactions are plain 21k-gas ETH transfers, **64.1%** use ≤ 50k gas, **4,906 unique contracts** (`43.9 MiB` of bytecode) are touched, and a handful of hot contracts consume the bulk of execution gas.

Guillotine breaks the false tradeoff by promoting each contract automatically across **three execution tiers** — with **opt-in Block-STM parallel block execution** on multi-core hosts:

```text
                       Contract Call (code, code_hash, gas)
                                        │
                ┌───────────────────────┴───────────────────────┐
                │ Hot in JitCache?                              │
                ▼                                               ▼
      [ Tier 2: Native JIT ]                          [ Cold (Call #1) ]
      Direct AArch64 & x86_64 emitter                           │
      • 30–200 µs compile (no LLVM)                             ▼
      • Stateful host trampolines                     [ Tier 0: Baseline ]
      • Register stack window                         • Zero schedule allocation
      • 64-bit narrow-word fast paths                 • Lazy SIMD JUMPDEST bitmap
                ▲                                     • Backward-jump OSR
                │ promote (hits ≥ jit_hot_threshold)            │
                │                                               │ promote (Call #2 / OSR)
      [ Tier 1: Fused Tailcall Dispatch ]  ◄────────────────────┘
      • Single-pass schedule compiler (no intermediate bitmap copy)
      • 8-byte untagged Dispatch.Item + basic-block gas/stack batching
      • Stock Zig 0.17.1 `preserve_none` tailcalls + 4-limb TOS registers
```

## Tier 0 — Cold baseline interpreter

When a contract is seen for the first time and is not yet in `DispatchCache`, eagerly compiling a full fused dispatch schedule wastes hundreds of microseconds on contracts that only run a few dozen opcodes (like a proxy dispatch or an ERC-20 `transfer`).

* **Zero schedule allocation:** Executes raw bytecode directly on Guillotine's pooled stack and memory buffers.
* **Local provable-boundary JUMPDEST validation:** Instead of scanning 24 KiB of bytecode up front, each jump target is validated locally from a provable instruction boundary just below the target, with a bounded prefix scan fallback — zero upfront scan cost on cold calls.
* **Static-gas backward-jump OSR (`cold_tier0_osr_gas = 50_000`):** Counts only static instruction gas (ignoring storage/cold-access/callee gas so ordinary Solidity function returns never trigger spurious promotion). When a cold contract runs a real compute loop on call #1 (`snailtracer`, `burntpix`), hitting the threshold at a backward jump compiles Tier 1 and resumes mid-call at the target `JUMPDEST` with zero double gas charging.

## Measured impact (Apple M3 Max, `100%` differential & EEST parity)

| Benchmark | Before 3-Tier | After 3-Tier | Reference (`revm` / `evm2` / LLVM JITs) |
| :--- | ---: | ---: | :--- |
| **Warm cross-EVM geomean (25 workloads)** | `1.00×` vs `revm` | **`1.78×` vs `revm` / `1.61×` vs `evm2`** | #1 on 21/25 workloads (19/20 non-precompiles) |
| **Cold `seaport` / `fiat_token`** | `800 µs` / `1,070 µs` | **`8.31 µs` / `8.56 µs`** (**96×–125×**) | `revm` `7.83 µs` / `6.71 µs`, `evmone-base` `16.96 µs` / `13.88 µs` |
| **Cold `snailtracer` / `burntpix`** | `56.9 ms` / `38.8 ms` | **`32.53 ms` / `19.15 ms`** (**#1 cold!**) | `evm2` `64.4 ms` / `53.0 ms`, `revm` `81.9 ms` / `68.2 ms` |
| **Tier 2 JIT `uniswap_v3_pool`** | `interp fallback` | **`4.29 µs`** (`848 µs` compile) | **Beats** `evm2-jit` (`5.75 µs` / `3.09 s`) & `revmc` (`5.63 µs` / `1.43 s`) |
| **Tier 2 JIT `snailtracer` / `burntpix`** | `interp fallback` | **`20.43 ms` / `8.39 ms`** (`348 / 886 µs` compile) | Matches/beats LLVM 22–23 while compiling **2,400×–17,000× faster** |
| **100-block mainnet replay (1T)** | `3.93 s` cold / `2.32 s` warm | **`1.81 s` cold / `1.24 s` warm (`2,393 MGas/s`)** | **#1 single-threaded** ahead of `evm2` (`1.33 s`), `revm` (`1.66 s`), `gevm` (`1.84 s`), `ethrex` (`1.97 s`), `geth` (`2.37 s`) |
| **100-block mainnet replay (Block-STM 8T)** | — | **`665 ms` warm (`4,472 MGas/s`, `44.5k tx/s`)** | `2.49×` faster than `revm`, `29,560 / 29,560` receipts + bit-exact post-state |

## Tier 1 — Single-pass fused `preserve_none` tailcall engine

Once a contract is called again (or promoted via OSR), Guillotine compiles it in a **single linear pass** into an 8-byte `Dispatch.Item` schedule cached in `DispatchCache`:

* **Single-pass compilation with backpatched block headers:** Computes static basic-block `(gas, min_stack, max_stack)` on the fly and writes them back when the block ends — no second scan, no intermediate per-byte bitmap allocation, and no full-buffer `toOwnedSlice` copy.
* **Register-passing tailcalls:** Every handler uses stock Zig `0.17.1` `preserve_none` with `@call(.always_tail, ...)`, keeping `frame`, `cursor`, `sp`, `gas`, and the top-of-stack `u256` (`t0..t3`) pinned in registers.
* **Inline superinstructions:** `PUSH` + arithmetic/bitwise/memory ops store 1–4 inline 64-bit limbs directly in the schedule (`PUSH_*_INLINE`, `PUSH_*_LIMBS2..4`, `AND_MASK160`, `PUSH_SHL/SHR_INLINE`, `EQ/LT/GT/ISZERO_JUMPI`, `PUSH_ADD_DUP1`), with zero pointer indirection.

See [Dispatch](/concepts/dispatch) and [Synthetic opcodes](/concepts/synthetic-opcodes) for the full schedule layout.

## Tier 2 — Sub-millisecond stateful native JIT

Instead of pulling in LLVM (which costs seconds per contract to compile), Guillotine ships a **zero-dependency direct machine-code emitter** (`src/jit/`) targeting both **AArch64** and **x86\_64**:

* **Sub-millisecond compile time (`30–200 µs`):** `1,000×–50,000×` faster compilation than LLVM, making JIT viable for live blocks and RPC nodes without offline pre-compilation.
* **Stateful EVM support:** Host trampolines let real DeFi contracts (`SLOAD`/`SSTORE`, `TLOAD`/`TSTORE`, `CALL`/`STATICCALL`/`DELEGATECALL`, `LOG*`, `CREATE*`, context/environment opcodes) execute natively.
* **Intra-block register stack & 64-bit narrow-word fast paths:** Virtual stack tracking eliminates `DUP`/`SWAP`/constant memory traffic, and values known to fit in 64 bits (loop counters, memory offsets, shifts, selectors, gas) lower to single native 64-bit instructions (`adds`, `mul`, `udiv`, `cmp`) with overflow bailouts to full 256-bit arithmetic.
* **Inline memory & calldata fast paths:** Bounds-checked `MLOAD`/`MSTORE`/`CALLDATALOAD` execute inline without C helper calls whenever memory does not need to grow.

## Optional multi-core scaling: Block-STM parallel replay

For full-block execution (`src/block/parallel_executor.zig`, `zig build bench-blocks -- --threads N`), Guillotine adds an opt-in **optimistic parallel executor (Block-STM)**:

* Speculatively executes transactions across a pool of per-core `Evm` instances backed by a multi-version state store (`MVMemory`).
* Pre-recovers EIP-7702 authorizations in parallel and defers non-conflicting coinbase priority-fee credits to serial commit so independent transactions never falsely contend on `coinbase`.
* Validates read-sets deterministically and re-executes only conflicting transactions, guaranteeing **100.00% bit-exact consensus parity** with single-threaded execution.

Single-threaded and multi-threaded numbers are always benchmarked and reported strictly separately.

## Internal engineering specifications

* [Three-Tier Adaptive Execution](https://github.com/evmts/guillotine/blob/main/docs/performance/THREE-TIER-EXECUTION.md)
* [Native Direct JIT Architecture](https://github.com/evmts/guillotine/blob/main/docs/performance/JIT-ARCHITECTURE.md)
* [Precompiles, Crypto, and Hot-Path State](https://github.com/evmts/guillotine/blob/main/docs/performance/PRECOMPILES-AND-STATE.md)
* [Optimistic Parallel Block Execution (Block-STM)](https://github.com/evmts/guillotine/blob/main/docs/performance/PARALLEL-BLOCK-EXECUTION.md)
