Skip to content

core/state: reduce lock contention in triePrefetcher.used()#2049

Merged
cffls merged 2 commits intodevelopfrom
prefetcher_lock
Feb 11, 2026
Merged

core/state: reduce lock contention in triePrefetcher.used()#2049
cffls merged 2 commits intodevelopfrom
prefetcher_lock

Conversation

@cffls
Copy link
Copy Markdown
Contributor

@cffls cffls commented Feb 10, 2026

Description

The used() method previously held a global write lock while doing a map lookup, a blocking fetcher.wait(), and slice appends — serializing all N parallel IntermediateRoot goroutines behind a single mutex. Fix this by downgrading to a read lock for the map lookup and moving slice appends behind a per-subfetcher usedLock via a new appendUsed() method.

Changes

  • Bugfix (non-breaking change that solves an issue)
  • Hotfix (change that solves an urgent issue, and requires immediate attention)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (change that is not backwards-compatible and/or changes current functionality)
  • Changes only for a subset of nodes

Breaking changes

Please complete this section if any breaking changes have been made, otherwise delete it

Nodes audience

In case this PR includes changes that must be applied only to a subset of nodes, please specify how you handled it (e.g. by adding a flag with a default value...)

Checklist

  • I have added at least 2 reviewer or the whole pos-v1 team
  • I have added sufficient documentation in code
  • I will be resolving comments - if any - by pushing each fix in a separate commit and linking the commit hash in the comment reply
  • Created a task in Jira and informed the team for implementation in Erigon client (if applicable)
  • Includes RPC methods changes, and the Notion documentation has been updated

Cross repository changes

  • This PR requires changes to heimdall
    • In case link the PR here:
  • This PR requires changes to matic-cli
    • In case link the PR here:

Testing

  • I have added unit tests
  • I have added tests to CI
  • I have tested this code manually on local environment
  • I have tested this code manually on remote devnet using express-cli
  • I have tested this code manually on amoy
  • I have created new e2e tests into express-cli

Manual tests

Please complete this section with the steps you performed if you ran manual tests for this functionality, otherwise delete it

Additional comments

Please post additional comments in this section if you have them, otherwise delete it

The used() method previously held a global write lock while doing a map lookup, a blocking fetcher.wait(), and slice appends — serializing all N parallel IntermediateRoot goroutines behind a single mutex. Fix this by downgrading to a read lock for the map lookup and moving slice appends behind a per-subfetcher usedLock via a new appendUsed() method.
@claude
Copy link
Copy Markdown

claude bot commented Feb 10, 2026

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

@codecov
Copy link
Copy Markdown

codecov bot commented Feb 10, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 50.20%. Comparing base (96df644) to head (3d859b5).
⚠️ Report is 3 commits behind head on develop.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #2049      +/-   ##
===========================================
+ Coverage    50.15%   50.20%   +0.04%     
===========================================
  Files          871      871              
  Lines       150614   150621       +7     
===========================================
+ Hits         75546    75624      +78     
+ Misses       70023    69952      -71     
  Partials      5045     5045              
Files with missing lines Coverage Δ
core/state/trie_prefetcher.go 75.43% <100.00%> (+15.85%) ⬆️

... and 20 files with indirect coverage changes

Files with missing lines Coverage Δ
core/state/trie_prefetcher.go 75.43% <100.00%> (+15.85%) ⬆️

... and 20 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown
Contributor

@lucca30 lucca30 left a comment

Choose a reason for hiding this comment

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

Besides the lint fix, the code looks pretty good. Nice finding on replacing a global lock by a more granular one.

@sonarqubecloud
Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
5.4% Duplication on New Code (required ≤ 3%)

See analysis details on SonarQube Cloud

@kamuikatsurgi
Copy link
Copy Markdown
Member

Misclicked on CoPilot.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request optimizes the triePrefetcher.used() method to reduce lock contention when multiple goroutines update usage statistics for different subfetchers concurrently. The optimization is particularly beneficial during IntermediateRoot processing, where multiple goroutines operate on independent storage tries.

Changes:

  • Refactored triePrefetcher.used() to use a read lock instead of a write lock for map lookups, allowing concurrent access by multiple goroutines
  • Introduced per-subfetcher usedLock to protect usedAddr and usedSlot slice appends, replacing the previous global write lock
  • Added comprehensive tests to verify correctness of concurrent access and validate that the optimization actually provides parallelism

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
core/state/trie_prefetcher.go Refactored used() method to use read lock; added appendUsed() method with per-subfetcher locking; updated report() to acquire usedLock when reading usage data
core/state/trie_prefetcher_test.go Added three new tests: TestConcurrentUsed (correctness), TestConcurrentUsedParallelism (performance), and TestUsedStateCorrectAfterReport (integration with report())

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

@cffls cffls merged commit 6abc9cb into develop Feb 11, 2026
28 of 29 checks passed
kamuikatsurgi pushed a commit that referenced this pull request Feb 13, 2026
* core/state: reduce lock contention in triePrefetcher.used()

The used() method previously held a global write lock while doing a map lookup, a blocking fetcher.wait(), and slice appends — serializing all N parallel IntermediateRoot goroutines behind a single mutex. Fix this by downgrading to a read lock for the map lookup and moving slice appends behind a per-subfetcher usedLock via a new appendUsed() method.

* fix linter
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.

6 participants