Skip to content

Commit

Permalink
tweak: Use lru.Cache instead of lru.BasicLRU.
Browse files Browse the repository at this point in the history
  • Loading branch information
tyler-smith committed Sep 18, 2023
1 parent 0506a50 commit d706b39
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 18 deletions.
12 changes: 4 additions & 8 deletions eth/tracers/blocknative/decoder/cache.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package decoder

import (
"sync"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/lru"
)
Expand All @@ -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),
}
}
10 changes: 0 additions & 10 deletions eth/tracers/blocknative/decoder/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}
Expand All @@ -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
}
Expand Down

0 comments on commit d706b39

Please sign in to comment.