-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Closed
Milestone
Description
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.
func Append(buf []byte, order AppendByteOrder, data any) ([]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):
func write(w io.Writer, order binary.AppendByteOrder, data any) error {
buf := writeBuffer(w, binary.Size(data))
binary.Append(buf[:0], order, data)
_, err := w.Write(buf)
return err
}
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:
buf := binary.Append(make([]byte, 0, 128), binary.LittleEndian, v)
earthboundkid, tklauser, dylandreimerink, florianl, dkanaliev and 4 more