Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass gas to instructions by value #600

Merged
merged 2 commits into from
Apr 13, 2023
Merged

Pass gas to instructions by value #600

merged 2 commits into from
Apr 13, 2023

Conversation

chfast
Copy link
Member

@chfast chfast commented Mar 28, 2023

  • Pass and return gas counter to instructions implementations by value, don't use ExecutionState::gas_left. This improves performance.
  • Introduce Result type {status, gas}.
  • Change StopToken to TermResult (strong alias of Result).
  • Remove gas_left from ExecutionState.

@chfast chfast added the optimization Iproves performance without functional changes label Mar 28, 2023
@codecov
Copy link

codecov bot commented Mar 28, 2023

Codecov Report

Merging #600 (f2dc3cf) into master (f1928c6) will decrease coverage by 0.01%.
The diff coverage is 100.00%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #600      +/-   ##
==========================================
- Coverage   97.21%   97.21%   -0.01%     
==========================================
  Files          74       74              
  Lines        7575     7571       -4     
==========================================
- Hits         7364     7360       -4     
  Misses        211      211              
Flag Coverage Δ
blockchaintests 65.16% <97.45%> (+1.55%) ⬆️
statetests 63.22% <89.87%> (+0.63%) ⬆️
unittests 94.11% <100.00%> (-0.01%) ⬇️

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

Impacted Files Coverage Δ
lib/evmone/baseline.hpp 100.00% <ø> (ø)
test/unittests/execution_state_test.cpp 100.00% <ø> (ø)
lib/evmone/advanced_analysis.hpp 100.00% <100.00%> (ø)
lib/evmone/advanced_instructions.cpp 100.00% <100.00%> (ø)
lib/evmone/baseline.cpp 100.00% <100.00%> (ø)
lib/evmone/execution_state.hpp 94.00% <100.00%> (-0.55%) ⬇️
lib/evmone/instructions.hpp 100.00% <100.00%> (ø)
lib/evmone/instructions_calls.cpp 100.00% <100.00%> (ø)
lib/evmone/instructions_storage.cpp 100.00% <100.00%> (ø)

@chfast
Copy link
Member Author

chfast commented Mar 28, 2023

Benchmarks

Haswell, GCC12

-4.0%
baseline/execute/main/blake2b_huff/empty_mean                                -0.0067         -0.0067            10            10            10            10
bnocgoto/execute/main/blake2b_huff/empty_mean                                -0.0532         -0.0532            11            10            11            10
baseline/execute/main/blake2b_huff/8415nulls_mean                            -0.0091         -0.0091           635           629           635           629
bnocgoto/execute/main/blake2b_huff/8415nulls_mean                            -0.0472         -0.0472           647           617           647           617
baseline/execute/main/blake2b_shifts/8415nulls_mean                          -0.0218         -0.0218          6041          5909          6041          5909
bnocgoto/execute/main/blake2b_shifts/8415nulls_mean                          -0.0542         -0.0543          6891          6517          6891          6517
baseline/execute/main/sha1_divs/empty_mean                                   -0.0367         -0.0367            44            42            44            42
bnocgoto/execute/main/sha1_divs/empty_mean                                   -0.0545         -0.0545            46            43            46            43
baseline/execute/main/sha1_divs/5311_mean                                    -0.0297         -0.0297          3520          3415          3520          3415
bnocgoto/execute/main/sha1_divs/5311_mean                                    -0.0594         -0.0594          3623          3408          3623          3408
baseline/execute/main/sha1_shifts/empty_mean                                 -0.0265         -0.0265            21            20            21            20
bnocgoto/execute/main/sha1_shifts/empty_mean                                 -0.0387         -0.0387            22            21            22            21
baseline/execute/main/sha1_shifts/5311_mean                                  -0.0299         -0.0299          1688          1637          1688          1637
bnocgoto/execute/main/sha1_shifts/5311_mean                                  -0.0400         -0.0400          1741          1672          1741          1672
baseline/execute/main/snailtracer/benchmark_mean                             -0.0175         -0.0175         33676         33085         33676         33085
bnocgoto/execute/main/snailtracer/benchmark_mean                             -0.0072         -0.0071         33618         33378         33618         33377
baseline/execute/main/structarray_alloc/nfts_rank_mean                       -0.0745         -0.0745           413           382           413           382
bnocgoto/execute/main/structarray_alloc/nfts_rank_mean                       -0.1055         -0.1055           435           389           435           389
baseline/execute/main/swap_math/spent_mean                                   -0.0850         -0.0850             2             2             2             2
bnocgoto/execute/main/swap_math/spent_mean                                   -0.0596         -0.0596             2             2             2             2
baseline/execute/main/swap_math/received_mean                                -0.0537         -0.0537             2             2             2             2
bnocgoto/execute/main/swap_math/received_mean                                -0.0654         -0.0654             3             2             3             2
baseline/execute/main/swap_math/insufficient_liquidity_mean                  -0.0629         -0.0629             1             1             1             1
bnocgoto/execute/main/swap_math/insufficient_liquidity_mean                  -0.0767         -0.0767             1             1             1             1
baseline/execute/main/weierstrudel/1_mean                                    -0.0181         -0.0181           175           172           175           172
bnocgoto/execute/main/weierstrudel/1_mean                                    +0.0181         +0.0181           170           173           170           173
baseline/execute/main/weierstrudel/15_mean                                   -0.0151         -0.0151          1655          1630          1655          1630
bnocgoto/execute/main/weierstrudel/15_mean                                   +0.0009         +0.0009          1629          1631          1629          1631
OVERALL_GEOMEAN                                                              -0.0408         -0.0408             0             0             0             0

Haswell, Clang16

-3.3%
baseline/execute/main/blake2b_huff/empty_mean                                -0.0198         -0.0198            10            10            10            10
bnocgoto/execute/main/blake2b_huff/empty_mean                                -0.0613         -0.0613            11            10            11            10
baseline/execute/main/blake2b_huff/8415nulls_mean                            -0.0273         -0.0273           619           602           619           602
bnocgoto/execute/main/blake2b_huff/8415nulls_mean                            -0.0591         -0.0591           690           649           690           649
baseline/execute/main/blake2b_shifts/8415nulls_mean                          -0.0243         -0.0243          5909          5765          5909          5765
bnocgoto/execute/main/blake2b_shifts/8415nulls_mean                          -0.0444         -0.0444          7071          6757          7071          6757
baseline/execute/main/sha1_divs/empty_mean                                   -0.0157         -0.0157            44            44            44            44
bnocgoto/execute/main/sha1_divs/empty_mean                                   -0.0215         -0.0215            47            46            47            46
baseline/execute/main/sha1_divs/5311_mean                                    -0.0089         -0.0089          3541          3510          3541          3510
bnocgoto/execute/main/sha1_divs/5311_mean                                    -0.0190         -0.0190          3719          3648          3719          3648
baseline/execute/main/sha1_shifts/empty_mean                                 -0.0120         -0.0120            23            23            23            23
bnocgoto/execute/main/sha1_shifts/empty_mean                                 -0.0388         -0.0388            25            24            25            24
baseline/execute/main/sha1_shifts/5311_mean                                  -0.0154         -0.0154          1884          1855          1884          1855
bnocgoto/execute/main/sha1_shifts/5311_mean                                  -0.0343         -0.0343          2035          1965          2035          1965
baseline/execute/main/snailtracer/benchmark_mean                             -0.0296         -0.0296         32101         31151         32100         31150
bnocgoto/execute/main/snailtracer/benchmark_mean                             -0.0318         -0.0317         34371         33280         34371         33280
baseline/execute/main/structarray_alloc/nfts_rank_mean                       -0.0386         -0.0386           452           435           452           435
bnocgoto/execute/main/structarray_alloc/nfts_rank_mean                       -0.0336         -0.0336           479           463           479           463
baseline/execute/main/swap_math/spent_mean                                   -0.0284         -0.0284             2             2             2             2
bnocgoto/execute/main/swap_math/spent_mean                                   -0.0778         -0.0778             2             2             2             2
baseline/execute/main/swap_math/received_mean                                -0.0354         -0.0354             2             2             2             2
bnocgoto/execute/main/swap_math/received_mean                                -0.0922         -0.0922             3             3             3             3
baseline/execute/main/swap_math/insufficient_liquidity_mean                  -0.0248         -0.0248             1             1             1             1
bnocgoto/execute/main/swap_math/insufficient_liquidity_mean                  -0.0880         -0.0880             2             1             2             1
baseline/execute/main/weierstrudel/1_mean                                    -0.0072         -0.0072           155           154           155           154
bnocgoto/execute/main/weierstrudel/1_mean                                    -0.0156         -0.0156           155           152           155           152
baseline/execute/main/weierstrudel/15_mean                                   -0.0110         -0.0110          1512          1495          1512          1495
bnocgoto/execute/main/weierstrudel/15_mean                                   -0.0216         -0.0216          1533          1500          1533          1500
OVERALL_GEOMEAN                                                              -0.0337         -0.0337             0             0             0             0

