Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.Sign up
cmd/compile: byte(string) incurs allocation even when it does not escape #20881
The following Go program reports that the function makes an allocation to do the string to byte conversion, even though the compiler reports the conversion as non-escaping.
Given that the compiler has marked it as non-escaping, I'd expect no allocations.
go version devel +eab99a8 Mon Jun 26 21:12:22 2017 +0000 linux/amd64
Although the particular example would be fixed if #2205 was fixed, I don't think it's quite the same issue. I hadn't realised that all variable-length allocations escaped (strictly speaking that's not true, because byte(string) where len(string)<=32 is stack-allocated), and I'm not sure that others do either.
I'm probably not the only one who was using -gcflags -m output as a proxy for whether there's an allocation or not. Given that we're (I presume) not about to make some kind of instant-free arena for variable-length items, perhaps another way of addressing this issue might be to change the output printed by the -m flag to make it clearer that this the result of this kind of operation may actually escape to the heap.