Hi all,
First of all, forgive me to ask this. I am a noob to Golang and find it very promising to learn. I read that Golang is not as performant as C or C++ code. What's the reason behind this? AFAIK Golang is very close to C or inherits many features. If this is the case, why is Golang gaining popularity? How about the future of Golang developers? What's the primary domain of use - cloud?
Some people like to blame all of the slowness of languages like Go, C#, and Java on garbage collection, but that isn't a very complete story. People will correctly tell you that normally garbage collection takes up a very very small % of total runtime, and that is true, so what else is going on? There are some indirect and related things as well.
-
Languages that have garbage collection are attempting to be memory safe languages, so:
-
They will also tend to have array bounds checks when the compiler can't prove they can be elided
-
They will tend not to allow as many clever casts
-
They will tend not to allow pointer arithmetic
-
Providing ways to leverage SIMD instructions by hand is hard (Go gives access only through Go assembler at the moment)
-
They will tend not to have a way for the compiler to do floating point optimizations that can change results like --ffast-math in C compilers
-
-
Garbage Collection has some indirect effects aside from how much time is spent doing GC
-
Every time the GC runs it pollutes the CPU caches, slowing down code that runs later
-
GC will normally imply a degree of overhead when doing FFI (interop with other languages). Go has even more overhead due to how it manages it's stack, which facilitates goroutine features. Lots of things require FFI still (3d rendering, for example) so those things will always be slower.
-
The control you get over memory layout of your data is usually limited to some degree. For instance in Go you can't explicitly control whether data goes on the stack, or heap, or *where* to goes on the heap. On the bright side in Go you can at least work with any type you create as a value or a reference, so you can at least control things like making sure your array is an array of values in contiguous memory vs an array of references, which you can't in Java. Accessing RAM is very slow, controlling memory layout on modern computers can be a big deal, assuring that you hit the L1 and L2 cache as much as possible.
-
There are also just some cultural things, people working on the C and C++ ecosystems are doing so because performance is priority one. People working on ecosystems of memory safe languages obviously have a bit of priority for safety, correctness, or productivity or they wouldn't want the GC!
None of these things in isolation is normally a very big deal but they can add up.
There are tons of differences in the implementations of C and Go that slightly slow down Go in favour of safety. A few things:
-
goroutine scheduling
-
growing stacks
-
Runtime checks for memory access (you panic on nil not segfault)
-
runtime reflection
-
Bounds checking on arrays/slices
-
No naive casting (safe type assertion)
-
Different calling conventions
-
this allows panics at any pint
-
Go relies more on the stack (no fastcall)
-
-
Defer
-
Much fewer optimizations in favour of fast compile times
-
latency optimized GC, this limits throughput
All of this and much more lead to the fact that cgo has quite some overhead.
C is still the most dominant language and this will be remain true longer than we all live. Go improves C in lots of aspects that address issues faced today.
-
Go is memory safe making it way less dangerous to program critical systems
-
Great multicore capabilities in times after Moore's law
-
Go scales great