Skylake, Clang17

-2.4%
baseline/execute/main/blake2b_huff/empty_mean                                -0.0002         -0.0002            11            11            11            11
bnocgoto/execute/main/blake2b_huff/empty_mean                                -0.0045         -0.0045            13            13            13            13
baseline/execute/main/blake2b_huff/8415nulls_mean                            -0.0112         -0.0112           709           701           709           701
bnocgoto/execute/main/blake2b_huff/8415nulls_mean                            -0.0070         -0.0070           796           790           796           790
baseline/execute/main/blake2b_shifts/8415nulls_mean                          -0.0322         -0.0322          6653          6439          6653          6439
bnocgoto/execute/main/blake2b_shifts/8415nulls_mean                          -0.0286         -0.0286          8009          7780          8009          7780
baseline/execute/main/sha1_divs/empty_mean                                   -0.0216         -0.0216            49            48            49            48
bnocgoto/execute/main/sha1_divs/empty_mean                                   -0.0272         -0.0272            51            50            51            50
baseline/execute/main/sha1_divs/5311_mean                                    -0.0328         -0.0328          3932          3803          3932          3803
bnocgoto/execute/main/sha1_divs/5311_mean                                    -0.0285         -0.0285          4065          3949          4065          3949
baseline/execute/main/sha1_shifts/empty_mean                                 -0.0163         -0.0163            26            25            26            25
bnocgoto/execute/main/sha1_shifts/empty_mean                                 -0.0217         -0.0217            28            28            28            28
baseline/execute/main/sha1_shifts/5311_mean                                  -0.0134         -0.0134          2105          2077          2105          2077
bnocgoto/execute/main/sha1_shifts/5311_mean                                  -0.0319         -0.0318          2329          2254          2329          2254
baseline/execute/main/snailtracer/benchmark_mean                             -0.0312         -0.0312         36091         34964         36089         34963
bnocgoto/execute/main/snailtracer/benchmark_mean                             -0.0050         -0.0049         38960         38767         38958         38766
baseline/execute/main/structarray_alloc/nfts_rank_mean                       -0.0042         -0.0041           499           497           499           497
bnocgoto/execute/main/structarray_alloc/nfts_rank_mean                       -0.0851         -0.0851           574           525           574           525
baseline/execute/main/swap_math/spent_mean                                   -0.0369         -0.0369             2             2             2             2
bnocgoto/execute/main/swap_math/spent_mean                                   -0.0766         -0.0766             3             3             3             3
baseline/execute/main/swap_math/received_mean                                -0.0253         -0.0253             3             2             3             2
bnocgoto/execute/main/swap_math/received_mean                                -0.0952         -0.0952             3             3             3             3
baseline/execute/main/swap_math/insufficient_liquidity_mean                  -0.0254         -0.0254             2             1             2             1
bnocgoto/execute/main/swap_math/insufficient_liquidity_mean                  -0.0609         -0.0608             2             2             2             2
baseline/execute/main/weierstrudel/1_mean                                    +0.0153         +0.0153           166           169           166           169
bnocgoto/execute/main/weierstrudel/1_mean                                    +0.0272         +0.0272           166           170           166           170
baseline/execute/main/weierstrudel/15_mean                                   +0.0028         +0.0028          1625          1629          1625          1629
bnocgoto/execute/main/weierstrudel/15_mean                                   +0.0076         +0.0076          1632          1644          1632          1644
OVERALL_GEOMEAN                                                              -0.0243         -0.0243             0             0             0             0

Apple M1, Clang16

