News for hackers. Stuff that matters.
Re:Go 1.18 by pkulak on Tuesday March 15, 2022 @02:35PM (#30689138)
I can never seem to understand why a GCed language has pointers, and makes you memorize when to use stack vs heap.
Re:Go 1.18 by Laremere on Tuesday March 15, 2022 @02:55PM (#30689427)
Go uses escape analysis to ensure memory safety of pointers. Taking a reference of a local variable means it might end up on the heap, and calling new might allocate on the stack. It all depends on if Go can determine that the lifetime of the pointers is less than the stack frame.

It's good if you don't want to care about those details, it's bad if you really care about those details for performance reasons.

Re:Go 1.18 by mftb on Tuesday March 15, 2022 @02:45PM (#30689275)
It's pretty straightforward, knowing when to use the stack vs heap can improve your performance. Having both paradigms available in the language means you can start out relying on the GC for everything. Later when you're ready, you can look to make more intelligent use of the stack.
Re:Go 1.18 by bashery on Wednesday March 16, 2022 @09:07AM (#30698283)
+1
Re:Go 1.18 by lawl on Tuesday March 15, 2022 @07:29PM (#30693325)
Hits the sweet spot for me between the one extreme of having to malloc/free everything myself and the other extreme where everything is immutable, garbage collected and you dont know what causes allocation.

The former is a nightmare for security and productivity, the latter is a nightmare for performance.

Re:Go 1.18 by int_19h on Tuesday March 15, 2022 @10:16PM (#30694519)
Go can implicitly heap-allocate even a local variable if you e.g. capture it in a closure. This is actually more implicit than you get in Java or C#.
Re:Go 1.18 by morelisp on Wednesday March 16, 2022 @03:08AM (#30696087)
Java can decide to heap-allocate or not heap-allocate at runtime based on what optimizations the JIT has performed so far, and so it can change over the course of a program's run. I don't think you can get more implicit than that.
Re:Go 1.18 by int_19h on Wednesday March 16, 2022 @06:18AM (#30697079)
Why would Java ever heap-allocate a local, regardless of optimizations?

It can decide to heap-allocate or stack-allocate objects (when you new them) based on escape analysis, which is a JIT optimization. But that's very different from heap-allocating variables.

Re:Go 1.18 by morelisp on Wednesday March 16, 2022 @07:02AM (#30697355)
Then I'm not even sure what you're talking about when you say "heap-allocate even a local variable" if you don't actually mean the value of that variable. If you draw that distinction, "variables" are never allocated; spaces for values are.

Regardless, whether something is a local or not in Java still varies depending on the JIT's decisions. (Or rather, the JIT does not see local/non-local variables, only lifetimes, and whether the lifetime may extend past the JIT's current view.)

Re:Go 1.18 by int_19h on Wednesday March 16, 2022 @07:52PM (#30705700)
In Java, the value of the variable of a class type is not an object - it's a reference to the object. Those references are always stack-allocated. Variables of primitive types like ints are also, of course, stack allocated.

In Go, if you have a local of type int, and that local is captured by a closure, it will be allocated on the heap to extend its lifetime. Java doesn't need to do this because it requires captures to be effectively final. On the other hand, C# does the same thing as Go.

Re:Go 1.18 by foldr on Tuesday March 15, 2022 @05:34PM (#30692085)
>and makes you memorize when to use stack vs heap

You don't have to think about stack vs. heap allocation in Go unless you're writing performance sensitive code. You could pretend that Go heap allocates everything and still read and write Go code without any problems.

Re:Go 1.18 by hnov on Tuesday March 15, 2022 @02:52PM (#30689380)
I think it's at least partially about value vs (mutable) reference semantics in Golang's case and the hope is that the compiler will know to whether heap or stack allocate based on escape analysis.
Re:Go 1.18 by pjmlp on Wednesday March 16, 2022 @04:28AM (#30696515)
Performance, Java made the mistake to not have good ways to deal with GC free allocations and now who knows when Valhala will ever happen.

Thankfully plenty of other GC based languages hadn't made the same mistake.