Hacker News new | ask | show | jobs
by scriptproof 3596 days ago
If you make a program that is executed a million of times or more a day, it make sense to have a language that is "near the CPU", and allows to optimize and speed up the most. This is what Go is. It will be a mistake to use it elsewhere.
2 comments

Golang is only 'near cpu' when compared to python or ruby. Golang is much closer to java/c# then c/c++
Go (the language) have at least two implementations: the official Go implementation and GCC (yes, Go is included in GCC, along with Fortran and Ada).

The latest Go implementation (Go 1.7) has made Go a lot faster. I would argue that it closer the speed of executables generated with GCC (gcc/g++) than OpenJDK, the Oracle JVM, Mono or the .NET compiler for C#.

Go (the language) can be made just as fast as C (the language), for many cases. Go has the advantage of making it much easier to use multiple processors, though.

> The latest Go implementation (Go 1.7) has made Go a lot faster. I would argue that it closer the speed of executables generated with GCC (gcc/g++)

Go 1.7 performs nowhere near the set of optimizations that GCC and LLVM do. GCC/LLVM have a huge number of algebraic simplifications (InstCombine), aggressive alias analysis, memory dependence analysis, instruction scheduling, an optimized instruction selector, a highly tuned register allocator with stuff like rematerialization, SCCP, etc. etc. It will take years and years for Golang to come close.

> Go (the language) can be made just as fast as C (the language), for many cases.

No, it can't. The M:N scheduling model will always have some overhead relative to 1:1 if you don't need the performance profile of C10K-style servers. The dynamic semantics of "defer" is an unavoidable performance tax over RAII. Unwinding is mandated by the language, inhibiting some optimizations. There is little control over allocation: language constructs allocate in ways that are not immediately obvious. The fact that interfaces result in huge numbers of virtual calls results in a good amount of overhead that (unlike Java) Go can't even eliminate with inline caching, because it's AOT compiled. This is just off the top of my head.

> Go has the advantage of making it much easier to use multiple processors, though.

Not really. Go's parallelism primitives are just as low-level as those of C. The "one size fits all" scheduling algorithm is a poor fit for getting the most performance out of multicore. The lack of generics is a real problem: it prevents you from using optimized concurrent data structures without paying the tax of interface{} or going through code generation hoops.

In any case, the lack of SIMD basically kills Go's applicability in these domains.

Everything you wrote is true, but I think you're overstating the performance cost of Go design and implementation choices. I think the parent comment is fair in saying that Go is somewhere between GCC and the JVM in terms of performance. But I agree that Go is not designed for extreme performance (the kind of software where even a 1% gain matters), and that C/C++/Rust are better for these purpose.
Until you have control over stack/heap and data locality you're never going to be able to approach C/C++/Rust speeds.

Conversely if you're using C/C++ through a ton of heap/virtual pointers then you're losing a lot of the value the language brings and should be using something higher level.

Go allocates on the heap unless it can prove something doesn't escape, in which case it's on the stack. Not explicit programmer control, but I think you can reasonably make it do what you want.

Because it exposes pointers as a first-class concept, you also have good control of how data is laid out in memory (=> locality).

It's not like Python or Java where everything is a pointer and gets spread out all over memory.

> Not explicit programmer control, but I think you can reasonably make it do what you want.

Escape analysis, like any such analysis, gets much more difficult in the presence of higher-order control flow. Currently the Go compilers punt on higher order control flow analysis. And Go uses higher-order control flow in spades, due to its heavy reliance on interfaces.

The end result is that lots of stuff is heap allocated.

> Java where everything is a pointer and gets spread out all over memory.

That's not true for Java. Its generational garbage collector performs bump allocation in the nursery, yielding tightly packed objects with excellent cache behavior. Allocation in HotSpot is like 3-5 instructions (really!)

I think the HotSpot approach makes the most sense: instead of trying to carve out special cases that fall down regularly, focus on making heap allocations fast, as you'll need to make them fast anyway. After that, add things like escape analysis (which HotSpot has as well).

Java: But you're still chasing pointers for an array of objects right? Vs being able to just say, "I want this array to be X objects, all laid out in in a row in memory." I'm not a java programmer, but I'm pretty sure I've seen code that used primitive types rather than classes to get around this.

Actually, even Go isn't helping as much as it could here -- sometimes you want to have an array of objects that lays out each column (field) of memory contiguously, which Go gives you no easy way to do. But then neither does C or C++.

