One less allocation

This commit is contained in:
Lou Knauer 2021-10-11 10:56:38 +02:00
parent 3aae1e80fb
commit fecc33a224

View File

@ -16,7 +16,9 @@ const (
// So that we can reuse allocations // So that we can reuse allocations
var bufferPool sync.Pool = sync.Pool{ var bufferPool sync.Pool = sync.Pool{
New: func() interface{} { New: func() interface{} {
return make([]Float, 0, BUFFER_CAP) return &buffer{
data: make([]Float, 0, BUFFER_CAP),
}
}, },
} }
@ -37,13 +39,12 @@ type buffer struct {
} }
func newBuffer(ts, freq int64) *buffer { func newBuffer(ts, freq int64) *buffer {
return &buffer{ b := bufferPool.Get().(*buffer)
frequency: freq, b.frequency = freq
start: ts, b.start = ts
data: bufferPool.Get().([]Float)[:0], b.prev = nil
prev: nil, b.next = nil
next: nil, return b
}
} }
// If a new buffer was created, the new head is returnd. // If a new buffer was created, the new head is returnd.
@ -137,10 +138,11 @@ func (b *buffer) free(t int64) (int, error) {
} }
n += 1 n += 1
bufferPool.Put(b.data) b.frequency = 0
b.data = nil b.start = 0
b.next = nil b.next = nil
b.prev = nil b.prev = nil
bufferPool.Put(b)
b = prev b = prev
} }
return n, nil return n, nil