core: only pull out state once

This commit is contained in:
Marius van der Wijden 2025-06-23 12:01:22 +02:00 committed by Gary Rong
parent ee68abeaa1
commit b6417f2efe
3 changed files with 17 additions and 31 deletions

View file

@ -18,6 +18,7 @@ package vm
import ( import (
"math" "math"
"math/big"
"testing" "testing"
"time" "time"
@ -77,29 +78,19 @@ func TestLoopInterrupt(t *testing.T) {
func BenchmarkInterpreter(b *testing.B) { func BenchmarkInterpreter(b *testing.B) {
var ( var (
evm = NewEVM(BlockContext{}, nil, params.TestChainConfig, Config{}) statedb, _ = state.New(types.EmptyRootHash, state.NewDatabaseForTesting())
startGas uint64 = 30_000_000 evm = NewEVM(BlockContext{BlockNumber: big.NewInt(1), Time: 1, Random: &common.Hash{}}, statedb, params.MergedTestChainConfig, Config{})
value = uint256.NewInt(0) startGas uint64 = 100_000_000
contract = NewContract(common.Address{}, common.Address{}, value, startGas, nil) value = uint256.NewInt(0)
stack = newstack()
mem = NewMemory()
contract = NewContract(common.Address{}, common.Address{}, value, startGas, nil)
) )
contract.Code = common.Hex2Bytes(loopInterruptTests[0]) stack.push(uint256.NewInt(123))
stack.push(uint256.NewInt(123))
gasSStoreEIP3529 = makeGasSStoreFunc(params.SstoreClearsScheduleRefundEIP3529)
b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
evm.interpreter.Run(contract, []byte{}, false) gasSStoreEIP3529(evm, contract, stack, mem, 1234)
} }
} }
// BenchmarkInterpreter-14 14613990 79.01 ns/op 32 B/op 2 allocs/op
func BenchmarkDup(b *testing.B) {
stack := newstack()
stack.push(uint256.NewInt(1234))
stack.push(uint256.NewInt(4321))
for range b.N {
for range 1_000_000 {
//stack.dup(1)
stack.dup1()
}
}
}
// BenchmarkDup-14 38 31579698 ns/op 188685761 B/op 2 allocs/op

View file

@ -34,10 +34,10 @@ func makeGasSStoreFunc(clearingRefund uint64) gasFunc {
} }
// Gas sentry honoured, do the actual gas calculation based on the stored value // Gas sentry honoured, do the actual gas calculation based on the stored value
var ( var (
y, x = stack.Back(1), stack.peek() y, x = stack.Back(1), stack.peek()
slot = common.Hash(x.Bytes32()) slot = common.Hash(x.Bytes32())
current = evm.StateDB.GetState(contract.Address(), slot) current, original = evm.StateDB.GetStateAndCommittedState(contract.Address(), slot)
cost = uint64(0) cost = uint64(0)
) )
// Check slot presence in the access list // Check slot presence in the access list
if _, slotPresent := evm.StateDB.SlotInAccessList(contract.Address(), slot); !slotPresent { if _, slotPresent := evm.StateDB.SlotInAccessList(contract.Address(), slot); !slotPresent {
@ -52,7 +52,6 @@ func makeGasSStoreFunc(clearingRefund uint64) gasFunc {
// return params.SloadGasEIP2200, nil // return params.SloadGasEIP2200, nil
return cost + params.WarmStorageReadCostEIP2929, nil // SLOAD_GAS return cost + params.WarmStorageReadCostEIP2929, nil // SLOAD_GAS
} }
original := evm.StateDB.GetCommittedState(contract.Address(), x.Bytes32())
if original == current { if original == current {
if original == (common.Hash{}) { // create slot (2.1.1) if original == (common.Hash{}) { // create slot (2.1.1)
return cost + params.SstoreSetGasEIP2200, nil return cost + params.SstoreSetGasEIP2200, nil

View file

@ -117,10 +117,6 @@ func (st *Stack) dup(n int) {
st.push(&st.data[st.len()-n]) st.push(&st.data[st.len()-n])
} }
func (st *Stack) dup1() {
st.push(&st.data[st.len()-1])
}
func (st *Stack) peek() *uint256.Int { func (st *Stack) peek() *uint256.Int {
return &st.data[st.len()-1] return &st.data[st.len()-1]
} }