News for hackers. Stuff that matters.
Re:Computer Language Benchmarks Game – Rust vs. Go by SwellJoe on Tuesday December 23, 2014 @06:35PM (#8790824)
Had I been asked "which do you think is faster, on average?" I would have guessed Go. I'm not sure why but that was my gut feeling. Rust feels newer and less developed (though they are both similar in age), to me, and also more ambitious in some regards.

But, this turns that on its head. Microbenchmarks are, of course, to be taken with a grain of salt, but it's still a useful data point. More useful would be a discussion of why it is so.

Re:Computer Language Benchmarks Game – Rust vs. Go by wycats on Tuesday December 23, 2014 @06:55PM (#8790877)
A design goal of Rust is to target the performance of well-written, safe C++. It doesn't (today) even ship with a garbage collector, discourages heap allocation (which is your only option in normal Go), and avoids dynamic dispatch in the vast majority of cases. In contrast, idiomatic Go code is garbage collected, heap allocation heavy, and dynamic dispatch heavy.

That isn't always going to mean that Rust code is faster, but for benchmark games like this, when Rust is slower it almost always means that the benchmark is wrong.

Re:Computer Language Benchmarks Game – Rust vs. Go by SwellJoe on Tuesday December 23, 2014 @07:07PM (#8790913)
I read an article recently about memory in Go (the "why is my go program 138GB!?" article), which was enlightening and also weird. I don't have any experience with either language, but have plans to learn a smattering of both in 2015.

As for dynamic dispatch...does Rust not support it, or is there some language construct that allows solving the problems dynamic dispatch solves without imposing annoying limitations or needless verbosity? And, aren't there fast implementations of dynamic dispatch?

Re:Computer Language Benchmarks Game – Rust vs. Go by wycats on Tuesday December 23, 2014 @07:24PM (#8790956)
> As for dynamic dispatch...does Rust not support it, or is there some language construct that allows solving the problems dynamic dispatch solves without imposing annoying limitations or needless verbosity?

Rust supports dynamic dispatch if you truly need it, but the normal approach is to use trait bounds, which the compiler expands when used. Here's an example:

    fn main() {
        let r = MemReader::new("hello world\n".as_bytes());
        get_line(&r);
    }

    fn get_line<R: Buffer>(r: &R) -> IoResult<String> {
        r.read_line()
    } 
In this very simple example, the `get_line` function takes any value that implements the `Buffer` trait, which provides the `read_line` method. When `get_line` is called inside of `main`, the compiler creates a special version of `get_line` that takes a `MemReader`, and dispatches to its implementation of `read_line` statically. In practice, this feels a lot like dynamic dispatch, but using trait bounds always results in static dispatch in practice. Also, in this example, the `MemReader` is stack-allocated, and lent to the `get_line` method.

In very rare cases, you may want dynamic dispatch. For example, imagine you have an array of Buffers, and want to read a line from each of them. In this case, you use a "trait object" (a `Box<Buffer>`), which moves the object to the heap and allows dynamic dispatch.

In practice, I have written many thousands of lines of production Rust code and have only encountered a need for trait objects a handful of times. This means that idiomatic, normal Rust code is stack-allocated and statically dispatched.

The verbosity of trait bounds has decreased more and more over time, and my favorite proposal (by aturon) for making it pretty close to maximally ergonomic is:

    fn main() {
        let r = MemReader::new("hello world\n".as_bytes());
        get_line(&r);
    }

    fn get_line(r: &impl Reader) -> IoResult<String> {
        r.read_line()
    } 
I hope this lands as new syntax after 1.0 :)
Re:Computer Language Benchmarks Game – Rust vs. Go by allan_s on Tuesday December 23, 2014 @10:43PM (#8791447)
it would indeed be great to have this syntax, can you link to the RFC if you have it? so that we can follow how is this proposal going.
Re:Computer Language Benchmarks Game – Rust vs. Go by steveklabnik on Tuesday December 23, 2014 @07:17PM (#8790942)
Rust supports both. You might enjoy reading http://programmers.stackexchange.com/a/247313
Re:Computer Language Benchmarks Game – Rust vs. Go by tterrace on Tuesday December 23, 2014 @08:45PM (#8791167)
Could you link the "why is my go program 138GB!" article?
Re:Computer Language Benchmarks Game – Rust vs. Go by dbaupp on Tuesday December 23, 2014 @08:59PM (#8791205)
I believe it's http://utcc.utoronto.ca/~cks/space/blog/programming/GoBigVir...

Permalink to the relevant section of the Go source, with descriptive comments: https://github.com/golang/go/blob/04cf881fbea55d5bca584c78b1...

Re:Computer Language Benchmarks Game – Rust vs. Go by shanemhansen on Tuesday December 23, 2014 @09:55PM (#8791346)
Technically it's not true that heap allocation is your only option on go. In Go you have 0 options but stack allocation is automatically done in some cases.
Re:Computer Language Benchmarks Game – Rust vs. Go by TheHydroImpulse on Wednesday December 24, 2014 @12:11AM (#8791639)
Yes, escape analysis does tend to eliminate some unnecessary heap allocations but they're specifically isolated to a single subroutine (it's better than not having it). Whereas with Rust, you can safely pass around references to stack allocated memory to other subroutines, given proper lifetimes.
Re:Computer Language Benchmarks Game – Rust vs. Go by pdpi on Tuesday December 23, 2014 @08:03PM (#8791055)
There's not even a chance at a competition, because of their respective design goals.

Go is explicitly meant to have _really_ fast compilation times, which precludes the Go compilers from any expensive optimisations. Rust, on the other hand, has an explicit goal of being as fast as idiomatic/safe C/C++ while removing the burden of writing the safety yourself, so it really _has_ to be fast.

Re:Computer Language Benchmarks Game – Rust vs. Go by samnardoni on Tuesday December 23, 2014 @06:45PM (#8790843)
Rust uses LLVM, which does all the optimisation. Go generates the machine code itself and its optimisation is pretty weak compared to LLVM.
Re:Computer Language Benchmarks Game – Rust vs. Go by SwellJoe on Tuesday December 23, 2014 @06:57PM (#8790882)
An older version of the benchmarks discussed on the Go mailing list was done with GCCGO (which I assume is Go with a GCC backend). Given that GCC is historically ahead of LLVM on CPU optimizations (a gap that has shrunk in recent years), I'm guessing that's not a complete answer.

But, if it is the primary source, it would tell me that Go will close the gap as the compiler becomes more advanced and takes advantage of more CPU optimization features. Also, it's an interesting difference that Rust is an LLVM language while Go is compiled to native code directly; probably not from a performance perspective (at least not in the long run), but from a development perspective...is it easier or harder to develop new language features on LLVM or a native compiler?

https://groups.google.com/forum/#!topic/golang-nuts/NSPcEqhe...

Re:Computer Language Benchmarks Game – Rust vs. Go by dbaupp on Tuesday December 23, 2014 @08:14PM (#8791089)
Note: despite the "VM" in the name LLVM is a native compiler: the rust compiler constructs the appropriate internal representation to LLVM and passes it to LLVM for optimisation and native code generation. There's not really a fundamental difference between this and writing the code generation step in pure Rust other than the latter missing out on all the benefits (speed, architecture support etc) of LLVM.

That is to say: there's not much difference for difficulty of developing new language features, but avoiding LLVM makes it harder to develop new fast language features.

Re:Computer Language Benchmarks Game – Rust vs. Go by mamcx on Tuesday December 23, 2014 @06:48PM (#8790854)
Exist a well rounded samples for benchmark compilers?, I mean, not just micro, but more complete?
Re:Computer Language Benchmarks Game – Rust vs. Go by chipsy on Tuesday December 23, 2014 @08:14PM (#8791086)
You can typically predict performance behaviors across a whole language based on memory allocation patterns and the type system.

If automatic memory management is built in, that adds some overhead to most allocation situations(and encourages an architecture that exploits easy allocation - e.g. Java libraries where everything you do involves a new()). There are differences between garbage collection(mark-and-sweep) and reference counting as well; an RC system is "pay for what you allocate" with predictable scaling, making it more appropriate to employ for hitting real-time deadlines, however, the state-of-the-art in generational mark-and-sweep GC does better for average case throughput. Or to put it another way, if you have a single batch process that everything goes through, you can usually count on a GC system doing it faster than a RC equivalent. But if you want to run several things concurrently and tally results, or you are running an interactive app and care about getting your latency numbers down, the pauses involved in GC may be more problematic to you. (The semantic downside of RC, which has also made a lot of languages avoid it, is its inability to deal with circular data references.)

Of course, manually managed memory lets you design any allocation strategy you want, and so it can let you blow away automatically managed systems on microbenchmarks like these - stuff that starts mattering in the real world only after you've scaled up near to the constraints of the target hardware and any overhead is a Really Big Problem. There's definitely some more experimental automatic technology out there that "breaks the rules," but in terms of what you see in today's production environments, you can start estimating performance characteristics just by knowing what type of allocation is being done.

The other big issue, of course, is the type system. The design of the type system determines the constraints and difficulty of automated optimizations; classically, Fortran was one of the best languages for low-level optimization because it made things very easy for the compiler, and offloaded many considerations on the programmer. Today C has grown into a roughly equivalent space(pointer aliasing, the last big issue, was tackled in C99). Rust can do everything C does, but it also has a much smarter type system backing it up and enforcing memory safety. Dynamically typed languages like Python, Ruby, Lua etc. are essentially always slower and more memory-hungry because of their intentional flexibility - to do as much as they do at runtime, they end up retaining more stuff per piece of data and paying a bigger cost to manipulate it. Luajit is an example of best-in-class technology for dynamic languages, and it's still not as fast as Go on most of the benchmark game. [0]

Where Go stands in these two concerns is roughly comparable to Java or C#: it uses GC, and it has an intentionally simplistic static type system which points you towards built-in data structures, which makes it easy to work with for average-case situations, but hard or impossible to "tune up" as needs grow more complex. And if you check the benchmark game, all three are in the same window of performance, with some variance that could mostly come down to implementation detail.

[0] http://benchmarksgame.alioth.debian.org/u32/compare.php?lang...

Re:Computer Language Benchmarks Game – Rust vs. Go by bjz_ on Tuesday December 23, 2014 @11:52PM (#8791595)
> Rust can do everything C does, but it also has a much smarter type system backing it up and enforcing memory safety.

This is why I have hopes that Rust will make it easier to match the performance of micro-optimised C code in the future whilst still maintaining clarity and safety (the compiler knows more invariants about the code, so can be more aggressive in its optimisations).

Re:Computer Language Benchmarks Game – Rust vs. Go by copx on Tuesday December 23, 2014 @11:37PM (#8791557)
The benchmarksgame site does no longer feature LuaJIT benchmarks. The link you provided compares PUC Lua (an interpreter, no optimizer) against Google Go (AOT native code compiler, weak optimizer).
Re:Computer Language Benchmarks Game – Rust vs. Go by Dewie on Tuesday December 23, 2014 @09:45PM (#8791325)
So, Go is garbage collected and seems to rely on compiler optimizations for optimizing heap allocations to stack allocations (where applicable). Meanwhile, Rust is centered around manual memory management, discourages heap allocation if you can use stack allocation, and even has (or plans to have) a library for choosing different allocators when those give better performance (which should give an inclination of at what level Rust aims for). And it leverages an existing compiler architecture for generating the native code.

Your hunch/gut feeling seems to be very off.

Re:Computer Language Benchmarks Game – Rust vs. Go by SwellJoe on Wednesday December 24, 2014 @06:43AM (#8792496)
It was based on lack of knowledge about Rust. I didn't know any of those things about Rust. I assumed it was similar to Go (garbage collected, at the least). As I mentioned, I don't know a lot about either language; less about Rust. Which is why I mentioned I'd like to see a discussion about why Rust was faster.