Skip to content

commitment: move BranchCache behind PatriciaContext interface #20218

@mh0lt

Description

@mh0lt

Summary

The persistent BranchCache (added in PR #19954) is currently a direct field on HexPatriciaHashed. It should be moved behind the PatriciaContext interface so that:

  1. Concurrent trie subtries can each have their own context with seamless cache access — one PatriciaContext per subtrie, cache shared or partitioned as needed.
  2. The commitment package public API stays lean — no additional accessor surface on Trie or HexPatriciaHashed.
  3. Cache lifecycle is tied to context lifecycle — creation, invalidation, and cleanup follow the context rather than the trie struct.

Current state (PR #19954)

  • BranchCache is a field on HexPatriciaHashed
  • GetBranchCache() / SetBranchCache() are on the Trie interface
  • Cache is cleared on Reset() (fork safety)
  • Cache is invalidated on fold write paths (delete, propagate, branch)

Proposed refactoring

  • Move BranchCache ownership to PatriciaContext implementations (e.g. TrieContext)
  • PatriciaContext interface gets GetBranchCache() / SetBranchCache() methods
  • Remove BranchCache field and accessors from HexPatriciaHashed / Trie interface
  • branchFromCacheOrDB reads cache via hph.ctx.GetBranchCache() instead of hph.branchCache
  • Fold write invalidation calls through context

Context

Review feedback from @awskii on PR #19954:

"this cache can be implementation behind PatriciaContext because concurrent trie requires multiple contexts (one per subtrie) and behind this iface it can be seamlessly integrated"
"better keep cache behind patriciaContext and not bloating commitment"

🤖 Generated with Claude Code

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions