Open jech opened 9 hours ago
what about binary.NativeEndian
?
The reason for the difference is that binary.Write allocates a 4KB buffer for each call, and the allocation escapes by being passed to io.Writer.Write. On a little-endian machine the contents of memory in that buffer are identical to the original array, so the alloc could be avoided in that case.
what about binary.NativeEndian?
https://github.com/golang/go/blob/master/src/encoding/binary/native_endian_little.go#L9
CC @griesemer.
Go version
go version go1.23.3 linux/amd64
Output of
go env
in your module/workspace:What did you do?
I wrote a (large) number of slices of little-endian IEEE floats using
binary.Write
on a little-endian machine. I was expecting the library to recognise that I was writing in the native format, and special-case the write. However, it did not, and the performance was about 3000 slower than expected.https://go.dev/play/p/wNWegxHXjlG
What did you see happen?
What did you expect to see?
I was expecting the performance of the two implementations to be similar.