Skip to content

Commit dfaded5

Browse files
committed
tapdb: add inner proof size cache to type cachedProofs
1 parent fa5f91b commit dfaded5

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

tapdb/multiverse_cache.go

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,22 @@ func (c MultiverseCacheConfig) maxProofCacheSizeBytes() (uint64, error) {
8888
}
8989

9090
// cachedProofs is a list of cached proof leaves.
91-
type cachedProofs []*universe.Proof
91+
type cachedProofs struct {
92+
// proofs is the list of cached proofs.
93+
proofs []*universe.Proof
94+
95+
// proofSizeCache is a map of proof universe key to the size of the
96+
// proof in bytes.
97+
proofSizeCache map[[32]byte]uint64
98+
}
99+
100+
// newCachedProofs creates a new cached proofs list.
101+
func newCachedProofs(proofs []*universe.Proof) cachedProofs {
102+
return cachedProofs{
103+
proofs: proofs,
104+
proofSizeCache: make(map[[32]byte]uint64, len(proofs)),
105+
}
106+
}
92107

93108
// Size returns the total byte size of all cached proofs.
94109
func (c *cachedProofs) Size() (uint64, error) {
@@ -97,12 +112,22 @@ func (c *cachedProofs) Size() (uint64, error) {
97112
}
98113

99114
totalBytes := uint64(0)
100-
for _, proof := range *c {
115+
for _, proof := range c.proofs {
101116
if proof == nil {
102117
continue
103118
}
104119

105-
totalBytes += proof.LowerBoundByteSize()
120+
// Look up the cached size for this proof. If absent, compute
121+
// the lower-bound size and cache it under the proof’s universe
122+
// key.
123+
universeKey := proof.LeafKey.UniverseKey()
124+
size, ok := c.proofSizeCache[universeKey]
125+
if !ok {
126+
size = proof.LowerBoundByteSize()
127+
c.proofSizeCache[universeKey] = size
128+
}
129+
130+
totalBytes += size
106131
}
107132

108133
return totalBytes, nil
@@ -181,7 +206,7 @@ func (p *universeProofCache) fetchProof(id universe.Identifier,
181206
proofFromCache, err := p.cache.Get(uniProofKey)
182207
if err == nil {
183208
p.Hit()
184-
return *proofFromCache
209+
return proofFromCache.proofs
185210
}
186211

187212
p.Miss()
@@ -198,7 +223,7 @@ func (p *universeProofCache) insertProofs(id universe.Identifier,
198223
log.Debugf("Storing proof(s) in cache (universe_id=%v, leaf_key=%v, "+
199224
"count=%d)", id.StringForLog(), leafKey, len(proofs))
200225

201-
proofVal := cachedProofs(proofs)
226+
proofVal := newCachedProofs(proofs)
202227
if _, err := p.cache.Put(uniProofKey, &proofVal); err != nil {
203228
log.Errorf("Unable to insert proof into universe proof "+
204229
"cache: %v", err)

tapdb/multiverse_cache_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ func newTestUniverseProofs(t *testing.T, count int) []*universe.Proof {
411411
func proofCacheEntrySize(t *testing.T, proofs []*universe.Proof) uint64 {
412412
t.Helper()
413413

414-
cached := cachedProofs(proofs)
414+
cached := newCachedProofs(proofs)
415415
size, err := (&cached).Size()
416416
require.NoError(t, err)
417417
require.NotZero(t, size)

0 commit comments

Comments
 (0)