Skip to content

perf(state): eliminate speculative trie allocation storm via double-c…#2286

Open
shinobi133 wants to merge 4 commits into
0xPolygon:developfrom
shinobi133:fix/state-cache-performance-opt
Open

perf(state): eliminate speculative trie allocation storm via double-c…#2286
shinobi133 wants to merge 4 commits into
0xPolygon:developfrom
shinobi133:fix/state-cache-performance-opt

Conversation

@shinobi133

Copy link
Copy Markdown

…hecked locking

Summary

<one paragraph: what changed and why. If the change has measurable impact, add a small table or numbers (e.g. perf delta on a kurtosis devnet, mutation kill rate). Link the JIRA / GH issue inline if there is one — not required.>

Executed tests

<what was actually run beyond CI's standard unit / integration / e2e gates: kurtosis scenarios, chaos runs, manual checks against Amoy / mainnet RPCs, devnet upgrades, etc. Include output or pointers to where the run lives.>

Rollout notes

<consensus-affecting? requires coordinated upgrade? backwards-compatible? operator-facing change?>

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adjusts trieReader.subTrieConcurrent in core/state to avoid speculative per-address sub-trie allocations when many goroutines concurrently miss the subTries cache, aiming to reduce allocation/CPU spikes during parallel state reads.

Changes:

  • Added a double-checked locking pattern around sub-trie creation to ensure only one goroutine creates and stores a missing sub-trie.
  • Switched from LoadOrStore “first-writer-wins” to explicit mutex-guarded Store after creation.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread core/state/reader.go Outdated
Comment on lines +419 to +421
if v, ok := r.subTries.Load(addr); ok {
return v.(Trie), nil
}
Comment thread core/state/reader.go Outdated
Comment on lines +419 to +441
if v, ok := r.subTries.Load(addr); ok {
return v.(Trie), nil
}
// Optimize: Use r.lock to synchronize creation and prevent speculative allocation storms
r.lock.Lock()
if v, ok := r.subTries.Load(addr); ok {
r.lock.Unlock()
return v.(Trie), nil
}
root, err := r.resolveSubRoot(addr)
if err != nil {
r.lock.Unlock()
return nil, err
}
newTr, err := trie.NewStateTrie(trie.StorageTrieID(r.root, crypto.Keccak256Hash(addr.Bytes()), root), r.db)
if err != nil {
r.lock.Unlock()
return nil, err
}
newTr.EnableConcurrentReads()
r.subTries.Store(addr, newTr)
r.lock.Unlock()
return newTr, nil
Comment thread core/state/reader.go Outdated
if v, ok := r.subTries.Load(addr); ok {
return v.(Trie), nil
}
// Optimize: Use r.lock to synchronize creation and prevent speculative allocation storms
@pratikspatil024

Copy link
Copy Markdown
Member

@shinobi133 - please fix the CI (lint) and address the comments

@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants