1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
// Package ringbuffer provides a generic adaptive ring buffer implementation.
package ringbuffer
import (
"fmt"
)
// RingBuffer is a generic circular buffer that can grow when full and compact
// when oversized. It tracks size watermarks to determine when compaction is
// appropriate.
type RingBuffer[T any] struct {
buf []T
head int // index of the first element
tail int // index of the next write position
count int // number of elements in the buffer
// Watermark tracking for compaction decisions using max-in-window
maxInWindow int // peak count in current window
windowCounter int // operations since window reset
idleTicks int // consecutive operations at low utilization
}
const (
initialSize = 16
minSize = 16
windowSize = 256 // Reset max tracking every N operations
idleThreshold = 200 // Operations at low utilization before compaction
lowUtilPct = 0.25 // Utilization threshold for considering buffer idle
peakHeadroom = 1.5 // Headroom multiplier for compaction target
compactRatio = 2 // Only compact if capacity > target * compactRatio
)
// New creates a new RingBuffer with default settings.
// The buffer is initially nil and will be allocated on first push.
func New[T any]() *RingBuffer[T] {
return &RingBuffer[T]{}
}
// NewWithSize creates a new RingBuffer with a specific initial size.
// The buffer is initially nil and will be allocated on first push.
func NewWithSize[T any](size int) *RingBuffer[T] {
if size < 1 {
size = initialSize
}
return &RingBuffer[T]{}
}
// Push adds an element to the ring buffer. If the buffer is full, it will grow.
func (rb *RingBuffer[T]) Push(item T) {
// Lazy allocate buffer on first push
if rb.buf == nil {
rb.buf = make([]T, initialSize)
} else if rb.count == len(rb.buf) {
rb.grow()
}
rb.buf[rb.tail] = item
rb.tail = (rb.tail + 1) % len(rb.buf)
rb.count++
rb.updateWatermark()
}
// Pop removes and returns the oldest element from the ring buffer.
// Returns the zero value and false if the buffer is empty.
func (rb *RingBuffer[T]) Pop() (T, bool) {
if rb.count == 0 {
var zero T
// Update watermark even on empty pop to track idle time
rb.updateWatermark()
rb.considerCompaction()
return zero, false
}
item := rb.buf[rb.head]
var zero T
rb.buf[rb.head] = zero // clear reference for GC
rb.head = (rb.head + 1) % len(rb.buf)
rb.count--
rb.updateWatermark()
rb.considerCompaction()
return item, true
}
// Peek returns the oldest element without removing it.
// Returns the zero value and false if the buffer is empty.
func (rb *RingBuffer[T]) Peek() (T, bool) {
if rb.count == 0 {
var zero T
return zero, false
}
return rb.buf[rb.head], true
}
// Len returns the number of elements in the buffer.
func (rb *RingBuffer[T]) Len() int {
return rb.count
}
// Cap returns the current capacity of the underlying buffer.
func (rb *RingBuffer[T]) Cap() int {
if rb.buf == nil {
return 0
}
return len(rb.buf)
}
// IsEmpty returns true if the buffer contains no elements.
func (rb *RingBuffer[T]) IsEmpty() bool {
return rb.count == 0
}
// IsFull returns true if the buffer is at capacity.
func (rb *RingBuffer[T]) IsFull() bool {
return rb.count == len(rb.buf)
}
// Clear removes all elements from the buffer and resets watermarks.
func (rb *RingBuffer[T]) Clear() {
// Release buffer to save memory
rb.buf = nil
rb.head = 0
rb.tail = 0
rb.count = 0
rb.resetWatermarks()
}
// grow doubles the capacity of the ring buffer.
func (rb *RingBuffer[T]) grow() {
newSize := len(rb.buf) * 2
if newSize == 0 {
newSize = initialSize
}
rb.resize(newSize)
}
// resize changes the capacity of the ring buffer.
func (rb *RingBuffer[T]) resize(newSize int) {
if newSize < rb.count {
// Can't resize smaller than current content
newSize = rb.count
}
newBuf := make([]T, newSize)
// Copy elements in order from head to tail
if rb.count > 0 {
if rb.head < rb.tail {
copy(newBuf, rb.buf[rb.head:rb.tail])
} else {
// Wrapped around
n := copy(newBuf, rb.buf[rb.head:])
copy(newBuf[n:], rb.buf[:rb.tail])
}
}
rb.buf = newBuf
rb.head = 0
rb.tail = rb.count
}
// updateWatermark tracks the peak size within a sliding window.
func (rb *RingBuffer[T]) updateWatermark() {
// Track maximum in this window
if rb.count > rb.maxInWindow {
rb.maxInWindow = rb.count
}
// Reset window periodically
rb.windowCounter++
if rb.windowCounter >= windowSize {
rb.maxInWindow = rb.count
rb.windowCounter = 0
}
// Track consecutive operations at low utilization
if rb.buf == nil {
rb.idleTicks++
} else if rb.count < (len(rb.buf) >> 2) { // count < capacity/4
rb.idleTicks++
} else {
rb.idleTicks = 0
}
}
// considerCompaction checks if the buffer should be compacted.
func (rb *RingBuffer[T]) considerCompaction() {
// If empty and idle for a while, free the buffer completely
if rb.count == 0 && rb.idleTicks >= idleThreshold {
rb.buf = nil
rb.head = 0
rb.tail = 0
rb.idleTicks = 0
return
}
// Only consider compaction if we're significantly oversized
if rb.buf == nil || len(rb.buf) <= minSize {
return
}
// If buffer has been underutilized for a while, compact it
if rb.idleTicks >= idleThreshold {
// Target size based on peak in window + headroom, rounded up to power of 2
targetSize := (rb.maxInWindow * 3) >> 1 // maxInWindow * 1.5
if targetSize < minSize {
targetSize = minSize
}
// Round up to next power of 2 for efficient allocation
targetSize = nextPowerOf2(targetSize)
// Only compact if we can save significant space
if len(rb.buf) > targetSize*compactRatio {
rb.resize(targetSize)
rb.idleTicks = 0
rb.maxInWindow = rb.count
rb.windowCounter = 0
}
}
}
// nextPowerOf2 returns the next power of 2 greater than or equal to n.
func nextPowerOf2(n int) int {
if n <= 0 {
return 1
}
n--
n |= n >> 1
n |= n >> 2
n |= n >> 4
n |= n >> 8
n |= n >> 16
n |= n >> 32
n++
return n
}
// resetWatermarks clears watermark tracking state.
func (rb *RingBuffer[T]) resetWatermarks() {
rb.maxInWindow = 0
rb.windowCounter = 0
rb.idleTicks = 0
}
// Stats returns statistics about the ring buffer's behavior.
func (rb *RingBuffer[T]) Stats() Stats {
var utilization float64
cap := 0
if rb.buf != nil {
cap = len(rb.buf)
utilization = float64(rb.count) / float64(cap)
}
return Stats{
Len: rb.count,
Cap: cap,
PeakSize: rb.maxInWindow,
IdleTicks: rb.idleTicks,
Utilization: utilization,
}
}
// Stats contains statistics about ring buffer usage.
type Stats struct {
Len int // current number of elements
Cap int // current capacity
PeakSize int // peak size in current window
IdleTicks int // consecutive low-utilization operations
Utilization float64 // current utilization (len/cap)
}
func (s Stats) String() string {
return fmt.Sprintf("RingBuffer{len=%d, cap=%d, peak=%d, util=%.2f%%, idle=%d}",
s.Len, s.Cap, s.PeakSize, s.Utilization*100, s.IdleTicks)
}
|