multigas: use unchecked add to improve performance#644
Open
multigas: use unchecked add to improve performance#644
Conversation
Replacing safe/saturating adds with unchecked ones can improve performance by up to ~19% in synthetic benchmarks. We can safely replace checked adds when we know that there isn't a chance of overflow. For instance, in the EVM.run() function, a uint64 gas value won't overflow because the program would run out of gas first. Here are some benchmark results of programs that * Without optmization: ``` $ go test -bench=BenchmarkSimpleLoop -benchtime=300x ./core/vm/runtime/ goos: linux goarch: amd64 pkg: github.com/ethereum/go-ethereum/core/vm/runtime cpu: AMD Ryzen 9 7900 12-Core Processor BenchmarkSimpleLoop/staticcall-identity-100M-24 300 351708000 ns/op 224673426 B/op 6164445 allocs/op BenchmarkSimpleLoop/call-identity-100M-24 300 423641326 ns/op 241626949 B/op 6711476 allocs/op BenchmarkSimpleLoop/loop-100M-24 300 205678035 ns/op 3858 B/op 42 allocs/op BenchmarkSimpleLoop/loop2-100M-24 300 220230885 ns/op 3859 B/op 42 allocs/op BenchmarkSimpleLoop/call-nonexist-100M-24 300 608773800 ns/op 149273581 B/op 3731316 allocs/op BenchmarkSimpleLoop/call-EOA-100M-24 300 412351029 ns/op 95533857 B/op 2985050 allocs/op BenchmarkSimpleLoop/call-reverting-100M-24 300 622313948 ns/op 412718387 B/op 5715753 allocs/op PASS ``` * With optmization: ``` $ go test -bench=BenchmarkSimpleLoop -benchtime=300x ./core/vm/runtime/ goos: linux goarch: amd64 pkg: github.com/ethereum/go-ethereum/core/vm/runtime cpu: AMD Ryzen 9 7900 12-Core Processor BenchmarkSimpleLoop/staticcall-identity-100M-24 300 337222851 ns/op 224673593 B/op 6164445 allocs/op BenchmarkSimpleLoop/call-identity-100M-24 300 403063559 ns/op 241626933 B/op 6711476 allocs/op BenchmarkSimpleLoop/loop-100M-24 300 183246194 ns/op 3859 B/op 42 allocs/op BenchmarkSimpleLoop/loop2-100M-24 300 203989420 ns/op 3858 B/op 42 allocs/op BenchmarkSimpleLoop/call-nonexist-100M-24 300 521509487 ns/op 149273509 B/op 3731316 allocs/op BenchmarkSimpleLoop/call-EOA-100M-24 300 333846242 ns/op 95533859 B/op 2985050 allocs/op BenchmarkSimpleLoop/call-reverting-100M-24 300 593545585 ns/op 412723111 B/op 5715769 allocs/op PASS ```
gligneul
added a commit
to OffchainLabs/nitro
that referenced
this pull request
Mar 31, 2026
Pulls in OffchainLabs/go-ethereum#644 Close NIT-4668
Decrement single gas and increase multi-gas in a single function, ensuring we don't make mistakes of incrementing multi-gas without checking for overflow.
These functions were made specificly for the interpreter, we probably don't want them to be used in other places.
Contributor
MishkaRogachev
left a comment
There was a problem hiding this comment.
Everything looks correct, just some nitpicks
| if op == SELFDESTRUCT && cost == params.SelfdestructGasEIP150 { | ||
| usedMultiGas.SaturatingIncrementInto(multigas.ResourceKindComputation, params.WarmStorageReadCostEIP2929) | ||
| usedMultiGas.SaturatingIncrementInto(multigas.ResourceKindStorageAccessWrite, cost-params.WarmStorageReadCostEIP2929) | ||
| // It is safe to call UncheckedIncrementInto because because we checked single gas. |
| return ErrOutOfGas | ||
| } | ||
| c.Gas -= dynamicCost | ||
| // It is safe to call UncheckedAddInto because because we checked single gas. |
| return z, true | ||
| } | ||
|
|
||
| for i := 0; i < int(NumResourceKind); i++ { |
Contributor
There was a problem hiding this comment.
Since you are editing this file anyway, maybe update all plain for loops here to range-based form: for i := range int(NumResourceKind)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Replacing safe/saturating adds with unchecked ones can improve performance by up to ~19% in synthetic benchmarks. We can safely replace checked adds when we know that there isn't a chance of overflow. For instance, in the EVM.run() function, a uint64 gas value won't overflow because the program would run out of gas first.
Here are some benchmark results of programs that
Pulled in by OffchainLabs/nitro#4585