From fecc33a2242ad79ff0290be530c40167ae139d40 Mon Sep 17 00:00:00 2001 From: Lou Knauer Date: Mon, 11 Oct 2021 10:56:38 +0200 Subject: [PATCH] One less allocation --- memstore.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/memstore.go b/memstore.go index d9b81f2..a6e4a97 100644 --- a/memstore.go +++ b/memstore.go @@ -16,7 +16,9 @@ const ( // So that we can reuse allocations var bufferPool sync.Pool = sync.Pool{ 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 { - return &buffer{ - frequency: freq, - start: ts, - data: bufferPool.Get().([]Float)[:0], - prev: nil, - next: nil, - } + b := bufferPool.Get().(*buffer) + b.frequency = freq + b.start = ts + b.prev = nil + b.next = nil + return b } // 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 - bufferPool.Put(b.data) - b.data = nil + b.frequency = 0 + b.start = 0 b.next = nil b.prev = nil + bufferPool.Put(b) b = prev } return n, nil