-
Notifications
You must be signed in to change notification settings - Fork 222
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
Reduce bytes to strings allocs in decoder #367
Conversation
3418e0c
to
f33f35b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Generally I'm fine with this, though I wouldn't want to bump the minimum version to Go 1.20 just for this. But using build tags it should be possible to wrap this functionality such that it's only used when using Go 1.20 - just put the actual stringConverter
in a file with //+build go1.20
and a stub implementation that just uses a normal string conversion in a file with //+build !go1.20
.
ef5578c
to
f15ebda
Compare
That's a good idea to leverage build tags. Maybe I am doing something wrong, but for some reason go1.20 tag clashes with go 1.12 version defined in the go.mod.
|
We can use the old way of converting strings in all the Go versions instead of the build tags. func toString(b []byte) string {
var s string
h := (*reflect.StringHeader)(unsafe.Pointer(&s))
h.Data = uintptr(unsafe.Pointer(&b[0]))
h.Len = len(b)
return s
} |
646ef96
to
b4ff52c
Compare
Instead of allocating when converting small byte slices to strings in the decoder, we can write to a 4kb buffer and use unsafe.String which refers to that buffer. This approach was successfully tested in https://github.com/marselester/systemd which is used by https://github.com/parca-dev/parca-agent. Note, BenchmarkUnixFDs showed 8 allocs reduction. With larger dbus messages, the savings should be significant.
b4ff52c
to
ab0b98d
Compare
Thanks! |
Instead of allocating when converting small byte slices to strings in the decoder, we can write to a 4kb buffer and use
unsafe.String
which refers to that buffer. This approach was successfully tested in https://github.com/marselester/systemd which is used by https://github.com/parca-dev/parca-agent.Note,
BenchmarkUnixFDs
showed 8 allocs reduction. With larger dbus messages, the savings should be significant.The caveat is that we should bump Go to 1.20, I am not sure if that's something you're comfortable with.