From 7ec5c5b144866c392e3676778041a2ae6aa9d360 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sun, 5 Dec 2021 10:37:20 +0000 Subject: buffer line.Line for performance --- internal/io/line/line.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'internal/io/line') diff --git a/internal/io/line/line.go b/internal/io/line/line.go index d306c88..c168274 100644 --- a/internal/io/line/line.go +++ b/internal/io/line/line.go @@ -3,8 +3,17 @@ package line import ( "bytes" "fmt" + "sync" ) +// lineBuffer is there to optimize memory allocations. DTail otherwise allocates +// a lot of memory while reading logs. +var lineBuffer = sync.Pool{ + New: func() interface{} { + return &Line{} + }, +} + // Line represents a read log line. type Line struct { // The content of the log line. @@ -23,6 +32,22 @@ type Line struct { SourceID string } +func New(content *bytes.Buffer, count uint64, transmittedPerc int, sourceID string) *Line { + l := lineBuffer.Get().(*Line) + l.Content = content + l.Count = count + l.TransmittedPerc = transmittedPerc + l.SourceID = sourceID + return l +} + +// Null returns a new line with all members initialized to their null value. +func Null() *Line { + l := lineBuffer.Get().(*Line) + l.NullValues() + return l +} + // Return a human readable representation of the followed line. func (l Line) String() string { return fmt.Sprintf("Line(Content:%s,TransmittedPerc:%v,Count:%v,SourceID:%s)", @@ -31,3 +56,19 @@ func (l Line) String() string { l.Count, l.SourceID) } + +// Recycle the line. Once done, don't reuse this instance!!! +func (l *Line) Recycle() { + // No explicit reset required, as NewLine overrides all elements + // already takes care of it. + //l.Reset() + lineBuffer.Put(l) +} + +// NullValues nulls all line struct members to their default state. +func (l *Line) NullValues() { + l.Content = nil + l.Count = 0 + l.TransmittedPerc = 0 + l.SourceID = "" +} -- cgit v1.2.3