From 7cceec5e8ca0c0a0ee8be4806ced19e49e96778b Mon Sep 17 00:00:00 2001 From: Joshua MacDonald Date: Tue, 1 Oct 2024 14:06:07 -0700 Subject: [PATCH] (concurrentbatchprocessor): Allow re-use of the pending slice (#258) This avoids a pitfall in which we repeatedly re-slice `b.pending` to `b.pending[1:]`, which causes the slice to be re-allocated instead of re-used. To re-use the slice, shift its contents by one, zero the final element, and shorten it by one. --- .../concurrentbatchprocessor/batch_processor.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/collector/processor/concurrentbatchprocessor/batch_processor.go b/collector/processor/concurrentbatchprocessor/batch_processor.go index f166d61b..0aa2427f 100644 --- a/collector/processor/concurrentbatchprocessor/batch_processor.go +++ b/collector/processor/concurrentbatchprocessor/batch_processor.go @@ -375,12 +375,11 @@ func (b *shard) sendItems(trigger trigger) { ctx: b.pending[0].parentCtx, }) - // complete response sent so b.pending[0] can be popped from queue. - if len(b.pending) > 1 { - b.pending = b.pending[1:] - } else { - b.pending = []pendingItem{} - } + // Shift the pending array, to allow it to be re-used + // instead of re-allocated. + copy(b.pending[0:len(b.pending)-1], b.pending[1:]) + b.pending[len(b.pending)-1] = pendingItem{} + b.pending = b.pending[:len(b.pending)-1] } }