Skip to content

evm: Allocate the memory on the first growth - #1795

Open
chfast wants to merge 1 commit into
masterfrom
evm/allocate-memory-on-growth
Open

chfast wants to merge 1 commit into
masterfrom
evm/allocate-memory-on-growth

Conversation

@chfast

@chfast chfast commented Oct 29, 2026

Copy link
Copy Markdown
Member

Memory allocated its first 4 KiB in the constructor, with allocate_capacity(), the same realloc() call that grow_capacity() makes. It now starts with no capacity, and its first growth allocates the memory in grow_capacity(), which already rounds the size up to whole pages: every capacity stays as before, and allocate_capacity() folds into grow_capacity(). Until the first growth the data pointer is null. Every access to the memory in the library is behind a non-zero size, so it comes after a growth.

A state that never grows its memory now allocates none, and the first growth of one that does takes the out-of-line path, with realloc() of a null pointer instead of malloc(). Instructions of the whole life of a Memory, malloc(), realloc() and free() included, counted with callgrind (x86-64, GCC 15 and Clang 21):

Memory GCC before GCC after Clang before Clang after
never grown 301 10 300 10
grown by one word 334 407 330 398
grown by mstore(0x40, 0x80) 378 412 368 401

Baseline keeps one execution state for each call depth for the life of the VM, so it pays either difference once per depth. Advanced creates a state for each execution. The machine code of the growths, the instructions and the interpreter loops is unchanged; only the state creation, in VM::get_execution_state() and Advanced's execute(), loses the malloc() call and its out-of-memory check.

Memory allocated its first 4 KiB in the constructor, with
allocate_capacity(), the same realloc() call that grow_capacity() makes.
Start with no capacity instead: the first growth allocates the memory in
grow_capacity(), which already rounds the size up to whole pages, so
every capacity stays as before and allocate_capacity() folds into
grow_capacity(). Every access to the memory is behind a non-zero size,
so it comes after a growth.

A state that never grows its memory now allocates none. The life of a
Memory takes 10 instructions instead of 301 when it is never grown, and
33 to 73 more when it is, from the out-of-line first growth (GCC 15,
malloc(), realloc() and free() included). Baseline keeps one state for
each call depth, Advanced creates one for each execution.
@codspeed

codspeed Bot commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will regress 6 benchmarks

1 warning

⚠️ Base and head ran on different runner types

Some benchmarks with significant performance changes were compared across different runner types, so the changes might not come from the code. Run base and head on the same runner type, or use CodSpeed Macro Runners for more stable results.

Learn more about Macro Runners

Open the report in CodSpeed to investigate

⚡ 7 improved benchmarks
❌ 6 regressed benchmarks
✅ 886 untouched benchmarks

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ test_codecopy[fork_Osaka-blockchain_test-fixed_src_dst_True-0.75x max code size-benchmark-gas-value_1M] 42.1 ms 42.8 ms -1.69%
❌ test_calldatacopy_from_call[fork_Osaka-blockchain_test-calldata_size_1024-non_zero_data_False-fixed_src_dst_True-benchmark-gas-value_1M] 5.6 ms 5.7 ms -1.68%
❌ test_push[fork_Osaka-blockchain_test-opcode_PUSH1-benchmark-gas-value_1M] 9.7 ms 9.9 ms -1.66%
❌ test_push[fork_Osaka-blockchain_test-opcode_PUSH0-benchmark-gas-value_1M] 13 ms 13.2 ms -1.33%
❌ test_dup[fork_Osaka-blockchain_test-opcode_DUP2-benchmark-gas-value_1M] 9.5 ms 9.6 ms -1.29%
❌ test_calldatasize[fork_Osaka-blockchain_test-zero_data_False-calldata_size_0-benchmark-gas-value_1M] 14.2 ms 14.3 ms -1.23%
⚡ test_calldatasize[fork_Osaka-blockchain_test-zero_data_True-calldata_size_32-benchmark-gas-value_1M] 14.4 ms 14 ms +2.49%
⚡ test_block_context_ops[fork_Osaka-blockchain_test-opcode_BASEFEE-benchmark-gas-value_1M] 19.1 ms 18.8 ms +1.52%
⚡ test_gas_op[fork_Osaka-blockchain_test-benchmark-gas-value_1M] 12.3 ms 12.1 ms +1.41%
⚡ test_block_context_ops[fork_Osaka-blockchain_test-opcode_CHAINID-benchmark-gas-value_1M] 19 ms 18.8 ms +1.27%
⚡ test_dup[fork_Osaka-blockchain_test-opcode_DUP3-benchmark-gas-value_1M] 9.6 ms 9.5 ms +1.13%
⚡ test_callvalue_from_call[fork_Osaka-blockchain_test-non_zero_value_True-benchmark-gas-value_1M] 6.2 ms 6.1 ms +1.11%
⚡ test_pc_op[fork_Osaka-blockchain_test-benchmark-gas-value_1M] 13 ms 12.9 ms +1.1%

Tip

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


Comparing evm/allocate-memory-on-growth (b20a4da) with master (fc9264a)

Open in CodSpeed

@codecov

codecov Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 33.33333% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 98.24%. Comparing base (fc9264a) to head (b20a4da).

Files with missing lines Patch % Lines
lib/evmone/execution_state.hpp 33.33% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1795      +/-   ##
==========================================
- Coverage   98.24%   98.24%   -0.01%     
==========================================
  Files         186      186              
  Lines       17175    17173       -2     
  Branches     3921     3921              
==========================================
- Hits        16873    16871       -2     
  Misses        233      233              
  Partials       69       69              
Flag Coverage Δ
eest-develop-gmp 26.95% <33.33%> (-0.01%) ⬇️
eest-legacy 17.22% <33.33%> (-0.01%) ⬇️
eest-libsecp256k1 29.11% <33.33%> (-0.01%) ⬇️
eest-stable 84.25% <33.33%> (+0.01%) ⬆️
evmone-unittests 94.11% <33.33%> (-0.01%) ⬇️
riscv64 94.82% <33.33%> (-0.01%) ⬇️

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

Components Coverage Δ
core 96.62% <33.33%> (-0.01%) ⬇️
tooling 94.87% <ø> (ø)
tests 99.82% <ø> (ø)
Files with missing lines Coverage Δ
lib/evmone/vm.cpp 94.44% <ø> (ø)
lib/evmone/execution_state.hpp 95.00% <33.33%> (-0.17%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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