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

feature(op-geth): optimize validJumpdest #4

Open
wants to merge 1 commit into
base: cf
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion core/opcodeCompiler/compiler/OpCodeCache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package compiler
import (
"encoding/json"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/lru"
"github.com/ethereum/go-ethereum/log"
"os"
"os/signal"
Expand All @@ -20,6 +21,7 @@ type OptCode []byte

type OpCodeCache struct {
opcodesCache map[common.Address]OptCode
bitvecCache *lru.SizeConstrainedCache[common.Hash, []byte]
codeCacheMutex sync.RWMutex
codeCacheSize uint64
}
Expand Down Expand Up @@ -47,6 +49,18 @@ func (c *OpCodeCache) GetCachedCode(address common.Address) OptCode {
return processedCode
}

func (c *OpCodeCache) GetBitvecCache(codeHash common.Hash) []byte {
bitvec, ok := c.bitvecCache.Get(codeHash)
if !ok {
bitvec = nil
}
return bitvec
}

func (c *OpCodeCache) AddBitvecCache(codeHash common.Hash, bitvec []byte) {
c.bitvecCache.Add(codeHash, bitvec)
}

func (c *OpCodeCache) UpdateCodeCache(address common.Address, code OptCode) error {

c.codeCacheMutex.Lock()
Expand All @@ -70,11 +84,15 @@ func (c *OpCodeCache) UpdateCodeCache(address common.Address, code OptCode) erro

var opcodeCache *OpCodeCache

const codeCacheFileName = "codecache.json"
const (
codeCacheFileName = "codecache.json"
bitvecCacheSize = 64 * 1024 * 1024
)

func init() {
opcodeCache = &OpCodeCache{
opcodesCache: make(map[common.Address]OptCode, CodeCacheGCThreshold>>10),
bitvecCache: lru.NewSizeConstrainedCache[common.Hash, []byte](bitvecCacheSize),
codeCacheMutex: sync.RWMutex{},
}

Expand Down
15 changes: 15 additions & 0 deletions core/opcodeCompiler/compiler/opcodeProcessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@ func LoadOptimizedCode(address common.Address) OptCode {

}

func LoadBitvec(codeHash common.Hash) []byte {
if !enabled {
return nil
}
bitvec := codeCache.GetBitvecCache(codeHash)
return bitvec
}

func StoreBitvec(codeHash common.Hash, bitvec []byte) {
if !enabled {
return
}
codeCache.AddBitvecCache(codeHash, bitvec)
}

func GenOrLoadOptimizedCode(address common.Address, code []byte) {
if !enabled {
return
Expand Down
14 changes: 12 additions & 2 deletions core/vm/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package vm

import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/opcodeCompiler/compiler"
"github.com/holiman/uint256"
"math/big"
)
Expand Down Expand Up @@ -115,8 +116,17 @@ func (c *Contract) isCode(udest uint64) bool {
if !exist {
// Do the analysis and save in parent context
// We do not need to store it in c.analysis
analysis = codeBitmap(c.Code)
c.jumpdests[c.CodeHash] = analysis
if c.optimized {
analysis = compiler.LoadBitvec(c.CodeHash)
if analysis == nil {
analysis = codeBitmap(c.Code)
compiler.StoreBitvec(c.CodeHash, analysis)
}
c.jumpdests[c.CodeHash] = analysis
} else {
analysis = codeBitmap(c.Code)
c.jumpdests[c.CodeHash] = analysis
}
}
// Also stash it in current contract for faster access
c.analysis = analysis
Expand Down
Loading