-3.4%
baseline/execute/main/blake2b_huff/empty_mean                                -0.0468         -0.0468             7             7             7             7
bnocgoto/execute/main/blake2b_huff/empty_mean                                -0.0225         -0.0225            10            10            10            10
baseline/execute/main/blake2b_huff/8415nulls_mean                            -0.0361         -0.0361           458           441           458           441
bnocgoto/execute/main/blake2b_huff/8415nulls_mean                            -0.0243         -0.0243           637           621           637           621
baseline/execute/main/blake2b_shifts/8415nulls_mean                          -0.0413         -0.0413          4020          3854          4020          3854
bnocgoto/execute/main/blake2b_shifts/8415nulls_mean                          -0.0415         -0.0415          5598          5366          5598          5366
baseline/execute/main/sha1_divs/empty_mean                                   -0.0356         -0.0358            34            32            34            32
bnocgoto/execute/main/sha1_divs/empty_mean                                   -0.0446         -0.0432            35            34            35            34
baseline/execute/main/sha1_divs/5311_mean                                    -0.0481         -0.0480          2638          2511          2638          2511
bnocgoto/execute/main/sha1_divs/5311_mean                                    -0.0477         -0.0475          2779          2647          2779          2647
baseline/execute/main/sha1_shifts/empty_mean                                 -0.0567         -0.0566            15            14            15            14
bnocgoto/execute/main/sha1_shifts/empty_mean                                 -0.0433         -0.0435            23            22            23            22
baseline/execute/main/sha1_shifts/5311_mean                                  -0.0583         -0.0583          1211          1140          1211          1140
bnocgoto/execute/main/sha1_shifts/5311_mean                                  -0.0460         -0.0460          1888          1801          1888          1801
baseline/execute/main/snailtracer/benchmark_mean                             -0.0282         -0.0282         19673         19119         19673         19119
bnocgoto/execute/main/snailtracer/benchmark_mean                             -0.0228         -0.0228         22516         22003         22516         22003
baseline/execute/main/structarray_alloc/nfts_rank_mean                       -0.0816         -0.0818           328           302           328           301
bnocgoto/execute/main/structarray_alloc/nfts_rank_mean                       -0.0343         -0.0343           451           436           451           436
baseline/execute/main/swap_math/spent_mean                                   -0.0605         -0.0604             1             1             1             1
bnocgoto/execute/main/swap_math/spent_mean                                   +0.0023         +0.0010             2             2             2             2
baseline/execute/main/swap_math/received_mean                                -0.0375         -0.0384             2             1             2             1
bnocgoto/execute/main/swap_math/received_mean                                -0.0111         -0.0111             2             2             2             2
baseline/execute/main/swap_math/insufficient_liquidity_mean                  -0.0512         -0.0515             1             1             1             1
bnocgoto/execute/main/swap_math/insufficient_liquidity_mean                  -0.0079         -0.0080             1             1             1             1
baseline/execute/main/weierstrudel/1_mean                                    -0.0094         -0.0095           123           122           123           122
bnocgoto/execute/main/weierstrudel/1_mean                                    +0.0028         +0.0028           124           124           124           124
baseline/execute/main/weierstrudel/15_mean                                   -0.0254         -0.0254          1157          1127          1157          1127
bnocgoto/execute/main/weierstrudel/15_mean                                   -0.0096         -0.0096          1177          1165          1177          1165
OVERALL_GEOMEAN                                                              -0.0347         -0.0348             0             0             0             0

@chfast chfast force-pushed the tracing_gas branch 2 times, most recently from abec11f to 477dd5b Compare April 6, 2023 14:14
Base automatically changed from tracing_gas to master April 6, 2023 14:28
@@ -7,7 +7,7 @@
namespace evmone::instr::core
{
template <Opcode Op>
evmc_status_code call_impl(StackTop stack, ExecutionState& state) noexcept
Result call_impl(StackTop stack, int64_t gas_left, ExecutionState& state) noexcept
Copy link
Member

Choose a reason for hiding this comment

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

Nit: it's called gas in invoke wrappers, should it be also gas here?

Suggested change
Result call_impl(StackTop stack, int64_t gas_left, ExecutionState& state) noexcept
Result call_impl(StackTop stack, int64_t gas, ExecutionState& state) noexcept

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm switching from gas_left to gas because seems good enough.

Copy link
Member Author

Choose a reason for hiding this comment

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

I will leave gas_left in instructions this time because it comes from state.gas_left. This slightly limits the number of change.s

chfast added 2 commits April 13, 2023 12:12
- Pass to and return gas counter to instructions implementations by
  value, don't use ExecutionState::gas_left. This improves performance.
- Introduce Result type {status, gas}.
- Change StopToken to TermResult (strong alias of Result).
@chfast chfast merged commit 7028b6b into master Apr 13, 2023
@chfast chfast deleted the gas branch April 13, 2023 10:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
optimization Iproves performance without functional changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants