logoalt Hacker News

simioneslast Thursday at 3:01 PM1 replyview on HN

Depending on escape analysis, the array underlying the slice can get allocated on the stack as well, if it doesn't escape the function context. Of course, in this case, because we are returning a pointer to it via the slice, that optimization isn't applicable.


Replies

knorkerlast Thursday at 9:16 PM

Agreed that it could in principle. But I can't immediately get it to do so: https://go.dev/play/p/9hLHattS8cf

Both arrays in this example seem to be on the heap.

show 1 reply