I'm sending strings delimited by CRLF over a socket. As an exercise, I'm trying to do it while minimizing the total number of allocations.
I wrote a demo with two implementations, CopyWithTerminatingNewlines1 and CopyWithTerminatingNewlines2, both of which copy lines from a `chan []byte` to an `io.Writer`, inserting delimiting CRLFs. But the results are confusing to me (Go 1.17.3 on linux/amd64): https://gist.github.com/slingamn/e4727ad854bdbe585134af9e55956e12 1. Both implementations are functionally identical, and `(*net.Buffers)WriteTo` gets specialized to writev(2) at runtime 2. If I'm interpreting the disassembly correctly, CopyWithTerminatingNewlines1 allocates a new net.Buffers on every iteration of the loop, while CopyWithTerminatingNewlines2 does not 3. This is because (*net.Buffers).WriteTo causes the net.Buffers to escape to the heap But I'm not sure why this is the case --- am I doing something wrong, or is this a limitation of the escape analyzer that could in principle be improved, or is there some essential reason for this? Thanks for your time. -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/f455ff4d-edc2-4cfa-8408-0057f5682cd6n%40googlegroups.com.