From e23cec23ef54ade94be718eb5d91e438b4583482 Mon Sep 17 00:00:00 2001 From: Abolfazl Date: Sun, 22 Mar 2026 17:07:11 +0330 Subject: [PATCH] Implement array compaction for oversized queue Added logic to compact the backing array when it becomes significantly oversized to prevent unbounded memory growth from repeated push/pop cycles. --- internal/mlq/mlq.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/internal/mlq/mlq.go b/internal/mlq/mlq.go index e2def65..e7c2732 100644 --- a/internal/mlq/mlq.go +++ b/internal/mlq/mlq.go @@ -95,6 +95,14 @@ func (m *MultiLevelQueue[T]) popLocked(keyExtractor func(T) uint32) (T, int, boo q.Items[0] = zero q.Items = q.Items[1:] + // Compact the backing array when it becomes significantly oversized + // to prevent unbounded memory growth from repeated push/pop cycles. + if cap(q.Items) > 64 && len(q.Items) < cap(q.Items)/4 { + compact := make([]T, len(q.Items)) + copy(compact, q.Items) + q.Items = compact + } + // Update census and bitmask if keyExtractor != nil { delete(m.census, keyExtractor(item)) @@ -193,6 +201,13 @@ func (m *MultiLevelQueue[T]) PopIf(priority int, predicate func(T) bool, keyExtr q.Items[0] = zero // Memory safety q.Items = q.Items[1:] + // Compact the backing array when it becomes significantly oversized + if cap(q.Items) > 64 && len(q.Items) < cap(q.Items)/4 { + compact := make([]T, len(q.Items)) + copy(compact, q.Items) + q.Items = compact + } + if keyExtractor != nil { delete(m.census, keyExtractor(item)) }