--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/0ef15402-2e71-4522-bf67-26f766da55e5n%40googlegroups.com.
And how to interpret the conflicting messages for the following program?
--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/0f563565-42a0-40db-b513-50e9d0dd6fb3n%40googlegroups.com.
It also escapes if `n` is a local variable.From the code, it looks, if the capacity of the maked slice is larger than 1<<12, then the slice is allocated on heap.
Is there a possibility that, in the "make" implementation, different routines are chosen by different capacity arguments?
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/a44f78ab-2c10-475d-aeeb-1eef8cb8e412n%40googlegroups.com.
And how to interpret the conflicting messages for the following program?In one case, `g` is inlined, which is sufficient to prove that its return does not escape. But if you only analyse `g` as-is, you must assume that the return has to escape, as you don't know what the caller would do with is.
--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/571020ba-89c4-4de6-8eba-17d46b20ce42n%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/b78084a6-518a-4444-ab04-f0fd4c095a9an%40googlegroups.com.
That's not generally how it works, FWIW. Especially as far as escape analysis is concerned, the default is to escape and the compiler only marks it as non-escaping if it can prove so. So, the heuristic might not be perfect, but the only "unreasonable" imperfection would be, if it would put things on the stack which don't belong there. Any "the heuristic is not good enough to prove that it doesn't have to go on the heap" is, absent evidence of the opposite, a reasonable imperfection.That being said, maybe someone can give a good reason as to why large values shouldn't always live on the stack. To me, that seems pretty natural - we
generally want the stack to be relatively small (because we might need to have a lot of them) and we don't want it to grow in large steps (because then we might have to repeatedly grow/shrink them, when a function is called in a loop). And to me, slices and arrays seem naturally different - one is just a chunk of memory, the other is such a chunk of memory and a header containing a pointer to it. I agree that it's *probably* possible to either also have large arrays escape or also have large slices live on the stack - depending on what's more efficient. But I'm not enough of an expert to answer that.
I don't try to make criticisms here. I just seek a reasonable explanation for this implementation.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/f522b85c-9116-4f18-8aac-c3a41dcacf32n%40googlegroups.com.
I don't try to make criticisms here. I just seek a reasonable explanation for this implementation.As I said, maybe there is one someone else can provide. But I doubt it. I would assume the explanation is "it's a heuristic, written by humans, the implementation of which evolved over a decade - so of course there will be inconsistencies".I also don't think it's productive to demand an explanation - if the current implementation has a flaw (and FTR, I agree that it would make sense to treat `var x = [1<<13]int` and `var x = make([]int, 1<<13)` the same in regards to escape analysis) the solution isn't to explain how it came to be, but to fix it. Which is to say, to file an issue to that effect.That is, after all, how the current implementation came to be in the first place - we started with "everything goes on the heap" and then, little by little, people found cases where it's easy to show that a variable can live on the stack and implemented that analysis.If you just want to understand why the compiler comes to these conclusions (for reasons other than because you think they're flawed) the code is open source. I think that would be a reasonable start.Again, to be clear: Maybe someone who knows more about that code is able and willing to provide a better explanation for why it is this way or even a reason why it should be this way. In the meantime, there are other avenues you can explore.
In the following code, "make([]T, n)" is reported as escaped.But does it really always escape at run time?
Does the report just mean "make([]T, n) possibly escapes to heap"?
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/945468fd-0ba9-4102-bf71-e645466d08e1n%40googlegroups.com.
On Tue, Jun 1, 2021 at 4:40 PM tapi...@gmail.com <tapi...@gmail.com> wrote:
> The following is a tip to get an array pointer but avoid the array escaping.
I don't think so. The pointer to the local array 't', stored in 'y'
never leaves the function, so there's no need for 't' to escape. See
the previous post.