On Mon, 27 May 2019, 07:18 Sergey Kamardin, <[email protected]> wrote:
> Hello Ian, > > Thank you for your answer. > > On Sun, 05/26/19, May 26, 2019 at 07:59:07PM -0400, Ian Lance Taylor wrote: > > This is not valid. The rule is that SliceHeader is only valid when > > inspecting an actual slice header. You have to write > > > > h.Data = uintptr(unsafe.Pointer(&b)) > > h.Len = len(b) > > h.Cap = len(b) > > > > I assume this is reduced greatly from the real code, as you could also > > just write > > > > bts = b[:] > > Does this mean that it is not possible to get rid of heap allocation for > passing slice of bytes to some method interface like io.Reader.Read()? > In general heap allocation is needed for this case because r is an interface and there is no guarantee that it won't store the slice argument somewhere and then return. If you were to use a static type, the compiler might be able to determine that b doesn't escape and allocate it on the stack. You might find sync.Pool helps. > Both code examples above are leading b to escape to the heap when > passing to the interface method. > > -- > Regards, > Sergey. > > -- > 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 [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/20190527061838.GA8345%40MacBook-Pro-Sergej.local > . > For more options, visit https://groups.google.com/d/optout. > -- 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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAJhgaci7Hq%2B0pkyHB35zBxRs2Hhu%3D_R6dxZ3Q%2BRF_91p_aRO0A%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
