go-ethereum/core/vm
Jonny Rhea 6e62cc5aa8
core/vm: compute stack operations in place (#35156)
The stack primitives pop by value: pop() returns the 32-byte value
itself, so every popped operand is copied out of the stack arena before
it is used. The result side was already in place, peek returns a pointer
and binary ops write into the new stack top. This PR fixes the operand
side: pointer-returning primitives (popPtr, popPtrPeek, etc), with the
handlers rewritten to read operands directly from their arena slots.
Every popped operand paid the copy, whatever the op went on to do with
it, so this optimization covers the arithmetic and comparison ops as
much as JUMP, MSTORE, SSTORE and RETURN.

The copy is visible in the assembly. On arm64, master's opLt spends four
instructions moving the popped value through the frame, and the
comparison then reads it back from there:

LDP (R5), (R6, R7) ; load words 0 and 1 of the popped value from the
arena
    LDP  16(R5), (R5, R8)            ; load words 2 and 3
STP (R6, R7), vm.~r0-64(SP) ; store words 0 and 1 into a frame slot
    STP  (R5, R8), vm.~r0-48(SP)     ; store words 2 and 3

With popPtrPeek those four instructions are gone, the frame shrinks from
locals=0x58 to locals=0x18, and the function from 336 to 288 bytes. The
compiler cannot remove the copy itself: uint256.Int is a four-element
array, and Go's SSA does not promote arrays longer than one element to
registers, so a by-value pop pays this round trip no matter how far
inlining gets, for LT exactly as for ADD.

The CALL and CREATE families are deliberately not converted: a child
frame reuses the same stack arena, so parent pointers into popped slots
die when the child pushes. The rule is recorded on the primitives:
pointers stay valid until the next push or any sub call. Converting the
call family safely means materializing scalars before the child call,
left for later work with a call-heavy benchmark to justify it.

### Benchmarks

Measured with the benchmark suite from #35144 (the evm-bench contract
workloads and the block import benchmark), which is not part of this
PR's diff. Apple M4 Max, fixed iteration counts, n=10, all p=0.000. B/op
and allocs/op are statistically identical on every benchmark:

| benchmark | master | PR | vs master |
|---|---|---|---|
| Snailtracer | 60.0 ms | 54.1 ms | -9.8% |
| TenThousandHashes | 13.2 ms | 12.2 ms | -7.8% |
| ERC20Transfer | 11.7 ms | 11.0 ms | -5.5% |
| ERC20Mint | 7.49 ms | 7.02 ms | -6.2% |
| ERC20ApprovalTransfer | 8.92 ms | 8.44 ms | -5.4% |

This PR is independent of #35144 but plays nicely with it: the generated
dispatch there splices these handler bodies, so the in-place forms land
in its fast path too, where they measure larger.

### Testing

The rewritten handlers run on the interpreter's only execution path, so
correctness rests on references outside the change:

- **Consensus fixtures.** The full tests package passes: state tests,
the execution-spec families, blockchain tests.
- **Opcode testcases.** The JSON testcases compare individual opcode
results against committed expected values.
- **Tracer fixtures.** The tracetest reference files pin exact log and
return data shapes, covering the rewritten LOG and RETURN paths.
- **Cross-build differential.** A goevmlab campaign running this
branch's evm against master's evm over generated state tests across four
forks (Prague, Cancun, London, Osaka) with full trace comparison:
160,566 tests, zero divergences.

---------

Co-authored-by: MariusVanDerWijden <m.vanderwijden@live.de>
2026-06-16 07:47:05 -05:00
..
program all: fix problematic function name in comment (#32513) 2025-08-29 08:54:23 +08:00
runtime core: implement EIP-8037, state creation gas cost increase (#33601) 2026-06-12 14:29:03 +02:00
testdata core/vm: switch modexp gas computation to uint64 (#32527) 2025-09-08 13:33:10 +02:00
analysis_legacy.go core/vm: add configurable jumpdest analysis cache (#32143) 2025-08-01 13:57:38 +02:00
analysis_legacy_test.go core/vm: using testing.B.Loop (#32660) 2025-09-19 17:06:55 -06:00
common.go core: implement EIP-8037, state creation gas cost increase (#33601) 2026-06-12 14:29:03 +02:00
contract.go core: implement EIP-8037, state creation gas cost increase (#33601) 2026-06-12 14:29:03 +02:00
contracts.go core: implement EIP-8037, state creation gas cost increase (#33601) 2026-06-12 14:29:03 +02:00
contracts_fuzz_test.go core: implement EIP-8037, state creation gas cost increase (#33601) 2026-06-12 14:29:03 +02:00
contracts_test.go core: implement EIP-8037, state creation gas cost increase (#33601) 2026-06-12 14:29:03 +02:00
doc.go core/vm: remove JIT VM codes (#16362) 2018-03-26 13:48:04 +03:00
eip7610.go cmd/geth, core/state, tests: rework EIP7610 check (#34718) 2026-04-14 15:54:36 +02:00
eip7610_test.go cmd/geth, core/state, tests: rework EIP7610 check (#34718) 2026-04-14 15:54:36 +02:00
eips.go core/vm: compute stack operations in place (#35156) 2026-06-16 07:47:05 -05:00
errors.go core/vm: include operand in error message (#34635) 2026-04-13 14:13:33 +02:00
evm.go core: implement EIP-8037, state creation gas cost increase (#33601) 2026-06-12 14:29:03 +02:00
gas.go core/vm: rework gas measurement for call variants (#33648) 2026-03-19 10:02:49 -06:00
gas_table.go core: implement EIP-8037, state creation gas cost increase (#33601) 2026-06-12 14:29:03 +02:00
gas_table_test.go core: implement EIP-8037, state creation gas cost increase (#33601) 2026-06-12 14:29:03 +02:00
gascosts.go core: implement EIP-8037, state creation gas cost increase (#33601) 2026-06-12 14:29:03 +02:00
instructions.go core/vm: compute stack operations in place (#35156) 2026-06-16 07:47:05 -05:00
instructions_test.go core/vm: fix test error message (#35134) 2026-06-12 19:38:45 +02:00
interface.go core/state: track the block-level accessList (#34803) 2026-05-13 20:38:47 +08:00
interpreter.go core: implement EIP-8037, state creation gas cost increase (#33601) 2026-06-12 14:29:03 +02:00
interpreter_test.go core: implement EIP-8037, state creation gas cost increase (#33601) 2026-06-12 14:29:03 +02:00
jump_table.go core: implement EIP-8037, state creation gas cost increase (#33601) 2026-06-12 14:29:03 +02:00
jump_table_export.go cmd, core, trie, triedb: split CachingDB into merkle + binary dbs. (#34700) 2026-04-17 08:55:54 +08:00
jump_table_test.go all: update license comments and AUTHORS (#31133) 2025-02-05 23:01:17 +01:00
jumpdests.go core/vm: add configurable jumpdest analysis cache (#32143) 2025-08-01 13:57:38 +02:00
memory.go core/vm: improve memory resize (#33056) 2025-11-26 16:50:16 +01:00
memory_table.go core/vm: implement stack arena (#33960) 2026-04-28 11:10:44 +02:00
memory_test.go core/vm: improve memory resize (#33056) 2025-11-26 16:50:16 +01:00
opcodes.go core/vm: implement eip-7843: SLOTNUM (#33589) 2026-02-26 13:53:46 +01:00
operations_acl.go core: implement EIP-8037, state creation gas cost increase (#33601) 2026-06-12 14:29:03 +02:00
operations_verkle.go core/vm: implement stack arena (#33960) 2026-04-28 11:10:44 +02:00
stack.go core/vm: compute stack operations in place (#35156) 2026-06-16 07:47:05 -05:00
stack_table.go core/vm: 64 bit memory and gas calculations (#19210) 2019-03-12 11:40:05 +02:00