core/vm: add precompile cache

This commit is contained in:
MariusVanDerWijden 2025-06-11 15:32:01 +02:00
parent ec880b1737
commit 759f0100e0

View file

@ -1,17 +1,24 @@
package vm package vm
import ( import (
"sync"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/lru" "github.com/ethereum/go-ethereum/common/lru"
) )
var caches map[common.Address]*lru.Cache[string, []byte] var (
caches map[common.Address]*lru.Cache[string, []byte]
mu sync.RWMutex
)
func init() { func init() {
caches = make(map[common.Address]*lru.Cache[string, []byte]) caches = make(map[common.Address]*lru.Cache[string, []byte])
} }
func addCache(precompile common.Address, input, output []byte) { func addCache(precompile common.Address, input, output []byte) {
mu.Lock()
defer mu.Unlock()
cache, ok := caches[precompile] cache, ok := caches[precompile]
if !ok { if !ok {
cache = lru.NewCache[string, []byte](128) cache = lru.NewCache[string, []byte](128)
@ -21,6 +28,8 @@ func addCache(precompile common.Address, input, output []byte) {
} }
func getCache(precompile common.Address, input []byte) ([]byte, bool) { func getCache(precompile common.Address, input []byte) ([]byte, bool) {
mu.RLock()
defer mu.RUnlock()
cache, ok := caches[precompile] cache, ok := caches[precompile]
if !ok { if !ok {
caches[precompile] = lru.NewCache[string, []byte](128) caches[precompile] = lru.NewCache[string, []byte](128)