Skip to content
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

encoding/binary: Write does not optimise the case of native-format slices #70503

Open
jech opened this issue Nov 21, 2024 · 4 comments
Open

encoding/binary: Write does not optimise the case of native-format slices #70503

jech opened this issue Nov 21, 2024 · 4 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Performance
Milestone

Comments

@jech
Copy link

jech commented Nov 21, 2024

Go version

go version go1.23.3 linux/amd64

Output of go env in your module/workspace:

Irrelevant, but happy to provide.

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?

BenchmarkPutFloats-8         	  196766	      5958 ns/op	 671.41 MB/s
BenchmarkPutFloatsUnsafe-8   	465501314	         2.246 ns/op	1781336.18 MB/s

What did you expect to see?

I was expecting the performance of the two implementations to be similar.

@seankhliao
Copy link
Member

what about binary.NativeEndian?

@adonovan
Copy link
Member

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.

@jech
Copy link
Author

jech commented Nov 21, 2024

@dmitshur dmitshur added Performance NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Nov 22, 2024
@dmitshur dmitshur added this to the Backlog milestone Nov 22, 2024
@dmitshur
Copy link
Contributor

CC @griesemer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Performance
Projects
None yet
Development

No branches or pull requests

4 participants