Skip to content

evm: Place the EVM stacks and memories in segments of the VM context - #1878

Open
chfast wants to merge 1 commit into
evm/vm-context-enterfrom
evm/memory-segments
Open

chfast wants to merge 1 commit into
evm/vm-context-enterfrom
evm/memory-segments

Conversation

@chfast

@chfast chfast commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

The VM context gives each execution a frame: its EVM stack space and then a reservation of the largest EVM memory its gas can pay for, at most 64 MiB. The frame starts where the memory of the execution it is nested in ends. The first segment, 8 MiB allocated with the context, holds the frames of executions with real gas limits. A frame that doesn't fit starts the next segment, twice its size so that the nested frames fit too. The context keeps the segments for reuse. The EVM memory never moves, so its growth needs no reallocation. A growth beyond 64 MiB, which costs over 8e9 gas, aborts the process. Previously, each call depth had its own 32 KiB stack, zeroed on creation, and its own memory, reallocated as it grew and kept at its largest size.

In the execution-specs tests with deep calls, this lowers the peak memory up to 2 times and the memory system calls up to 18 times.

@chfast

chfast commented Dec 2, 2026

Copy link
Copy Markdown
Member Author

Measurements against main f46d16d on a Ryzen 9 5950X, Release builds.

Peak RSS and memory system calls (evmone test, execution-specs tests v21.0.3, GCC 16; the calls are mmap + munmap + brk + mremap, counted with strace):

Workload Peak RSS main → PR Memory syscalls main → PR
static_call1_mb1024_calldepth (Cancun) 561 → 544 MiB 1271 → 37
stCallCreateCallCodeTest (Cancun) 45 → 15 MiB 316 → 57
stCallCodes (Cancun) 11 → 8 MiB 69 → 52
state_tests/for_osaka 88 → 75 MiB 650 → 510
state_tests/for_cancun 628 → 614 MiB 1514 → 576
blockchain_tests/for_osaka 231 → 231 MiB 648 → 643

evmone-bench: geomean change of the best of 3 alternating runs on a pinned core; instructions and cycles from perf counters. The default gas limit is INT64_MAX, so every benchmark takes the full 8 GiB reservation in an extra segment.

Group GCC 16 instructions GCC 16 cycles Clang 23 instructions Clang 23 cycles
baseline/execute/main −0.1% +0.5..0.9% −0.1% −0.2%
advanced/execute/main −0.1% +2% −0.1% +0.9%
baseline/execute/micro/memory_grow_* (by16, by32) −1.5% +5% (by32) −2..−3.5% −1..+2%
  • The setup of the frame in enter() costs +0.2..+0.4% instructions in the short swap_math calls.
  • GCC snailtracer is +5% cycles with −0.4% instructions and the same cache and TLB misses, but +11% branch mispredictions; with Clang it is −0.6% cycles. Starting the memory 2 KiB after the stack (against 4K aliasing) doesn't change it, so I attribute it to code layout, as in Code alignment of the Baseline dispatch and the JUMPDEST analysis loop #1828.
  • With the bench gas limit set to 2^24 (the first segment, max_memory_size() computed): instructions ±0.1% on average, swap_math +0.2..+1%.

Checks beyond CI: valgrind on all unit tests (0 errors); ASan + UBSan on all execution-specs tests (the stack overflows of deep-recursion tests in a local Debug libstdc++ build, as on main, pass with a 1 GiB stack); a build with the 64 MiB reservation cap forced on 64-bit Linux and assertions enabled passes ctest and the execution-specs tests; evmone-fuzzer, about 3.6M inputs with no mismatches.

@codecov

codecov Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.23%. Comparing base (97c7d10) to head (01fbe7f).

Additional details and impacted files
@@                  Coverage Diff                  @@
##           evm/vm-context-enter    #1878   +/-   ##
=====================================================
  Coverage                 98.23%   98.23%           
=====================================================
  Files                       191      192    +1     
  Lines                     17423    17520   +97     
  Branches                   3977     3994   +17     
=====================================================
+ Hits                      17115    17211   +96     
- Misses                      237      238    +1     
  Partials                     71       71           
Flag Coverage Δ
eest-develop-gmp 27.22% <52.34%> (+0.08%) ⬆️
eest-legacy 17.19% <50.78%> (+0.12%) ⬆️
eest-libsecp256k1 29.03% <52.34%> (+0.07%) ⬆️
eest-stable 83.84% <100.00%> (-0.01%) ⬇️
evmone-unittests 94.11% <98.43%> (+0.01%) ⬆️
riscv64 94.97% <98.43%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
core 96.64% <100.00%> (+<0.01%) ⬆️
tooling 94.80% <ø> (ø)
tests 99.80% <100.00%> (+<0.01%) ⬆️
Files with missing lines Coverage Δ
lib/evmone/advanced_execution.cpp 100.00% <100.00%> (ø)
lib/evmone/baseline_execution.cpp 97.61% <100.00%> (+0.01%) ⬆️
lib/evmone/execution_state.hpp 98.24% <100.00%> (-1.76%) ⬇️
lib/evmone/instructions.hpp 100.00% <100.00%> (ø)
lib/evmone/vm_context.cpp 100.00% <100.00%> (ø)
lib/evmone/vm_context.hpp 100.00% <100.00%> (ø)
test/unittests/evm_calls_test.cpp 99.83% <100.00%> (+<0.01%) ⬆️
test/unittests/evmone_test.cpp 100.00% <100.00%> (ø)
test/unittests/execution_state_test.cpp 100.00% <100.00%> (ø)
test/unittests/vm_context_test.cpp 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@codspeed

codspeed Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will regress 27 benchmarks

1 warning

⚠️ 39 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚡ 18 improved benchmarks
❌ 27 regressed benchmarks
✅ 1001 untouched benchmarks
⏩ 770 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ test_mod_arithmetic[fork_Amsterdam-blockchain_test-opcode_ADDMOD-mod_bits_255-benchmark-gas-value_1M] 14.8 ms 17 ms -12.94%
❌ test_mod_arithmetic[fork_Amsterdam-blockchain_test-opcode_ADDMOD-mod_bits_63-benchmark-gas-value_1M] 14.9 ms 17.1 ms -12.8%
❌ test_mod_arithmetic[fork_Amsterdam-blockchain_test-opcode_ADDMOD-mod_bits_191-benchmark-gas-value_1M] 19.7 ms 21.9 ms -10.03%
❌ test_mod_arithmetic[fork_Amsterdam-blockchain_test-opcode_ADDMOD-mod_bits_127-benchmark-gas-value_1M] 20.6 ms 22.8 ms -9.63%
❌ test_nested_calls[fork_Amsterdam-blockchain_test-opcode_STATICCALL-benchmark-gas-value_1M] 23.7 ms 24.9 ms -4.76%
❌ test_nested_calls[fork_Amsterdam-blockchain_test-opcode_DELEGATECALL-benchmark-gas-value_1M] 22.7 ms 23.8 ms -4.64%
❌ baseline/analyse/main/blake2b_shifts 26.5 µs 27.7 µs -4.62%
❌ test_nested_calls[fork_Amsterdam-blockchain_test-opcode_CALLCODE-benchmark-gas-value_1M] 22.6 ms 23.6 ms -4.48%
❌ test_delegatecall_staticcall[fork_Amsterdam-blockchain_test-warm_access_True-opcode_DELEGATECALL-benchmark-gas-value_1M] 14.7 ms 15.1 ms -3.15%
❌ test_delegatecall_staticcall[fork_Amsterdam-blockchain_test-warm_access_True-opcode_STATICCALL-benchmark-gas-value_1M] 15.8 ms 16.3 ms -2.92%
❌ test_push_truncated_data[fork_Amsterdam-blockchain_test-PUSH1 with no data-benchmark-gas-value_1M] 16 ms 16.5 ms -2.92%
❌ test_push_truncated_data[fork_Amsterdam-blockchain_test-PUSH32 with no data-benchmark-gas-value_1M] 16.2 ms 16.6 ms -2.91%
❌ test_push_truncated_data[fork_Amsterdam-blockchain_test-PUSH2 with no data-benchmark-gas-value_1M] 16 ms 16.5 ms -2.9%
❌ test_push_truncated_data[fork_Amsterdam-blockchain_test-PUSH2 with half its data-benchmark-gas-value_1M] 16.3 ms 16.7 ms -2.87%
❌ test_push_truncated_data[fork_Amsterdam-blockchain_test-PUSH32 with half its data-benchmark-gas-value_1M] 16.3 ms 16.8 ms -2.87%
❌ test_push_truncated_data[fork_Amsterdam-blockchain_test-PUSH32 one byte short-benchmark-gas-value_1M] 16.3 ms 16.8 ms -2.86%
❌ test_sibling_frame_memory[fork_Amsterdam-blockchain_test-mem_size_0-benchmark-gas-value_1M] 16 ms 16.5 ms -2.83%
❌ test_calldatasize[fork_Amsterdam-blockchain_test-zero_data_True-calldata_size_1024-benchmark-gas-value_1M] 14.3 ms 14.6 ms -2.07%
❌ test_calldatasize[fork_Amsterdam-blockchain_test-zero_data_False-calldata_size_1024-benchmark-gas-value_1M] 14.2 ms 14.5 ms -2.02%
❌ baseline/analyse/main/swap_math 13.9 µs 14.2 µs -1.67%
... ... ... ... ...

ℹ️ Only the first 20 benchmarks are displayed. Go to the app to view all benchmarks.

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing evm/memory-segments (01fbe7f) with evm/vm-context-enter (97c7d10)

Open in CodSpeed

Footnotes

  1. 770 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

The VM context gives each execution a frame: its EVM stack space and
then a reservation of the largest EVM memory its gas can pay for, at
most 64 MiB. The frame starts where the memory of the execution it is
nested in ends. The first segment, 8 MiB allocated with the context,
holds the frames of executions with real gas limits. A frame that
doesn't fit starts the next segment, twice its size so that the nested
frames fit too. The context keeps the segments for reuse. The EVM memory
never moves, so its growth needs no reallocation. A growth beyond
64 MiB, which costs over 8e9 gas, aborts the process. Previously, each
call depth had its own 32 KiB stack, zeroed on creation, and its own
memory, reallocated as it grew and kept at its largest size.

In the execution-specs tests with deep calls, this lowers the
peak memory up to 2 times and the memory system calls up to 18 times.
@chfast
chfast force-pushed the evm/vm-context-enter branch from 1f97aff to 97c7d10 Compare December 3, 2026 18:19
@chfast
chfast force-pushed the evm/memory-segments branch from 1b906d1 to 01fbe7f Compare December 3, 2026 18:19

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant