-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
bufio: add Writer.AvailableBuffer #47527
Comments
This proposal has been added to the active column of the proposals project |
This seems reasonable. Does anyone object to this? |
Based on the discussion above, this proposal seems like a likely accept. |
No change in consensus, so accepted. 🎉 |
Change https://golang.org/cl/345569 mentions this issue: |
This adds a new Writer.AvailableBuffer method that returns an empty buffer with a possibly non-empty capacity for use with append-like APIs. The typical usage pattern is something like: b := bw.AvailableBuffer() b = appendValue(b, v) bw.Write(b) It allows logic combining append-like APIs with bufio.Writer to avoid needing to allocate and manage buffers themselves and allows the append-like APIs to directly write into the buffer for a bufio.Writer. Fixes golang#47527 Change-Id: I9cd169f3f8e8c7cd40818caf3daf1944c826fc66 Reviewed-on: https://go-review.googlesource.com/c/go/+/345569 Trust: Joe Tsai <[email protected]> Run-TryBot: Joe Tsai <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> (cherry picked from golang.org/cl/345569)
This adds a new Writer.AvailableBuffer method that returns an empty buffer with a possibly non-empty capacity for use with append-like APIs. The typical usage pattern is something like: b := bw.AvailableBuffer() b = appendValue(b, v) bw.Write(b) It allows logic combining append-like APIs with bufio.Writer to avoid needing to allocate and manage buffers themselves and allows the append-like APIs to directly write into the buffer for a bufio.Writer. Fixes golang#47527 Change-Id: I9cd169f3f8e8c7cd40818caf3daf1944c826fc66 Reviewed-on: https://go-review.googlesource.com/c/go/+/345569 Trust: Joe Tsai <[email protected]> Run-TryBot: Joe Tsai <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> (cherry picked from golang.org/cl/345569)
There are many APIs in Go that append to a
[]byte
and return a[]byte
.Unfortunately, these APIs operate poorly with
bufio.Writer
since that type only accepts[]byte
as an input (via theWrite
method) and never provides a[]byte
. This requires logic trying to combine append-like functionality withbufio.Writer
to be responsible for allocating a buffer, whenbufio.Writer
already has a buffer.I propose the addition of a new method that provides access to the underlying
[]byte
:Example usage:
In the common cases where
bw.Available()
is large enough,strconv.AppendUint
appends to the buffer without allocating. When the buffer is passed toWrite
, the underlying call tocopy
returns immediately without actually copying since the source and destination are identical. This saves us both an allocation and a copy.In the rare case where it allocates, the subsequent
Write
call still works as expected, but needs to copy.The detriment of this API is that it exposes the internal buffer of
bufio.Writer
when it was previously impossible to access it. However, the other types in the package (i.e.,Reader
andScanner
) already provide unsafe access to the underlying buffer via theReader.Peek
,Reader.ReadSlice
, andScanner.Bytes
methods.The text was updated successfully, but these errors were encountered: