Skip to content

Commit bdc76f0

Browse files
committed
fixes
1 parent 705154c commit bdc76f0

File tree

2 files changed

+34
-37
lines changed

2 files changed

+34
-37
lines changed

block/internal/cache/pending_base.go

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,10 @@ func (pb *pendingBase[T]) setLastSubmittedHeight(ctx context.Context, newLastSub
103103
if err != nil {
104104
pb.logger.Error().Err(err).Msg("failed to store height of latest item submitted to DA")
105105
}
106-
107-
// Clear marshalled cache for submitted heights
108-
pb.clearMarshalledCacheUpTo(newLastSubmittedHeight)
106+
// Note: We don't explicitly clear the cache here. Instead, we use lazy invalidation
107+
// in getMarshalledForHeight by checking against lastHeight. This avoids O(N)
108+
// iteration over the cache on every submission. The LRU will naturally evict
109+
// old entries when capacity is reached.
109110
}
110111
}
111112

@@ -128,8 +129,14 @@ func (pb *pendingBase[T]) init() error {
128129
return nil
129130
}
130131

131-
// getMarshalledForHeight returns cached marshalled bytes for a height, or nil if not cached
132+
// getMarshalledForHeight returns cached marshalled bytes for a height, or nil if not cached.
133+
// Uses lazy invalidation: entries at or below lastHeight are considered invalid and ignored.
132134
func (pb *pendingBase[T]) getMarshalledForHeight(height uint64) []byte {
135+
// Lazy invalidation: don't return cached data for already-submitted heights
136+
if height <= pb.lastHeight.Load() {
137+
return nil
138+
}
139+
133140
pb.marshalledCacheMu.RLock()
134141
defer pb.marshalledCacheMu.RUnlock()
135142

@@ -139,25 +146,16 @@ func (pb *pendingBase[T]) getMarshalledForHeight(height uint64) []byte {
139146
return nil
140147
}
141148

142-
// setMarshalledForHeight caches marshalled bytes for a height
149+
// setMarshalledForHeight caches marshalled bytes for a height.
150+
// Does not cache heights that have already been submitted.
143151
func (pb *pendingBase[T]) setMarshalledForHeight(height uint64, marshalled []byte) {
144-
pb.marshalledCacheMu.Lock()
145-
defer pb.marshalledCacheMu.Unlock()
146-
147-
pb.marshalledCache.Add(height, marshalled)
148-
}
152+
// Don't cache already-submitted heights
153+
if height <= pb.lastHeight.Load() {
154+
return
155+
}
149156

150-
// clearMarshalledCacheUpTo removes cached marshalled bytes up to and including the given height.
151-
// With LRU cache, we iterate through keys and remove those <= height.
152-
func (pb *pendingBase[T]) clearMarshalledCacheUpTo(height uint64) {
153157
pb.marshalledCacheMu.Lock()
154158
defer pb.marshalledCacheMu.Unlock()
155159

156-
// Get all keys and remove those that are <= height
157-
keys := pb.marshalledCache.Keys()
158-
for _, h := range keys {
159-
if h <= height {
160-
pb.marshalledCache.Remove(h)
161-
}
162-
}
160+
pb.marshalledCache.Add(height, marshalled)
163161
}

block/internal/submitting/da_submitter.go

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"runtime"
99
"sync"
10+
"sync/atomic"
1011
"time"
1112

1213
lru "github.com/hashicorp/golang-lru/v2"
@@ -118,6 +119,10 @@ type DASubmitter struct {
118119
envelopeCache *lru.Cache[uint64, []byte]
119120
envelopeCacheMu sync.RWMutex
120121

122+
// lastSubmittedHeight tracks the last successfully submitted height for lazy cache invalidation.
123+
// This avoids O(N) iteration over the cache on every submission.
124+
lastSubmittedHeight atomic.Uint64
125+
121126
// signingWorkers is the number of parallel workers for signing
122127
signingWorkers int
123128
}
@@ -223,8 +228,8 @@ func (s *DASubmitter) SubmitHeaders(ctx context.Context, headers []*types.Signed
223228
if l := len(submitted); l > 0 {
224229
lastHeight := submitted[l-1].Height()
225230
cache.SetLastSubmittedHeaderHeight(ctx, lastHeight)
226-
// Clear envelope cache for successfully submitted heights
227-
s.clearEnvelopeCacheUpTo(lastHeight)
231+
// Update last submitted height for lazy cache invalidation (O(1) instead of O(N))
232+
s.lastSubmittedHeight.Store(lastHeight)
228233
}
229234
},
230235
"header",
@@ -361,10 +366,15 @@ func (s *DASubmitter) signAndCacheEnvelope(header *types.SignedHeader, marshalle
361366
}
362367

363368
// getCachedEnvelope retrieves a cached envelope for the given height.
369+
// Uses lazy invalidation: entries at or below lastSubmittedHeight are considered invalid.
364370
func (s *DASubmitter) getCachedEnvelope(height uint64) []byte {
365371
if s.envelopeCache == nil {
366372
return nil
367373
}
374+
// Lazy invalidation: don't return cached data for already-submitted heights
375+
if height <= s.lastSubmittedHeight.Load() {
376+
return nil
377+
}
368378
s.envelopeCacheMu.RLock()
369379
defer s.envelopeCacheMu.RUnlock()
370380

@@ -375,30 +385,19 @@ func (s *DASubmitter) getCachedEnvelope(height uint64) []byte {
375385
}
376386

377387
// setCachedEnvelope stores an envelope in the cache.
388+
// Does not cache heights that have already been submitted.
378389
func (s *DASubmitter) setCachedEnvelope(height uint64, envelope []byte) {
379390
if s.envelopeCache == nil {
380391
return
381392
}
382-
s.envelopeCacheMu.Lock()
383-
defer s.envelopeCacheMu.Unlock()
384-
385-
s.envelopeCache.Add(height, envelope)
386-
}
387-
388-
// clearEnvelopeCacheUpTo removes cached envelopes up to and including the given height.
389-
func (s *DASubmitter) clearEnvelopeCacheUpTo(height uint64) {
390-
if s.envelopeCache == nil {
393+
// Don't cache already-submitted heights
394+
if height <= s.lastSubmittedHeight.Load() {
391395
return
392396
}
393397
s.envelopeCacheMu.Lock()
394398
defer s.envelopeCacheMu.Unlock()
395399

396-
keys := s.envelopeCache.Keys()
397-
for _, h := range keys {
398-
if h <= height {
399-
s.envelopeCache.Remove(h)
400-
}
401-
}
400+
s.envelopeCache.Add(height, envelope)
402401
}
403402

404403
// SubmitData submits pending data to DA layer

0 commit comments

Comments
 (0)