core/vm: Idea for enabling an experimental evm#34645
Draft
kevaundray wants to merge 6 commits intoethereum:masterfrom
Draft
core/vm: Idea for enabling an experimental evm#34645kevaundray wants to merge 6 commits intoethereum:masterfrom
kevaundray wants to merge 6 commits intoethereum:masterfrom
Conversation
- Move common charge gas functionality into shared function - add flag for triggering the experimental interpreter
kevaundray
commented
Apr 3, 2026
Comment on lines
-201
to
-224
| // All ops with a dynamic memory usage also has a dynamic gas cost. | ||
| var operation *operation | ||
| var memorySize uint64 | ||
| if operation.dynamicGas != nil { | ||
| // calculate the new memory size and expand the memory to fit | ||
| // the operation | ||
| // Memory check needs to be done prior to evaluating the dynamic gas portion, | ||
| // to detect calculation overflows | ||
| if operation.memorySize != nil { | ||
| memSize, overflow := operation.memorySize(stack) | ||
| if overflow { | ||
| return nil, ErrGasUintOverflow | ||
| } | ||
| // memory is expanded in words of 32 bytes. Gas | ||
| // is also calculated in words. | ||
| if memorySize, overflow = math.SafeMul(toWordSize(memSize), 32); overflow { | ||
| return nil, ErrGasUintOverflow | ||
| } | ||
| } | ||
| // Consume the gas and return an error if not enough gas is available. | ||
| // cost is explicitly set so that the capture state defer method can get the proper cost | ||
| var dynamicCost uint64 | ||
| dynamicCost, err = operation.dynamicGas(evm, contract, stack, mem, memorySize) | ||
| cost += dynamicCost // for tracing | ||
| if err != nil { |
Contributor
Author
There was a problem hiding this comment.
This is the biggest diff on existing code. This functionality is used in the experimental EVM so I pulled it out into chargeGasOp function to avoid code duplication
kevaundray
commented
Apr 3, 2026
Comment on lines
+49
to
+50
| // - all inlined opcodes only touch the stack, so executing a few extra ops | ||
| // before detecting OOG has no observable side effects. |
Contributor
Author
There was a problem hiding this comment.
This optimization in spirit, just says; we will execute and group "basic blocks" wrt their gas cost
kevaundray
commented
Apr 3, 2026
| contract.Gas -= gasUsed | ||
| return nil, &ErrStackUnderflow{stackLen: stack.len(), required: 2} | ||
| } | ||
| x, y := stack.pop(), stack.peek() |
Contributor
Author
There was a problem hiding this comment.
Its possible to remove these methods, and use the raw stack slice. It saves about 7-8.5% but makes the code harder to read
kevaundray
commented
Apr 3, 2026
| continue // skip pc++ | ||
| } | ||
| // Flush gas and fall back to normal dispatch | ||
| default: |
Contributor
Author
There was a problem hiding this comment.
Here we just fall back to the normal interpreter for everything else
kevaundray
commented
Apr 3, 2026
| x, y := stack.pop(), stack.peek() | ||
| y.Sub(&x, y) | ||
|
|
||
| case MUL: |
Contributor
Author
There was a problem hiding this comment.
sidenote: A different way to have done this, would have been to use go generate
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.
This is a simple idea for enabling an experimental EVM that can be developed/optimized alongside the normal EVM.
Currently it only optimizes, commonly used compute opcodes using a switch dispatch (the savings are about 50%), and then fallsback to the normal interpreter for everything else. None of the optimization techniques here are new.