Skip to content

evm: Find the execution state by comparing pointers - #1787

Draft
chfast wants to merge 1 commit into
evm/execution-contextfrom
evm/execution-context-lookup
Draft

chfast wants to merge 1 commit into
evm/execution-contextfrom
evm/execution-context-lookup

Conversation

@chfast

@chfast chfast commented Oct 27, 2026 •

Copy link
Copy Markdown
Member

ExecutionContext::get_execution_state() compares the depth with the vector's size(), which divides its byte size by sizeof(ExecutionState) (424): a multiplication by the inverse, and on rv64 also 7 instructions to build that 64-bit constant. This compares the pointer to the depth's state with the end instead. The depth is within the reserved capacity of 1025 states, so the pointer stays inside the allocated array. A debug assert checks this before the pointer arithmetic, so a depth outside the contract still stops there, as it did in the slow path before.

On x86 the lookup in execute() becomes imul, add, cmp, jae instead of 10 instructions. execute() gets shorter by 8 instructions with GCC 15, 6 with GCC 14 and 7 with Clang 21, and by 11 on rv64 with zilkworm's SP1 guest flags. No other function changes, and the dispatch loops are identical with all four. Under callgrind, the CALL-heavy benchmark fixture system/return_revert runs 6 fewer instructions per message call (186,538 over 31,090 calls).

Ported to zilkworm's evmone fork, it saves 0.025% of SP1 cycles on top of #1770 on a 100-block mainnet sample (2.30M of 9.37G cycles), with 99 of the 100 blocks faster and gas unchanged.

@codecov

codecov Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.24%. Comparing base (8243352) to head (9923925).

Additional details and impacted files
@@                  Coverage Diff                   @@
##           evm/execution-context    #1787   +/-   ##
======================================================
  Coverage                  98.24%   98.24%           
======================================================
  Files                        187      187           
  Lines                      17208    17210    +2     
  Branches                    3919     3919           
======================================================
+ Hits                       16906    16908    +2     
  Misses                       233      233           
  Partials                      69       69           
Flag Coverage Δ
eest-develop-gmp 26.97% <100.00%> (+<0.01%) ⬆️
eest-legacy 17.26% <100.00%> (+<0.01%) ⬆️
eest-libsecp256k1 29.13% <100.00%> (+<0.01%) ⬆️
eest-stable 84.25% <100.00%> (+<0.01%) ⬆️
evmone-unittests 94.12% <100.00%> (+<0.01%) ⬆️
riscv64 94.83% <100.00%> (+<0.01%) ⬆️

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

Components Coverage Δ
core 96.63% <100.00%> (+<0.01%) ⬆️
tooling 94.89% <ø> (ø)
tests 99.82% <ø> (ø)
Files with missing lines Coverage Δ
lib/evmone/execution_context.hpp 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 Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will degrade performance by 1.94%

❌ 5 regressed benchmarks
✅ 894 untouched benchmarks

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ test_calldatasize[fork_Osaka-blockchain_test-zero_data_False-calldata_size_0-benchmark-gas-value_1M] 14.1 ms 14.5 ms -2.42%
❌ test_calldatasize[fork_Osaka-blockchain_test-zero_data_False-calldata_size_256-benchmark-gas-value_1M] 14.1 ms 14.4 ms -2.34%
❌ test_calldatasize[fork_Osaka-blockchain_test-zero_data_True-calldata_size_1024-benchmark-gas-value_1M] 14.1 ms 14.4 ms -2.2%
❌ 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.56%
❌ test_calldatasize[fork_Osaka-blockchain_test-zero_data_True-calldata_size_32-benchmark-gas-value_1M] 14.3 ms 14.5 ms -1.16%

Tip

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


Comparing evm/execution-context-lookup (9923925) with evm/execution-context (8243352)

Open in CodSpeed

get_execution_state() compared the depth with size(), which divides the
vector's byte size by sizeof(ExecutionState): a multiplication by its
inverse, and on rv64 also 7 instructions to build that 64-bit constant.
Compare the pointer to the depth's state with the end instead. The depth
is within the reserved capacity, so the pointer is too; a debug assert
checks it before the pointer arithmetic.

The lookup in execute() takes 4 instructions instead of 10 on x86 with
GCC and Clang. execute() gets 6 to 8 instructions shorter on x86 and 11
on rv64 with zilkworm's SP1 guest flags. The dispatch loops don't change.
@chfast
chfast force-pushed the evm/execution-context-lookup branch from cb44c76 to 9923925 Compare October 27, 2026 20:44

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