|
| 1 | +package commitment |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "sync/atomic" |
| 6 | + |
| 7 | + "golang.org/x/sync/errgroup" |
| 8 | +) |
| 9 | + |
| 10 | +// BranchPrefetcher pre-fetches branch nodes into the persistent BranchCache |
| 11 | +// in the background, triggered at TouchKey time (during execution) so that |
| 12 | +// branch data is already cached when Process/HashSort runs later. |
| 13 | +// |
| 14 | +// This gives the trie a warm persistent cache without waiting for HashSort. |
| 15 | +type BranchPrefetcher struct { |
| 16 | + cache *BranchCache |
| 17 | + ctxFactory TrieContextFactory |
| 18 | + maxDepth int |
| 19 | + numWorkers int |
| 20 | + |
| 21 | + work chan []byte // hashed keys to prefetch branches for |
| 22 | + g *errgroup.Group |
| 23 | + ctx context.Context |
| 24 | + cancel context.CancelFunc |
| 25 | + |
| 26 | + prefetched atomic.Uint64 |
| 27 | + started atomic.Bool |
| 28 | + closed atomic.Bool |
| 29 | +} |
| 30 | + |
| 31 | +// NewBranchPrefetcher creates a prefetcher that populates the given BranchCache. |
| 32 | +// Call Start() to begin processing, then Submit() hashed keys as they arrive. |
| 33 | +func NewBranchPrefetcher(cache *BranchCache, ctxFactory TrieContextFactory, numWorkers, maxDepth int) *BranchPrefetcher { |
| 34 | + ctx, cancel := context.WithCancel(context.Background()) |
| 35 | + return &BranchPrefetcher{ |
| 36 | + cache: cache, |
| 37 | + ctxFactory: ctxFactory, |
| 38 | + maxDepth: maxDepth, |
| 39 | + numWorkers: numWorkers, |
| 40 | + ctx: ctx, |
| 41 | + cancel: cancel, |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +// Start launches background prefetch workers. |
| 46 | +func (p *BranchPrefetcher) Start() { |
| 47 | + if p.started.Swap(true) { |
| 48 | + return |
| 49 | + } |
| 50 | + p.work = make(chan []byte, p.numWorkers*128) |
| 51 | + p.g, p.ctx = errgroup.WithContext(p.ctx) |
| 52 | + |
| 53 | + for i := 0; i < p.numWorkers; i++ { |
| 54 | + p.g.Go(func() error { |
| 55 | + trieCtx, cleanup := p.ctxFactory() |
| 56 | + if cleanup != nil { |
| 57 | + defer cleanup() |
| 58 | + } |
| 59 | + |
| 60 | + for hashedKey := range p.work { |
| 61 | + select { |
| 62 | + case <-p.ctx.Done(): |
| 63 | + return p.ctx.Err() |
| 64 | + default: |
| 65 | + } |
| 66 | + p.prefetchBranches(trieCtx, hashedKey) |
| 67 | + } |
| 68 | + return nil |
| 69 | + }) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// prefetchBranches walks nibble prefixes of hashedKey and loads branch nodes |
| 74 | +// into the persistent cache. Stops at first missing branch (leaf zone). |
| 75 | +func (p *BranchPrefetcher) prefetchBranches(trieCtx PatriciaContext, hashedKey []byte) { |
| 76 | + for depth := 1; depth <= len(hashedKey) && depth <= p.maxDepth; depth++ { |
| 77 | + prefix := HexNibblesToCompactBytes(hashedKey[:depth]) |
| 78 | + |
| 79 | + // Already in cache — skip DB read |
| 80 | + if p.cache.Contains(prefix) { |
| 81 | + continue |
| 82 | + } |
| 83 | + |
| 84 | + branchData, _, err := trieCtx.Branch(prefix) |
| 85 | + if err != nil || len(branchData) < 4 { |
| 86 | + break // no branch at this depth, stop walking |
| 87 | + } |
| 88 | + |
| 89 | + p.cache.Put(prefix, branchData) |
| 90 | + p.prefetched.Add(1) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// Submit enqueues a hashed key for branch prefetching. Non-blocking; drops if full. |
| 95 | +func (p *BranchPrefetcher) Submit(hashedKey []byte) { |
| 96 | + if !p.started.Load() || p.closed.Load() { |
| 97 | + return |
| 98 | + } |
| 99 | + // Make a copy since caller may reuse the buffer |
| 100 | + key := make([]byte, len(hashedKey)) |
| 101 | + copy(key, hashedKey) |
| 102 | + select { |
| 103 | + case p.work <- key: |
| 104 | + default: // drop if channel full — prefetching is best-effort |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +// Prefetched returns the number of branch nodes prefetched into cache. |
| 109 | +func (p *BranchPrefetcher) Prefetched() uint64 { |
| 110 | + return p.prefetched.Load() |
| 111 | +} |
| 112 | + |
| 113 | +// Stop drains pending work, waits for workers to finish, and releases resources. |
| 114 | +func (p *BranchPrefetcher) Stop() { |
| 115 | + if p.closed.Swap(true) { |
| 116 | + return |
| 117 | + } |
| 118 | + if p.work != nil { |
| 119 | + close(p.work) |
| 120 | + } |
| 121 | + if p.g != nil { |
| 122 | + p.g.Wait() |
| 123 | + } |
| 124 | + p.cancel() |
| 125 | +} |
0 commit comments