mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-06-19 21:31:37 +00:00
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>
|
||
|---|---|---|
| .. | ||
| filtermaps | ||
| forkid | ||
| history | ||
| overlay | ||
| rawdb | ||
| state | ||
| stateless | ||
| tracing | ||
| txpool | ||
| types | ||
| vm | ||
| .gitignore | ||
| bal_test.go | ||
| bench_test.go | ||
| bintrie_witness_test.go | ||
| block_validator.go | ||
| block_validator_test.go | ||
| blockchain.go | ||
| blockchain_insert.go | ||
| blockchain_reader.go | ||
| blockchain_repair_test.go | ||
| blockchain_sethead_test.go | ||
| blockchain_snapshot_test.go | ||
| blockchain_stats.go | ||
| blockchain_test.go | ||
| chain_makers.go | ||
| chain_makers_test.go | ||
| dao_test.go | ||
| error.go | ||
| eth_transfer_logs_test.go | ||
| events.go | ||
| evm.go | ||
| gaspool.go | ||
| gen_genesis.go | ||
| genesis.go | ||
| genesis_alloc.go | ||
| genesis_test.go | ||
| headerchain.go | ||
| headerchain_test.go | ||
| jumpdest.go | ||
| mkalloc.go | ||
| rlp_test.go | ||
| sender_cacher.go | ||
| state_prefetcher.go | ||
| state_processor.go | ||
| state_processor_test.go | ||
| state_transition.go | ||
| state_transition_test.go | ||
| stateless.go | ||
| txindexer.go | ||
| txindexer_test.go | ||
| types.go | ||