Skip to content

Commit

Permalink
(concurrentbatchprocessor): Allow re-use of the pending slice (#258)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jmacd authored Oct 1, 2024
1 parent 58c3bdf commit 7cceec5
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions collector/processor/concurrentbatchprocessor/batch_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
}

Expand Down

0 comments on commit 7cceec5

Please sign in to comment.