Skip to content
Open
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
48 changes: 27 additions & 21 deletions core/state/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,19 +419,25 @@
if v, ok := r.subTries.Load(addr); ok {
return v.(Trie), nil
}
// Optimize: serialize creation to avoid speculative allocation storms on cache misses.
r.lock.Lock()
defer r.lock.Unlock()

if v, ok := r.subTries.Load(addr); ok {
return v.(Trie), nil
}

root, err := r.resolveSubRoot(addr)
if err != nil {
return nil, err
}

newTr, err := trie.NewStateTrie(trie.StorageTrieID(r.root, crypto.Keccak256Hash(addr.Bytes()), root), r.db)
if err != nil {
return nil, err
}
newTr.EnableConcurrentReads()
// First-writer-wins: another goroutine may have created it.
if existing, loaded := r.subTries.LoadOrStore(addr, newTr); loaded {
return existing.(Trie), nil
}
r.subTries.Store(addr, newTr)
return newTr, nil
}

Expand Down Expand Up @@ -587,8 +593,8 @@
}

// newReaderWithCache constructs the reader with local cache.
func newReaderWithCache(reader Reader) *readerWithCache {
return &readerWithCache{
func newReaderWithCache(reader Reader) *readerWithcache {

Check failure on line 596 in core/state/reader.go

View workflow job for this annotation

GitHub Actions / Build (ubuntu-22.04)

undefined: readerWithcache

Check failure on line 596 in core/state/reader.go

View workflow job for this annotation

GitHub Actions / Analyze (go)

undefined: readerWithcache

Check failure on line 596 in core/state/reader.go

View workflow job for this annotation

GitHub Actions / integration-tests (ubuntu-22.04)

undefined: readerWithcache

Check failure on line 596 in core/state/reader.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-22.04)

undefined: readerWithcache

Check failure on line 596 in core/state/reader.go

View workflow job for this annotation

GitHub Actions / unit-tests (ubuntu-22.04)

undefined: readerWithcache
return &readerWithcache{
Reader: reader,
}
}
Expand All @@ -601,7 +607,7 @@
//
// It also returns the cache entry (for provenance/unique-usage accounting)
// and whether this call inserted a new entry (first-writer-wins).
func (r *readerWithCache) account(addr common.Address, caller readerRole) (*types.StateAccount, bool, *accountCacheEntry, bool, error) {
func (r *readerWithcache) account(addr common.Address, caller readerRole) (*types.StateAccount, bool, *accountCacheEntry, bool, error) {

Check failure on line 610 in core/state/reader.go

View workflow job for this annotation

GitHub Actions / Build (ubuntu-22.04)

undefined: readerWithcache

Check failure on line 610 in core/state/reader.go

View workflow job for this annotation

GitHub Actions / Analyze (go)

undefined: readerWithcache

Check failure on line 610 in core/state/reader.go

View workflow job for this annotation

GitHub Actions / integration-tests (ubuntu-22.04)

undefined: readerWithcache

Check failure on line 610 in core/state/reader.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-22.04)

undefined: readerWithcache

Check failure on line 610 in core/state/reader.go

View workflow job for this annotation

GitHub Actions / unit-tests (ubuntu-22.04)

undefined: readerWithcache
// Try to resolve the requested account in the local cache (lock-free read).
if v, ok := r.accounts.Load(addr); ok {
ent := v.(*accountCacheEntry)
Expand All @@ -627,7 +633,7 @@
// The returned account might be nil if it's not existent.
//
// An error will be returned if the state is corrupted in the underlying reader.
func (r *readerWithCache) Account(addr common.Address) (*types.StateAccount, error) {
func (r *readerWithcache) Account(addr common.Address) (*types.StateAccount, error) {

Check failure on line 636 in core/state/reader.go

View workflow job for this annotation

GitHub Actions / Build (ubuntu-22.04)

undefined: readerWithcache

Check failure on line 636 in core/state/reader.go

View workflow job for this annotation

GitHub Actions / Analyze (go)

undefined: readerWithcache

Check failure on line 636 in core/state/reader.go

View workflow job for this annotation

GitHub Actions / integration-tests (ubuntu-22.04)

undefined: readerWithcache

Check failure on line 636 in core/state/reader.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-22.04)

undefined: readerWithcache

Check failure on line 636 in core/state/reader.go

View workflow job for this annotation

GitHub Actions / unit-tests (ubuntu-22.04)

undefined: readerWithcache
account, _, _, _, err := r.account(addr, roleUnknown)
return account, err
}
Expand All @@ -638,7 +644,7 @@
//
// It also returns the cache entry (for provenance/unique-usage accounting)
// and whether this call inserted a new entry (first-writer-wins).
func (r *readerWithCache) storage(addr common.Address, slot common.Hash, caller readerRole) (common.Hash, bool, *storageCacheEntry, bool, error) {
func (r *readerWithcache) storage(addr common.Address, slot common.Hash, caller readerRole) (common.Hash, bool, *storageCacheEntry, bool, error) {

Check failure on line 647 in core/state/reader.go

View workflow job for this annotation

GitHub Actions / Build (ubuntu-22.04)

undefined: readerWithcache

Check failure on line 647 in core/state/reader.go

View workflow job for this annotation

GitHub Actions / Analyze (go)

undefined: readerWithcache

Check failure on line 647 in core/state/reader.go

View workflow job for this annotation

GitHub Actions / integration-tests (ubuntu-22.04)

undefined: readerWithcache

Check failure on line 647 in core/state/reader.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-22.04)

undefined: readerWithcache

Check failure on line 647 in core/state/reader.go

View workflow job for this annotation

GitHub Actions / unit-tests (ubuntu-22.04)

undefined: readerWithcache
key := storageKey{addr: addr, slot: slot}

// Try to resolve the requested storage slot in the local cache (lock-free read).
Expand Down Expand Up @@ -667,13 +673,13 @@
// existent.
//
// An error will be returned if the state is corrupted in the underlying reader.
func (r *readerWithCache) Storage(addr common.Address, slot common.Hash) (common.Hash, error) {
func (r *readerWithcache) Storage(addr common.Address, slot common.Hash) (common.Hash, error) {

Check failure on line 676 in core/state/reader.go

View workflow job for this annotation

GitHub Actions / Build (ubuntu-22.04)

undefined: readerWithcache

Check failure on line 676 in core/state/reader.go

View workflow job for this annotation

GitHub Actions / Analyze (go)

undefined: readerWithcache

Check failure on line 676 in core/state/reader.go

View workflow job for this annotation

GitHub Actions / integration-tests (ubuntu-22.04)

undefined: readerWithcache

Check failure on line 676 in core/state/reader.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-22.04)

undefined: readerWithcache

Check failure on line 676 in core/state/reader.go

View workflow job for this annotation

GitHub Actions / unit-tests (ubuntu-22.04)

undefined: readerWithcache
value, _, _, _, err := r.storage(addr, slot, roleUnknown)
return value, err
}

type readerWithCacheStats struct {
*readerWithCache
type readerWithcacheStats struct {
*readerWithcache

Check failure on line 682 in core/state/reader.go

View workflow job for this annotation

GitHub Actions / Build (ubuntu-22.04)

undefined: readerWithcache

Check failure on line 682 in core/state/reader.go

View workflow job for this annotation

GitHub Actions / Analyze (go)

undefined: readerWithcache

Check failure on line 682 in core/state/reader.go

View workflow job for this annotation

GitHub Actions / integration-tests (ubuntu-22.04)

undefined: readerWithcache

Check failure on line 682 in core/state/reader.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-22.04)

undefined: readerWithcache

Check failure on line 682 in core/state/reader.go

View workflow job for this annotation

GitHub Actions / unit-tests (ubuntu-22.04)

undefined: readerWithcache
role readerRole

accountHit atomic.Int64
Expand All @@ -694,9 +700,9 @@
}

// newReaderWithCacheStats constructs the reader with additional statistics tracked.
func newReaderWithCacheStats(reader *readerWithCache, role readerRole) *readerWithCacheStats {
return &readerWithCacheStats{
readerWithCache: reader,
func newReaderWithcacheStats(reader *readerWithcache, role readerRole) *readerWithcacheStats {

Check failure on line 703 in core/state/reader.go

View workflow job for this annotation

GitHub Actions / Build (ubuntu-22.04)

undefined: readerWithcache

Check failure on line 703 in core/state/reader.go

View workflow job for this annotation

GitHub Actions / Analyze (go)

undefined: readerWithcache

Check failure on line 703 in core/state/reader.go

View workflow job for this annotation

GitHub Actions / integration-tests (ubuntu-22.04)

undefined: readerWithcache

Check failure on line 703 in core/state/reader.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-22.04)

undefined: readerWithcache

Check failure on line 703 in core/state/reader.go

View workflow job for this annotation

GitHub Actions / unit-tests (ubuntu-22.04)

undefined: readerWithcache
return &readerWithcacheStats{
readerWithcache: reader,
role: role,
}
}
Expand All @@ -705,8 +711,8 @@
// The returned account might be nil if it's not existent.
//
// An error will be returned if the state is corrupted in the underlying reader.
func (r *readerWithCacheStats) Account(addr common.Address) (*types.StateAccount, error) {
account, incache, ent, inserted, err := r.readerWithCache.account(addr, r.role)
func (r *readerWithcacheStats) Account(addr common.Address) (*types.StateAccount, error) {
account, incache, ent, inserted, err := r.readerWithcache.account(addr, r.role)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -735,8 +741,8 @@
// existent.
//
// An error will be returned if the state is corrupted in the underlying reader.
func (r *readerWithCacheStats) Storage(addr common.Address, slot common.Hash) (common.Hash, error) {
value, incache, entCopy, inserted, err := r.readerWithCache.storage(addr, slot, r.role)
func (r *readerWithcacheStats) Storage(addr common.Address, slot common.Hash) (common.Hash, error) {
value, incache, entCopy, inserted, err := r.readerWithcache.storage(addr, slot, r.role)
if err != nil {
return common.Hash{}, err
}
Expand All @@ -759,7 +765,7 @@
}

// GetStats implements ReaderWithStats, returning the statistics of state reader.
func (r *readerWithCacheStats) GetStats() ReaderStats {
func (r *readerWithcacheStats) GetStats() ReaderStats {
return ReaderStats{
AccountHit: r.accountHit.Load(),
AccountMiss: r.accountMiss.Load(),
Expand All @@ -769,7 +775,7 @@
}

// GetPrefetchStats returns attribution statistics for evaluating prefetch effectiveness.
func (r *readerWithCacheStats) GetPrefetchStats() PrefetchStats {
func (r *readerWithcacheStats) GetPrefetchStats() PrefetchStats {
return PrefetchStats{
AccountHitFromPrefetch: r.accountHitFromPrefetch.Load(),
StorageHitFromPrefetch: r.storageHitFromPrefetch.Load(),
Expand Down
Loading