-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
proposal: bytes: add (*bytes.Buffer).SetBytes method #67004
Comments
Why not just reset? https://pkg.go.dev/bytes#Buffer.Reset |
|
Reset truncates the existing buffer. The use case here is that I have a byte slice already and I have a bytes.Buffer object and I'd like to create an io.Reader to read those bytes without allocating. A legitimate claim is that the bytes.Reader API should work, because it indeed allows for resetting with a new slice. Where that turns out to not work is that bytes.Reader doesn't expose the underlying byte slice (it doesn't have a Bytes method). |
Cool! I'll close this and hope that some future wayward soul may discover this trick. |
|
I still wonder if As prior precedence, we added the |
Anybody want to add a test to make sure it keeps working? It's not like it's documented. |
I can add a test. I'm about to use this trick. |
Change https://go.dev/cl/581297 mentions this issue: |
Re-use a pre-allocated bytes.Buffer struct and shallow the copy the result of bytes.NewBuffer into it to avoid allocating the struct. Note that we're only reusing the bytes.Buffer struct itself and not the underling []byte temporarily stored within it. Updates #cleanup Updates tailscale/corp#18514 Updates golang/go#67004 Signed-off-by: Joe Tsai <joetsai@digital-static.net>
At present, there is no API to reset the underlying []byte of an existing Buffer struct, except to shallow copy the entire Buffer struct. Updates #67004 Change-Id: I08998f7a95ae5bde0897d86247d47f23cd784583 Reviewed-on: https://go-review.googlesource.com/c/go/+/581297 Auto-Submit: Joseph Tsai <joetsai@digital-static.net> Reviewed-by: Joedian Reid <joedian@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Keith Randall <khr@google.com> Run-TryBot: Joseph Tsai <joetsai@digital-static.net> Reviewed-by: Keith Randall <khr@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
@dsnet kindly please see @thanm's notice on the test CL https://go-review.googlesource.com/c/go/+/581297/2#message-b5102998fb0dc108e0ad11e3d249c9017581ab89 about the test failure https://ci.chromium.org/ui/p/golang/builders/ci/gotip-linux-amd64-noopt/b8749654087388264177/test-results on the gotip-linux-amd64-noopt-test_only builder. |
I was about to fix this and it appear it's already been fixed: https://go-review.googlesource.com/c/go/+/581915 My apologies for the failure. |
Re-use a pre-allocated bytes.Buffer struct and shallow the copy the result of bytes.NewBuffer into it to avoid allocating the struct. Note that we're only reusing the bytes.Buffer struct itself and not the underling []byte temporarily stored within it. Updates #cleanup Updates tailscale/corp#18514 Updates golang/go#67004 Signed-off-by: Joe Tsai <joetsai@digital-static.net>
Re-use a pre-allocated bytes.Buffer struct and shallow the copy the result of bytes.NewBuffer into it to avoid allocating the struct. Note that we're only reusing the bytes.Buffer struct itself and not the underling []byte temporarily stored within it. Updates #cleanup Updates tailscale/corp#18514 Updates golang/go#67004 Signed-off-by: Joe Tsai <joetsai@digital-static.net>
Proposal Details
Motivation
It should be possible to reset the byte slice underneath a
bytes.Buffer
without allocating a new object. Today the only method to initialize abytes.Buffer
with an explicit byte slice isbytes.NewBuffer
.The io.Reader interface is widely used and makes decoding/consuming byte streams uniform. In some cases, it is valuable to be able to optimize the special case of
*bytes.Buffer
as opposed to requiring a larger API surface area (see #63397 (comment)). Processing bytes can at times be the hottest path of a go program. Having a simple method to reset the*bytes.Buffer
object with a new byte slice can enable these uniform APIs to be utilized in high performance contexts.Proposal
Introduce a new method
(*bytes.Buffer).SetBytes
as defined below:This method has symmetry with
(*bytes.Buffer).Bytes()
which provides access to the underlying unread portion of the buffer.The text was updated successfully, but these errors were encountered: