Skip to content

evm: Wrap the execution gas counter in a strong type - #1564

Open
chfast wants to merge 2 commits into
masterfrom
evm/gas-strong-type
Open

chfast wants to merge 2 commits into
masterfrom
evm/gas-strong-type

Conversation

@chfast

@chfast chfast commented Jun 11, 2026 •

Copy link
Copy Markdown
Member

Wrap the execution gas counter in the Gas type so it no longer mixes
silently with other int64_t values (stack items, sizes, EVMC fields).
Conversions are explicit; the operators take raw int64_t costs, so
instruction implementations and cost tables are unchanged. The second
commit uses Gas also in the transaction execution of test/state.

Gas is an enum class rather than a class to keep the generated code
identical: the disassembly of libevmone (GCC 16, Clang 21, Release)
differs from master only by the order of a few instructions in cold
paths.

This prepares for changing the gas check implementation in one place.

evmone::advanced::AdvancedExecutionState st;
st.gas_left = 1;
st.gas_left = evmone::Gas{1};
st.gas_refund = 2;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't these also use it?

The execution gas counter is a bare int64_t, so it mixes silently with
stack values, sizes and the int64 fields of EVMC. Wrap it in Gas: the
conversions from and to int64_t are explicit, and the arithmetic and
comparison operators take raw int64_t costs, so the instruction bodies
and the cost tables stay unchanged. Gas is converted to int64_t only at
the EVMC boundary, for the GAS instruction and for tracing.

Gas is an enum class, not a class: with a class GCC makes different
inlining and register allocation decisions in the dispatch loops. With
the enum the generated code of both GCC and Clang is the same as before
except for the order of a few instructions in cold paths.

The StateGas charge() and refill() become templates because the test
state transition still uses them with raw int64_t gas.
Use Gas also for the execution gas of the top-level message: for the
authorization list costs and the recipient account creation of
EIP-7702/EIP-2780 and for the code deposit cost. The message gas is set
from it right before the call. The StateGas charge() and refill() then
take Gas and are no longer templates.
@chfast
chfast force-pushed the evm/gas-strong-type branch from d436cff to 6687d11 Compare October 23, 2026 16:51
@chfast chfast changed the title Wrap gas counter (gas_left) in a strong Gas type evm: Wrap the execution gas counter in a strong type Oct 23, 2026
@codspeed

codspeed Bot commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will degrade performance by 0.32%

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
❌ 13 regressed benchmarks
✅ 879 untouched benchmarks

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ test_push[fork_Osaka-blockchain_test-opcode_PUSH4-benchmark-gas-value_1M] 10.8 ms 12.2 ms -12.1%
❌ test_gas_op[fork_Osaka-blockchain_test-benchmark-gas-value_1M] 12.2 ms 12.7 ms -3.99%
❌ baseline/analyse/main/sha1_divs 12.3 µs 12.7 µs -3.15%
❌ test_dup[fork_Osaka-blockchain_test-opcode_DUP16-benchmark-gas-value_1M] 9.7 ms 9.9 ms -1.92%
❌ test_dup[fork_Osaka-blockchain_test-opcode_DUP9-benchmark-gas-value_1M] 9.7 ms 9.8 ms -1.6%
❌ test_dup[fork_Osaka-blockchain_test-opcode_DUP3-benchmark-gas-value_1M] 9.7 ms 9.8 ms -1.48%
❌ test_dup[fork_Osaka-blockchain_test-opcode_DUP15-benchmark-gas-value_1M] 9.7 ms 9.8 ms -1.43%
❌ test_dup[fork_Osaka-blockchain_test-opcode_DUP13-benchmark-gas-value_1M] 9.7 ms 9.8 ms -1.41%
❌ test_calldatasize[fork_Osaka-blockchain_test-zero_data_False-calldata_size_32-benchmark-gas-value_1M] 14.3 ms 14.5 ms -1.39%
❌ test_msize[fork_Osaka-blockchain_test-mem_size_0-benchmark-gas-value_1M] 13.6 ms 13.8 ms -1.21%
❌ test_block_context_ops[fork_Osaka-blockchain_test-opcode_BASEFEE-benchmark-gas-value_1M] 19 ms 19.2 ms -1.09%
❌ test_pc_op[fork_Osaka-blockchain_test-benchmark-gas-value_1M] 14.2 ms 14.3 ms -1.05%
❌ test_block_context_ops[fork_Osaka-blockchain_test-opcode_NUMBER-benchmark-gas-value_1M] 18.1 ms 18.3 ms -1.04%
⚡ test_push[fork_Osaka-blockchain_test-opcode_PUSH12-benchmark-gas-value_1M] 32.2 ms 28.8 ms +11.52%
⚡ test_push[fork_Osaka-blockchain_test-opcode_PUSH9-benchmark-gas-value_1M] 27 ms 24.3 ms +10.81%
⚡ test_calldatasize[fork_Osaka-blockchain_test-zero_data_False-calldata_size_0-benchmark-gas-value_1M] 14.5 ms 14.3 ms +1.36%
⚡ test_block_context_ops[fork_Osaka-blockchain_test-opcode_COINBASE-benchmark-gas-value_1M] 23.7 ms 23.4 ms +1.32%
⚡ test_block_context_ops[fork_Osaka-blockchain_test-opcode_TIMESTAMP-benchmark-gas-value_1M] 18.2 ms 18 ms +1.31%
⚡ test_calldatasize[fork_Osaka-blockchain_test-zero_data_True-calldata_size_32-benchmark-gas-value_1M] 14.5 ms 14.3 ms +1.29%
⚡ test_calldatasize[fork_Osaka-blockchain_test-zero_data_True-calldata_size_256-benchmark-gas-value_1M] 14.6 ms 14.4 ms +1.09%

