|
@@ -101,14 +101,7 @@ func NewHistoryBuffer(size int, window time.Duration) (result *Buffer) {
|
101
|
101
|
}
|
102
|
102
|
|
103
|
103
|
func (hist *Buffer) Initialize(size int, window time.Duration) {
|
104
|
|
- initialSize := size
|
105
|
|
- if window != 0 {
|
106
|
|
- initialSize = initialAutoSize
|
107
|
|
- if size < initialSize {
|
108
|
|
- initialSize = size // min(initialAutoSize, size)
|
109
|
|
- }
|
110
|
|
- }
|
111
|
|
- hist.buffer = make([]Item, initialSize)
|
|
104
|
+ hist.buffer = make([]Item, hist.initialSize(size, window))
|
112
|
105
|
hist.start = -1
|
113
|
106
|
hist.end = -1
|
114
|
107
|
hist.window = window
|
|
@@ -118,6 +111,18 @@ func (hist *Buffer) Initialize(size int, window time.Duration) {
|
118
|
111
|
hist.setEnabled(size)
|
119
|
112
|
}
|
120
|
113
|
|
|
114
|
+// compute the initial size for the buffer, taking into account autoresize
|
|
115
|
+func (hist *Buffer) initialSize(size int, window time.Duration) (result int) {
|
|
116
|
+ result = size
|
|
117
|
+ if window != 0 {
|
|
118
|
+ result = initialAutoSize
|
|
119
|
+ if size < result {
|
|
120
|
+ result = size // min(initialAutoSize, size)
|
|
121
|
+ }
|
|
122
|
+ }
|
|
123
|
+ return
|
|
124
|
+}
|
|
125
|
+
|
121
|
126
|
func (hist *Buffer) setEnabled(size int) {
|
122
|
127
|
var enabled uint32
|
123
|
128
|
if size != 0 {
|
|
@@ -339,12 +344,19 @@ func (list *Buffer) Resize(maximumSize int, window time.Duration) {
|
339
|
344
|
list.maximumSize = maximumSize
|
340
|
345
|
list.window = window
|
341
|
346
|
|
342
|
|
- // if we're not autoresizing, we need to resize now;
|
343
|
|
- // if we are autoresizing, we may need to shrink the buffer down to maximumSize,
|
344
|
|
- // but we don't need to grow it now (we can just grow it on the next Add)
|
|
347
|
+ // three cases where we need to preemptively resize:
|
|
348
|
+ // (1) we are not autoresizing
|
|
349
|
+ // (2) the buffer is currently larger than maximumSize and needs to be shrunk
|
|
350
|
+ // (3) the buffer is currently smaller than the recommended initial size
|
|
351
|
+ // (including the case where it is currently disabled and needs to be enabled)
|
345
|
352
|
// TODO make it possible to shrink the buffer so that it only contains `window`
|
346
|
353
|
if window == 0 || maximumSize < len(list.buffer) {
|
347
|
354
|
list.resize(maximumSize)
|
|
355
|
+ } else {
|
|
356
|
+ initialSize := list.initialSize(maximumSize, window)
|
|
357
|
+ if len(list.buffer) < initialSize {
|
|
358
|
+ list.resize(initialSize)
|
|
359
|
+ }
|
348
|
360
|
}
|
349
|
361
|
}
|
350
|
362
|
|