Skip to content

Commit fe144a0

Browse files
committed
fix(conflict): Charge the signature cache by target capacity
1 parent 361eead commit fe144a0

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

‎submitqueue/extension/conflict/tango/cache.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import (
2020
)
2121

2222
// The budget counts memory: a SignatureTarget is 16 bytes (uint64 plus int32,
23-
// padded), and the fixed overhead covers the entry, key and map bookkeeping.
23+
// padded) and the backing array is charged by capacity, and the fixed overhead
24+
// covers the entry, key and map bookkeeping.
2425
const (
2526
signatureTargetBytes = 16
2627
signatureEntryOverhead = 256
@@ -59,7 +60,7 @@ func newSignatureCache(maxBytes int64) *signatureCache {
5960
func signatureCacheBytes(signature TargetSignature) int64 {
6061
return signatureEntryOverhead +
6162
int64(len(signature.Queue)+len(signature.BatchID)+len(signature.BaseRevision)) +
62-
int64(len(signature.Targets))*signatureTargetBytes
63+
int64(cap(signature.Targets))*signatureTargetBytes
6364
}
6465

6566
// get returns the cached signature and marks it most recently used.

‎submitqueue/extension/conflict/tango/cache_test.go‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,15 @@ func TestSignatureCache_Eviction(t *testing.T) {
102102
cache.put(a)
103103
}
104104
},
105-
wantFound: map[string]bool{"a": true, "b": true, "c": false},
105+
wantFound: map[string]bool{"a": true, "b": true},
106106
},
107107
{
108108
name: "a signature larger than the budget is not cached",
109109
run: func(cache *signatureCache) {
110110
cache.put(a)
111111
cache.put(cacheTestSignature("q", "c", 1000))
112112
},
113-
wantFound: map[string]bool{"a": true, "b": false, "c": false},
113+
wantFound: map[string]bool{"a": true, "c": false},
114114
},
115115
}
116116
for _, tt := range tests {
@@ -127,6 +127,18 @@ func TestSignatureCache_Eviction(t *testing.T) {
127127
}
128128
}
129129

130+
func TestSignatureCache_ChargesSpareCapacity(t *testing.T) {
131+
tight := cacheTestSignature("q", "tight", 10)
132+
roomy := cacheTestSignature("q", "roomy", 10)
133+
roomy.Targets = append(make([]SignatureTarget, 0, 1000), roomy.Targets...)
134+
cache := newSignatureCache(signatureCacheBytes(tight) * 2)
135+
136+
cache.put(roomy)
137+
138+
_, found := cache.get("q", "roomy")
139+
assert.False(t, found)
140+
}
141+
130142
func TestSignatureCache_ZeroBudgetCachesNothing(t *testing.T) {
131143
cache := newSignatureCache(0)
132144

0 commit comments

Comments
 (0)