On Mon, Nov 8, 2021 at 9:59 AM Shivaram Lingamneni
<shivaramlingamn...@gmail.com> wrote:
>
> 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?

It's because the (*Buffers).WriteTo method makes a method call on an
interface type, and the compiler doesn't know whether the method
causes the Buffers argument to escape or not.

This could be improved in principle but I think it would be hard.

Ian

-- 
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/CAOyqgcWCBidgo6kii_4LFCtWvdrapOXJ-nRQx3CL%3DvpPg%2BOPoQ%40mail.gmail.com.

Reply via email to