Skip to content

Commit a6a23d8

Browse files
committed
part: Only reuse watches hash map when it is small
[ upstream commit d02e729 ] This avoids carrying over a large allocation [txn.watches] from a massive one-off transaction into future transactions. This should mitigate cilium/cilium#44310. Signed-off-by: Jussi Maki <jussi.maki@isovalent.com>
1 parent c7344ec commit a6a23d8

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

part/txn.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,17 +182,26 @@ func (txn *Txn[T]) Commit() *Tree[T] {
182182
return t
183183
}
184184

185+
// watchesReuseThreshold is the threshold at which the [txn.watches] hash
186+
// map is reused for next transaction.
187+
const watchesReuseThreshold = 64
188+
185189
// Notify closes the watch channels of nodes that were
186190
// mutated as part of this transaction. Must be called before
187191
// Tree.Txn() is used again.
188192
func (txn *Txn[T]) Notify() {
189193
for ch := range txn.watches {
190194
close(ch)
191195
}
192-
clear(txn.watches)
193196
if !txn.dirty && len(txn.watches) > 0 {
194197
panic("BUG: watch channels marked but txn not dirty")
195198
}
199+
// Clear or reallocate the watches hash map for the next transaction.
200+
if len(txn.watches) <= watchesReuseThreshold {
201+
clear(txn.watches)
202+
} else {
203+
txn.watches = make(map[chan struct{}]struct{})
204+
}
196205
if txn.dirty && txn.rootWatch != nil {
197206
close(txn.rootWatch)
198207
txn.rootWatch = nil

0 commit comments

Comments
 (0)