Start new slice on empty queue.

This commit is contained in:
Peter Štibraný 2025-10-02 12:20:29 +02:00
parent 4b2bb46930
commit 6f0e365823
1 changed files with 5 additions and 0 deletions

View File

@ -108,6 +108,11 @@ func (q *Queue[T]) Next(ctx context.Context) (T, error) {
first := q.elements[0]
q.elements = q.elements[1:]
if len(q.elements) == 0 {
// Start a new slice next time to avoid keeping reference to possibly large underlying array.
// This only works if the queue gets empty eventually.
q.elements = nil
}
return first, nil
}