Tip

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


Comparing evm/gas-strong-type (6687d11) with master (0412867)

Open in CodSpeed

@codecov

codecov Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.23%. Comparing base (0412867) to head (6687d11).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1564   +/-   ##
=======================================
  Coverage   98.23%   98.23%           
=======================================
  Files         185      186    +1     
  Lines       17131    17143   +12     
  Branches     3912     3912           
=======================================
+ Hits        16829    16841   +12     
  Misses        233      233           
  Partials       69       69           
Flag Coverage Δ
eest-develop-gmp 26.95% <80.64%> (+0.03%) ⬆️
eest-legacy 17.20% <64.51%> (+0.04%) ⬆️
eest-libsecp256k1 29.12% <80.64%> (+0.03%) ⬆️
eest-stable 84.25% <89.28%> (-0.01%) ⬇️
evmone-unittests 94.10% <93.54%> (+<0.01%) ⬆️
riscv64 94.81% <93.54%> (+<0.01%) ⬆️

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

Components Coverage Δ
core 96.61% <100.00%> (+<0.01%) ⬆️
tooling 94.87% <ø> (ø)
tests 99.82% <100.00%> (ø)
Files with missing lines Coverage Δ
lib/evmone/advanced_analysis.hpp 100.00% <100.00%> (ø)
lib/evmone/advanced_instructions.cpp 96.87% <100.00%> (ø)
lib/evmone/baseline_execution.cpp 97.61% <100.00%> (ø)
lib/evmone/execution_state.hpp 94.44% <100.00%> (ø)
lib/evmone/gas.hpp 100.00% <100.00%> (ø)
lib/evmone/instructions.hpp 100.00% <100.00%> (ø)
lib/evmone/instructions_calls.cpp 100.00% <100.00%> (ø)
lib/evmone/instructions_storage.cpp 100.00% <ø> (ø)
lib/evmone/state_gas.hpp 100.00% <ø> (ø)
test/state/host.cpp 100.00% <100.00%> (ø)
... and 2 more
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@chfast
chfast requested a balanced review from Copilot October 23, 2026 20:15
@chfast
chfast marked this pull request as ready for review October 23, 2026 20:15

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

Static review supports that the type migration preserves existing gas arithmetic and boundary conversions, with no blocking issues identified.

No open issues 🎉

What changed in this PR

Introduces a strong Gas type for execution gas counters, preventing implicit mixing with unrelated integers and preparing gas checks for centralized changes.

Changes:

  • Adds an int64_t-backed enum with arithmetic and comparison operators.
  • Migrates interpreter and transaction execution counters while retaining raw integer costs.
  • Makes conversions explicit at EVMC, stack, and tracing boundaries.
File Description
test/​unittests/​execution_state_test.cpp Updates gas initialization in the reset test.
test/​state/​state.cpp Uses Gas for transaction preparation charges.
test/​state/​host.cpp Uses Gas during contract code deployment.
lib/​evmone/​state_gas.hpp Accepts Gas counters for charges and refills.
lib/​evmone/​instructions.hpp Migrates instruction signatures and results to Gas.
lib/​evmone/​instructions_storage.cpp Updates storage instruction signatures.
lib/​evmone/​instructions_calls.cpp Migrates call/create gas handling and EVMC conversions.
lib/​evmone/​gas.hpp Defines the strong gas type and operators.
lib/​evmone/​execution_state.hpp Converts typed gas when constructing execution results.
lib/​evmone/​baseline_execution.cpp Migrates baseline dispatch and tracing conversions.
lib/​evmone/​advanced_instructions.cpp Updates advanced instruction adapters and stack conversion.
lib/​evmone/​advanced_analysis.hpp Stores and initializes advanced execution gas as Gas.

Review effort: Balanced

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.

3 participants