diff options
| author | Paul Buetow <pbuetow@mimecast.com> | 2021-12-05 10:37:20 +0000 |
|---|---|---|
| committer | Paul Buetow <pbuetow@mimecast.com> | 2021-12-05 10:37:20 +0000 |
| commit | 7ec5c5b144866c392e3676778041a2ae6aa9d360 (patch) | |
| tree | 55538e624912e24b94d6faa0ae5f0060b824d1bf /internal/io/line | |
| parent | 6c12fc4b33049111ad6ddc3f62bf979f843fad73 (diff) | |
buffer line.Line for performance
Diffstat (limited to 'internal/io/line')
| -rw-r--r-- | internal/io/line/line.go | 41 |
1 files changed, 41 insertions, 0 deletions
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 = "" +} |
