From d706b393063aa6d16adf4485c7760697a5d99345 Mon Sep 17 00:00:00 2001 From: Tyler Smith Date: Mon, 18 Sep 2023 08:13:45 -0400 Subject: [PATCH] tweak: Use lru.Cache instead of lru.BasicLRU. --- eth/tracers/blocknative/decoder/cache.go | 12 ++++-------- eth/tracers/blocknative/decoder/decoder.go | 10 ---------- 2 files changed, 4 insertions(+), 18 deletions(-) diff --git a/eth/tracers/blocknative/decoder/cache.go b/eth/tracers/blocknative/decoder/cache.go index f392b7875cf3..34155f883f47 100644 --- a/eth/tracers/blocknative/decoder/cache.go +++ b/eth/tracers/blocknative/decoder/cache.go @@ -1,8 +1,6 @@ package decoder import ( - "sync" - "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/lru" ) @@ -13,15 +11,13 @@ const ( ) type Caches struct { - contractsMu sync.RWMutex - contracts lru.BasicLRU[common.Address, *Contract] - assetsMu sync.RWMutex - assets lru.BasicLRU[AssetID, *AssetMetadata] + contracts *lru.Cache[common.Address, *Contract] + assets *lru.Cache[AssetID, *AssetMetadata] } func NewCaches() *Caches { return &Caches{ - contracts: lru.NewBasicLRU[common.Address, *Contract](cacheSizeContracts), - assets: lru.NewBasicLRU[AssetID, *AssetMetadata](cacheSizeAssets), + contracts: lru.NewCache[common.Address, *Contract](cacheSizeContracts), + assets: lru.NewCache[AssetID, *AssetMetadata](cacheSizeAssets), } } diff --git a/eth/tracers/blocknative/decoder/decoder.go b/eth/tracers/blocknative/decoder/decoder.go index 2e4b5161f450..c0f0e26488dd 100644 --- a/eth/tracers/blocknative/decoder/decoder.go +++ b/eth/tracers/blocknative/decoder/decoder.go @@ -66,9 +66,7 @@ func (d *Decoder) DecodeCallFrame(sender common.Address, receiver common.Address // DecodeContract decodes the contract at the given address. func (d *Decoder) DecodeContract(addr common.Address) (*Contract, error) { // Check the cache for an existing entry. - d.caches.contractsMu.RLock() contract, ok := d.caches.contracts.Get(addr) - d.caches.contractsMu.RUnlock() if ok { if contract == nil { return nil, ErrAccountNotAContract @@ -81,9 +79,7 @@ func (d *Decoder) DecodeContract(addr common.Address) (*Contract, error) { // have code. bytecode := ByteCode(d.evm.GetCode(addr)) if len(bytecode) == 0 { - d.caches.contractsMu.Lock() d.caches.contracts.Add(addr, nil) - d.caches.contractsMu.Unlock() return nil, ErrAccountNotAContract } @@ -92,9 +88,7 @@ func (d *Decoder) DecodeContract(addr common.Address) (*Contract, error) { if err != nil { return nil, err } - d.caches.contractsMu.Lock() d.caches.contracts.Add(addr, contract) - d.caches.contractsMu.Unlock() return contract, nil } @@ -121,9 +115,7 @@ func (d *Decoder) decodeAsset(contract *Contract, assetID AssetID) (*AssetMetada } // Check the cache for an existing entry. - d.caches.assetsMu.RLock() asset, ok := d.caches.assets.Get(assetID) - d.caches.assetsMu.RUnlock() if ok { return asset, nil } @@ -135,9 +127,7 @@ func (d *Decoder) decodeAsset(contract *Contract, assetID AssetID) (*AssetMetada return nil, err } - d.caches.assetsMu.Lock() d.caches.assets.Add(assetID, asset) - d.caches.assetsMu.Unlock() return asset, nil }