Isn't allocation just a couple instructions for basically GC languages?

> That's not true for Java. Its generational garbage collector performs bump allocation in the nursery, yielding tightly packed objects with excellent cache behavior. Allocation in HotSpot is like 3-5 instructions (really!)

When I see this link I get different impression.

http://mechanical-sympathy.blogspot.com/2012/10/compact-off-...

It is only heavy use of sun.misc.Unsafe and unidiomatic coding style that give Java semblance of memory efficiency.

Can you do an arena allocator in Go with disparate types? If not then you're really missing out on data locality.

In also not a huge fan of a compiler "automatically" performing escape analysis. Makes a single change causing cascading perf problems very easy and hard to catch.

no, not with disparate types (maybe if you resort to weird tricks with unsafe). i'd be curious to hear a use-case for this that ends up being different than just 'normal gc allocation' (not doubting you, just curious).
> The latest Go implementation (Go 1.7) has made Go a lot faster

After previous implementation has made Go a lot slower.

Two different things here: previous versions of the compiler got slower as it was moved, in automated fashion, from C to Go. Code compiled with Go was not slower. The new version of Go has both improved compile time (though not quite to the speed of the first few versions of the compiler) and improved code generation, so code is faster than any previous version of Go.
C++, otherwise a deeply flawed language, gives you more abstraction than Go and allows you to optimize and micromanage things more.
Not only C++, there are plenty of options.

The only thing good about Go, is being an evolution path for C coders willing to embrace a GC and some type safety.

One thing I can credit Go for is leading me from the untyped world to the typed one. I soon found the holes in the Go type system, and went looking for a stronger type system.

I now generally prefer Haskell.

If Go is from typed world, then Perl too.

Some magical built-in types (scalar, array, hash, typeglob, regexp, io handle) you cannot confuse, interface{} is scalar holding a reference, and built-in datatypes (arrays and hashes) are magical and you cannot construct something similar. Well, at least there's 'tie' mechanics in Perl after all that makes types extensible.

Go is more from the typed world than Python where I came from. It will tell you at compile time you are using a string rather than an int (unless you use interface{}).
The only difference is a selection of basic types. There's just no such types as string or int in Perl, Perl is a contextually polymorphic language whose scalars can be strings, numbers, or references (which includes objects). Although strings and numbers are considered pretty much the same thing for nearly all purposes, references are strongly-typed, uncastable pointers with builtin reference-counting and destructor invocation.
>The only thing good about Go, is being an evolution path for C coders willing to embrace a GC and some type safety.

You say that like it's a small thing, but what proportion of bugs in C code does that cover? Im guessing you'd hit over 50%.

It is a small thing because there are other languages that offer the same safety with more features and lets face it, if a C coder is willing to embrace a GC enabled language there are lots to chose from, with AOT compilation to native code.

I just expected more from Google, specially if one compares to the other company sponsored languages.

Comparing apples to apples, the first compiler sponsored by 'other company' was PHP, so Go looks not that bad in comparison. Reason is second, and maybe Google's second language would be 1ML.
Actually I was thinking in all languages that had commercial compilers, which goes way back than just PHP.
I love how C++ has had simple features like default arguments / function overloading for decades, while modern languages like Go and Rust require awkward workarounds.

Swift 3 looks good, though. They've learned the right lessons.

C++'s problem was never 'not enough features' it was precisely the opposite.
Default arguments, at least as done in C++, complicate the language's semantics (e.g., template specialization selection) far more than they raise the level abstraction. Definitely not a well-designed feature.

And Rust's traits are a far more principled (and thus better!) approach to overloading than anything C++ has (Boost's concept checks?). Traits turn concepts into language entities that are directly expressible in Rust syntax, rather than in awkward English documentation.

I certainly wouldn't say these are "simple features" in C++. Overload resolution in particular is one of the most complicated parts of the language. Default arguments can get weird since the right hand side of the default, more or less, just gets inlined at the call site; I do recall there were a couple bugs in the last year at work because of C++ default arguments, though I don't remember their details.

It's also fun when you don't realize a particular function has a default argument until you make a function pointer to it and assign/pass it to something that you mistakenly think is compatible. Depending on how nasty your codebase's use of templates and overloaded functions is, this can be a nightmare to debug.

There are also features that Rust has that C++ doesn't. So what?