Refactor delta addition/clearing

This commit is contained in:
Michael Aspinwall
2026-01-14 12:44:38 -05:00
parent d719bc771f
commit 2dad39751d

View File

@@ -282,6 +282,11 @@ func (f *RealFIFO) PopBatch(process ProcessBatchFunc) error {
isInInitialList := !f.hasSynced_locked()
unique := sets.NewString()
deltas := make([]Delta, 0, min(len(f.items), f.batchSize))
moveDeltaToProcessList := func(i int) {
deltas = append(deltas, f.items[i])
// The underlying array still exists and references this object, so the object will not be garbage collected unless we zero the reference.
f.items[i] = Delta{}
}
// only bundle unique items into a batch
for i := 0; i < f.batchSize && i < len(f.items); i++ {
if f.initialPopulationCount > 0 && i >= f.initialPopulationCount {
@@ -295,18 +300,14 @@ func (f *RealFIFO) PopBatch(process ProcessBatchFunc) error {
// still pop the broken item out of queue to be compatible with the non-batch behavior it should be safe
// when 1st element is broken, however for Nth broken element, there's possible risk that broken item
// still can be processed and broke the uniqueness of the batch unexpectedly.
deltas = append(deltas, item)
// The underlying array still exists and references this object, so the object will not be garbage collected unless we zero the reference.
f.items[i] = Delta{}
moveDeltaToProcessList(i)
break
}
if unique.Has(id) {
break
}
unique.Insert(id)
deltas = append(deltas, item)
// The underlying array still exists and references this object, so the object will not be garbage collected unless we zero the reference.
f.items[i] = Delta{}
moveDeltaToProcessList(i)
}
if f.initialPopulationCount > 0 {
f.initialPopulationCount -= len(deltas)