Hacker Timesnew | past | comments | ask | show | jobs | submitlogin

> Go's model is such that, compared to JVM-based languages, you have the control to create (much) less garbage in the first place, greatly reducing the size and impact of garbage collection pauses.

I have only heard of escape analysis (which Java has) and structs inside structs (which C# has; C# also lets you put structs on the stack). Is there anything that Go has over Java and C#?



Go lets you pass around raw values without overhead. An int32 is 4 bytes. A struct { a, b int32 } is eight bytes. A [4]int32 is 16 bytes. A [4]struct{ a, b int32 } is 32 bytes. (I think you get the point :-)

In Java all objects are passed by reference, which causes bloat and cache locality issues. Also there are bookkeeping costs associated with even trivial objects: a simple array carries around an additional 16 bytes of memory.

Furthermore, the core Java libraries make it very difficult to write code that doesn't generate a lot of garbage. In fact, it's very difficult to write allocation-efficient code in Java without writing very unidiomatic Java code. These problems are worsened in more dynamic JVM-based languages like Scala and Clojure, which generate huge amounts of garbage due to runtime reflection.

Here's an interesting discussion of these issues:

http://loadcode.blogspot.com/2009/12/go-vs-java.html


> dynamic JVM-based languages like Scala and Clojure

Scala is a statically typed language

Go may make more efficient use of memory for the cases you describe, but the JVM still beats the pants off Go:

http://shootout.alioth.debian.org/u64q/benchmark.php?test=al...

Also, you haven't factored in Java's escape analysis and on-stack allocation.


The topic of this discussion is garbage collection and memory allocation. The graphs on that page show Go beating Java in memory usage in every case. I'm not sure what your point is.

Scala is a statically typed language but it must do runtime type reflection to implement some of its features on top of the JVM. That comes at a cost (and in fact we decided not to implement Go on the JVM for this exact reason). I know of a certain well-known company that uses Scala heavily and their JVM instances spend >80% of their CPU time in garbage collection.

The JVM has had a tonne of optimization done to it. The Go compilers and runtime have had barely any. There's plenty of low-hanging fruit: recent changes to the gc code generation have yielded as much as 2x speedups in certain operations.

My observation, from watching very skilled Java programmers build and deploy programs, is that garbage generation and collection latency cause serious problems. My observation of similar Go programs is that these kinds of problems don't really come up.


> Go lets you pass around raw values without overhead.

Does that rely on heuristics like escape analysis or is it user defined (and predictable)?


In C#, it's up to the person defining the type to decide whether it has value/stack semantics (a struct) or not (a class). Structs have some limitations regarding constructors, and being passed by value is surprising to users used to classes. For these and other reasons, the vast majority of types are classes.

So you can have on-stack or embedded objects, but they are fairly rare.

In Go, the user of a type decides whether it will be used by reference (on the GC heap) or directly on the stack or embedded in an object: you can take a pointer to any type, or use the type directly.

That gives the consumer of a type much greater flexibility to not create garbage if they don't want to.


> In Go, the user of a type decides whether it will be used by reference (on the GC heap) or directly on the stack or embedded in an object: you can take a pointer to any type, or use the type directly.

Actually the distinction between heap and stack is not determined by how it is referenced. The compiler is free to stack-allocate any value as long as it does not escape the function. We can do this because pointers are opaque; there's no arithmetic.

The main point is that Go does not have classes. You just define methods on values. Values are no bigger than the data they represent, so you don't suffer from the same kinds of overheads seen in other "OOP"-centric languages.


How is that even possible? Surely there must be a type tag associated with each object if you can dispatch on it, just like in other languages.


A variable of an interface type is a pair consisting of a type descriptor and a concrete value. It is only when a concrete value of a statically known type is passed to something expecting an interface type that such a type tagged value is constructed.


For interface values there is a type associated with each value.

For normal values you don't need it, because Go is statically typed. The compiler knows where the method is.


Interesting, thanks!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: