You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'd like to propose adding a function with the following signature:
// Append the binary representation of data to buf.//// buf may be nil, in which case a new buffer will be allocated. See [Write] on// which data are acceptable.//// Returns the (possibly extended) buffer containing data or an error.funcAppend(buf []byte, orderAppendByteOrder, dataany) ([]byte, error)
This is useful when repeatedly encoding the same kind of value multiple times into a larger buffer and is a natural extension to #50601. A related proposal wants to add similar functions to other packages in encoding: #53693.
Together with #53685 it becomes possible to implement a version of binary.Write that doesn't allocate when using common io.Writer. See my comment for writeBuffer(). Roughly (untested):
I've been playing with the implementation a bit, and I'd like to extend my proposal to cover an equivalent function for Read.
// Decode data from buf according to the given byte order.//// Returns the number of bytes read from the beginning of buf or io.ErrUnexpectedEOF.funcDecode(buf []byte, orderByteOrder, dataany) (int, error)
The rationale is similar to Append: using this new function makes it possible to implement zero-allocation decode for common io.Reader.
I struggled to come up with a good name for the function, what is the inverse of Append? Renaming it to Encode seems clearer to me, and follows existing unexported types. The proposal would become:
I'd like to propose adding a function with the following signature:
This is useful when repeatedly encoding the same kind of value multiple times into a larger buffer and is a natural extension to #50601. A related proposal wants to add similar functions to other packages in
encoding
: #53693.Together with #53685 it becomes possible to implement a version of
binary.Write
that doesn't allocate when using commonio.Writer
. See my comment forwriteBuffer()
. Roughly (untested):If the CLs to avoid escaping in
reflect
APIs lands,Append
would allow encoding with zero allocations.I think it might also allow encoding into stack allocated slices, provided the compiler is (or becomes) smart enough:
The text was updated successfully, but these errors were encountered: