-
Notifications
You must be signed in to change notification settings - Fork 263
Zero-allocation optimization for sendLine and readLine #1803
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
dc8dfbf
Zero-allocation optimization for sendLine and readLine
duhminick ff2152f
Zero out Line object before putting back into pool
duhminick dc06313
Merge branch 'main' into dominic-zero-allocation
duhminick ecc9fae
644 to 600 - not sure how this popped up again
duhminick 03b0e7c
Move back ReleaseLine
duhminick efd80d9
Remove unsafe.String
duhminick 2f89d6a
Follow pattern for writing file in test
duhminick File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ import ( | |
| "sync" | ||
| "sync/atomic" | ||
| "time" | ||
| "unsafe" | ||
|
|
||
| "github.com/influxdata/telegraf" | ||
| "github.com/influxdata/telegraf/models" | ||
|
|
@@ -37,11 +38,6 @@ type Line struct { | |
| Offset int64 // offset of current reader | ||
| } | ||
|
|
||
| // NewLine returns a Line with present time. | ||
| func NewLine(text string, offset int64) *Line { | ||
| return &Line{text, time.Now(), nil, offset} | ||
| } | ||
|
|
||
| // SeekInfo represents arguments to `os.Seek` | ||
| type SeekInfo struct { | ||
| Offset int64 | ||
|
|
@@ -91,6 +87,8 @@ type Tail struct { | |
| lk sync.Mutex | ||
|
|
||
| FileDeletedCh chan struct{} | ||
|
|
||
| linePool sync.Pool | ||
| } | ||
|
|
||
| // TailFile begins tailing the file. Output stream is made available | ||
|
|
@@ -107,6 +105,9 @@ func TailFile(filename string, config Config) (*Tail, error) { | |
| Lines: make(chan *Line), | ||
| Config: config, | ||
| FileDeletedCh: make(chan struct{}), | ||
| linePool: sync.Pool{ | ||
| New: func() any { return &Line{} }, | ||
| }, | ||
| } | ||
|
|
||
| // when Logger was not specified in config, create new one | ||
|
|
@@ -260,7 +261,7 @@ func (tail *Tail) readLine() (string, error) { | |
| } | ||
| line = line[:len(line)-drop] | ||
| } | ||
| return string(line), err | ||
| return unsafe.String(unsafe.SliceData(line), len(line)), err | ||
| } | ||
|
|
||
| func (tail *Tail) readlineUtf16() (string, error) { | ||
|
|
@@ -418,7 +419,13 @@ func (tail *Tail) tailFileSync() { | |
| // Wait a second before seeking till the end of | ||
| // file when rate limit is reached. | ||
| msg := "Too much log activity; waiting a second before resuming tailing" | ||
| tail.Lines <- &Line{msg, time.Now(), errors.New(msg), tail.curOffset} | ||
| // Warning: Make sure to release line once done! | ||
| lineObject := tail.linePool.Get().(*Line) | ||
| lineObject.Text = msg | ||
| lineObject.Time = time.Now() | ||
| lineObject.Err = errors.New(msg) | ||
| lineObject.Offset = tail.curOffset | ||
| tail.Lines <- lineObject | ||
| select { | ||
| case <-time.After(time.Second): | ||
| case <-tail.Dying(): | ||
|
|
@@ -574,12 +581,18 @@ func (tail *Tail) sendLine(line string, offset int64) bool { | |
|
|
||
| for i, line := range lines { | ||
| // This select is to avoid blockage on the tail.Lines chan | ||
| // Warning: Make sure to release line once done! | ||
| lineObject := tail.linePool.Get().(*Line) | ||
| lineObject.Text = line | ||
| lineObject.Time = now | ||
| lineObject.Err = nil | ||
| lineObject.Offset = offset | ||
| select { | ||
| case tail.Lines <- &Line{line, now, nil, offset}: | ||
| case tail.Lines <- lineObject: | ||
| case <-tail.Dying(): | ||
| if tail.Err() == errStopAtEOF { | ||
| // Try sending, even if it blocks. | ||
| tail.Lines <- &Line{line, now, nil, offset} | ||
| tail.Lines <- lineObject | ||
| } else { | ||
| tail.dropCnt += len(lines) - i | ||
| return true | ||
|
|
@@ -627,6 +640,11 @@ func (tail *Tail) unreadByte() (err error) { | |
| return | ||
| } | ||
|
|
||
| func (tail *Tail) ReleaseLine(line *Line) { | ||
| *line = Line{} | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TIL: this won't go to the heap because the compiler is smart enough to not "escape" it |
||
| tail.linePool.Put(line) | ||
|
duhminick marked this conversation as resolved.
|
||
| } | ||
|
|
||
| // A wrapper of tomb Err() | ||
| func (tail *Tail) UnexpectedError() (err error) { | ||
| err = tail.Err() | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.