Zero-allocation optimization for sendLine and readLine - #1803
Merged
Conversation
```
BenchmarkUnsafeStringVsRegularString/unsafe.String
BenchmarkUnsafeStringVsRegularString/unsafe.String-10 1000000000 0.3661 ns/op
BenchmarkUnsafeStringVsRegularString/string([]byte)
BenchmarkUnsafeStringVsRegularString/string([]byte)-10 56644741 20.38 ns/op
```
```
func BenchmarkUnsafeStringVsRegularString(b *testing.B) {
data := []byte("benchmark test string with reasonable length for performance testing")
b.Run("unsafe.String", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = unsafe.String(unsafe.SliceData(data), len(data))
}
})
b.Run("string([]byte)", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = string(data)
}
})
}
```
sky333999
reviewed
Aug 1, 2025
duhminick
force-pushed
the
dominic-zero-allocation
branch
from
August 1, 2025 19:06
94a80c9 to
ff2152f
Compare
duhminick
commented
Aug 1, 2025
| } | ||
|
|
||
| func (tail *Tail) ReleaseLine(line *Line) { | ||
| *line = Line{} |
Contributor
Author
There was a problem hiding this comment.
TIL: this won't go to the heap because the compiler is smart enough to not "escape" it
sky333999
previously approved these changes
Aug 1, 2025
May have been causing a race condition
sky333999
approved these changes
Aug 4, 2025
okankoAMZ
approved these changes
Aug 4, 2025
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description of the issue
For each line read by the tailer, there are two extra objects that get allocated: a
Lineandstring. Both of these are quite small and they pile up quickly depending on how many log lines there are in a file. Having a lot of small objects adds pressure to the garbage collector.(do note that this is using a diff between two heaps, the count is much greater)
Description of changes
Update 1:
Removing the unsafe.String usage. It looks like there's a possible race condition when using it. I can't re-create it on my own machine even when running the test 50 times. However, the GH actions unit tests seem to get it pretty often (I retried a few times). My theory after digging is that since we're getting a slice of the bytes using the buffer reader, it may have mutated which makes unsafe.String unreliable to use.
Line object pooling
A sync.Pool is used which will allow for us to re-use
Lineobjects. We get aLineobject and update the fields, then send it over to theLineschannel. The consumer of theLineschannel (tailersrc.go) must put the object back into the sync.Pool once done.Zero-copy for string in readLine
Instead of creating a new string object, we instead use unsafe.String (https://pkg.go.dev/unsafe#String) which returns the underlying bytes as a string. This is considered unsafe as strings are immutable, however we are not modifying the data -- there should be no concern.
After
Notice that sendLine and readLine are no longer at the top. This also means that the lines are being properly released. If they weren't, it would pile up.
License
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
Tests
Requirements
Before commiting your code, please do the following steps.
make fmtandmake fmt-shmake lintIntegration Tests
To run integration tests against this PR, add the
ready for testinglabel.