by IshKebab · 3y ago · 0 comments
That's sort of true, but I wish people would stop cowardly saying that you can't say that one language is better than another overall. That's clearly not true.
IMO this article is very very long and doesn't really highlight the differences well, which IMO are:
* Go has much faster compile times.
* Rust's tooling is very good but I would still say Go's is better. It has built in fuzzing support! Cross compilation is as simple as setting an environment variable.
* Rust is a little faster, and avoids GC.
* Go's standard library is much more comprehensive than Rust's.
* Go is much easier to learn and to read.
* Rust is a better language if you have learnt it. It has much more powerful features (especially the functional ones) and is more principled. Going back to Go from Rust is frustrating.
* Rust has a much stronger type system and the borrow checker. Both make bugs far less likely.
* Async Rust is probably the worst bit about Rust. Go's goroutines are much easier to use. Though I would say traditional multithreaded code in Rust is pretty easy - it's just that a lot of the ecosystem forces you into async.
I also found this sentence to be confusing:
> If you only have time to invest in learning one language well, don’t make your final decision until you’ve used both Go and Rust for a variety of different kinds of programs, large and small.
So, if I only have the time to learn one language, I should...use two languages? For multiple projects? But somehow without learning them, because I don't have time?
Depending on what you're doing.
I am finding that Rust is easily beating all of the non-C languages in terms of full support for the latest performance technologies e.g. io_uring, SIMD (AVX2/AVX512/NEON), DirectStorage, CUDA etc as well as the highest quality wrappers for C/C++ libraries.
So for many applications Rust will simply leave Go for dust.
In fact for me the only benefits of Go are single binary and startup time. Otherwise I find Rust to be ideal for when I need low level performance. And Scala to be significantly better for backend apps since its faster, has access to more libraries, better concurrency and a stronger type system resulting in much more reliable code.
I hope you mean "compilation time" or "time to first prototype". My experience has been that, if a Rust binary doesn't start instantly, it's the fault of a specific library, not the language in general.
Also, they ARE working on single-binary deployment for Rust... they just have a much more "Take your time and do it right. Don't hack around ecosystem flaws" approach than Go.
If you're not cross-compiling/cross-deploying, Rust is already on par with non-cgo Go... you just need to use a musl-libc build target for Linux, specify RUSTFLAGS="-C target-feature=+crt-static" for Windows, etc. to get statically linked libc.
(The musl-libc targets are a simple `rustup target add` away if your only C dependency is the libc itself.)
- Go (the language) is missing the ability to declare something immutable. I don't mean making fields in a struct private then exposing getters. I mean the equivalent of an immutable reference in Rust or `const ref` in C++.
- Go mod and go workspace still can't get a cohesive story together [1]: - - If my module wants to depend on another module in a workspace, `go mod tidy` would freak out. - - If I use replace in my go mod file to point to the other modules in the same workspace, `go mod vendor` will copy these workspace modules to the vendor folder as well.
I always wondered: how is it not much faster? Dealing with the borrow checker is immense overhead, yet performance gains are low compared to Go. Is Go's GC simply that good? Is Rust's compile-time GC about something other than performance somehow?
I realize Go still needs to ship a minimal runtime in its binaries, but don't feel that's a meaningful burden compared to borrow checker fighting.
AFAIK, memory safety and language features as RAII is also available in C++, for instance. About the reasons for slow compilation, take a look at https://www.reddit.com/r/rust/comments/xna9mb/why_are_rust_p...
Not having a GC is also about not having a runtime as you mention (e.g. nice for creating Python extensions and embedded systems programming) and also more runtime deterministic performance: on that, if I'm not mistaken that was the reason for Discourse switching to Rust and also, e.g.: "the choice of Rust as the main execution language avoids the overhead of GC pauses and results in deterministic processing times" https://github.com/apache/arrow-ballista/blob/main/README.md
With idiomatic non GC programs, an object that is freed is deallocated at that point of execution.
Think of a loop running 15 times that calls a function which allocates objects, uses them and then returns.
In a GC language, there is only a single free. In a non GC language, each return of that function will cause a free to occur.
There's some other interesting optimisations that can only be done in a GC language.
The only way to get those optimisations in a non-gc language is to effectively implement your own allocation and deallocation using pools.
Predictability and determinism: In some environments, GC pauses are problematic.
Modern GCs in Go and Java running on fast enough CPUs can generally keep pause times down below 1 ms, but on a multi-GHz CPU that's multiple million clock cycles - a virtual eternity in some contexts.
Memory usage: GCs waste memory in exchange for performance. Go's GC doesn't compact (last I checked), so fragments the heap.
Ability to have no runtime: in some environments, the need for a runtime is problematic. That's a big part of why the Linux kernel supports Rust but not Go.
All of these issues tend to be more significant in e.g. hard real time contexts, in embedded environments with less powerful processors, etc.
Also btw "borrow checker fighting" is not much of an issue for experienced Rust developers. You quickly get used to how to write code to avoid borrow checker issues. And in general, writing code that way tends to be a good thing anyway.
By copying more?
Also, by knowing when and how to use wrappers like RC, Arc, RefCell, Box, etc., and by knowing when and how to use lifetime annotations.
As for copying, you learn when to move, when to copy, when to clone, and when to implement the relevant traits on your types.
It's really not that different from working in a language like C or C++, except that the compiler ensures that you're not making any mistakes.
Both of your sentences are true, but are not related. GCs indeed make a memory tradeoff for better performance, even if it is a compacting GC. But dynamic allocation even with Rust does fragment the memory and is in fact solved better by a compacting GC.
It's also stuck on an old fork of tcmalloc which isn't anywhere near the state of the art today and will only fall further behind if they don't at least start on a plan to make allocators interchangeable. Bear in mind that Rust has had interchangeable allocators for years, and Java has had interchangeable GCs for decades, while Go still has neither.
Go's GC is pretty good for what it is, especially now that it has become more of a control system that converges on a target collection time ratio. But it is neither a state of the art GC -- not even by the standards of 10 years ago given how far along the JVM was already -- nor is [de]allocation done with a state of the art allocator.
People praise that Go promises a 10ms max duration for GC Assist -- the mechanism that makes your actual critical-path work pause and help out the struggling GC -- and while 10ms is fine for a logs pipeline or something, it's extraordinarily bad for many kinds of API endpoints which should have a 99th percentile latency under 1ms (bearing in mind that even local networking is going to add to that considerably so the floor should start low).
That's despite Go's GC being hailed for targeting predictable low latency rather than overall throughput... this is the predictable low latency they're talking about. 10ms is a failure in a world of sub-ms targets, and Go as a language gives you very limited options to avoid it, even after abandonding community idioms and popular libraries.
Let me just focus on one very commonly used data structure: hash maps. Rust hash maps:
* Use SwissTable because they didn't make a short-sighted promise about delete-while-iterating behavior that makes Go incompatible with SwissTable's tombstone design and makes Go never shrink maps once created.
* Let you customize the hash function to whatever performs best for your key type & target hardware.
* Let you find & insert/update in one hash table lookup, where Go always requires at least two, and its compiler doesn't optimize even the most common idioms.
* Let you reference a key/value in-place instead of having to copy it out each time. Go won't allow this because the reference may become stale, but Rust can enforce this with lifetimes.
* Let you clone a map without rehashing every key to a new seed. I generally measure at least 15x speedup from this alone, unlocking very useful design patterns like "clone a map and apply a few temporary updates for a one-off operation like validation or simulation" with no extra code complexity. Go gives you no better option than slowly rehashing the entire map.
And that's just hash maps. How about Go's regex engine being one of the slowest in the world while Rust's regex crate being one of the fastest:
https://github.com/mariomka/regex-benchmark#optimized
https://github.com/BurntSushi/rebar#summary-of-search-time-b...
Further, Go refusing to have macros means that many libraries use reflection instead, which often makes those parts of the Go program perform no better than Python and in some cases worse. Rust can just generate all of that at compile time with macros, and optimize them with LLVM like any other code. Some Go libraries go to enormous lengths to reduce reflection overhead, but that's hard to justify for most things, and hard to maintain even once done. The legendary https://github.com/segmentio/encoding seems to be abandoned now and progress on Go JSON in general seems to have died with https://github.com/go-json-experiment/json . Remember that next time someone parrots the myth that Go is ideal for HTTP JSON APIs.
Many people claiming their projects are IO-bound are just assuming that's the case because most of the time is spent in their input reader. If they actually measured they'd see it's not even saturating a 100Mbps link, let alone 1-100Gbps, so by definition it is not IO-bound. Even if they didn't need more throughput than that, they still could have put those cycles to better use or at worst saved energy. Isn't that what people like to say about Go vs Python, that Go saves energy? Sure, but it still burns a lot more energy than it would if it had macros. Maybe someone's recipe website doesn't care but world-scale datacenter applications do.
Rust can use state-of-the-art memory allocators like mimalloc, while Go is still stuck on an old fork of tcmalloc, and not just tcmalloc in its original C, but transpiled to Go so it optimizes much less than LLVM would optimize it. (Many people benchmarking them forget to even try substitute allocators in Rust, so they're actually underestimating just how much faster Rust is)
Oh yeah, aside, tcmalloc was designed for C++ code with lots of temporary allocations that are allocated & freed by the same thread so they're very cheap when cached per thread/core. Go almost always frees things on a separate GC thread, completely undermining this design goal. So tcmalloc isn't even a good fit for Go, yet there's no other choice available. People don't even discuss it any more because what is there to discuss, Go and all of its users are just stuck with it.
Finally, even Go Generics have failed to improve performance, and in many cases can make it unimaginably worse through -- I kid you not -- global lock contention hidden behind innocent type assertion syntax: https://planetscale.com/blog/generics-can-make-your-go-code-...
It's not even close. There are many reasons Go is a lot slower than Rust and many of them are likely to remain forever. Most of them have not seen meaningful progress in a decade or more, and I'm not holding my breath for the next decade either.
With that said, I find it questionable to say that Go would be faster than Rust - the former does barely any optimizations (hence its fast compile time).
True. Golang multithreading is easy to reason to about. I had to make some mental gymnastics to write multithreaded scripts in Ruby and Python. The Golang implementation was the easiest by far.
Even if you mostly write Rust (or whatever), it’s still very useful to know other languages. Learning C will teach you how rust works under the hood, and teach you a lot about FFI systems everywhere. Learning javascript will be a massive help if you do web programming. I’m learning ML at the moment and python for that seems like a hard requirement. If you develop for Apple’s platforms, Swift is important to know. And so on.
And most languages just aren’t that hard to learn anyway. If you’re an experienced programmer, learning enough Go to be dangerous is a weekend project. It’s just not that much work.
The notion of "last language" that I used is, however, a bit different. It just meant that if you've implemented something in Rust, you won't need to rewrite in C or C++ due to presence of garbage collection or a need to integrate it as a library.
How so? Rust don't use C under the hood. (And even in ffi, you should just know the limitations of C to know what you can't do in FFI)
distributions are going to love your module! /s
There was a huge kerfuffle about it.
https://blog.yossarian.net/2021/02/28/Weird-architectures-we...
I really prefer strong type systems over weak ones, but despite that, I always like to point out that there's no evidence that strong type systems cause "bugs to be far less likely". No study I've seen (quite a few) has ever shown that.
There's a few bugs that become impossible (in Rust, some kinds of data races for example) but the vast majority of bugs can still occur and in practice, do occur.
There's a really great one that shows that Typescript or Flow prevents around 15% of Javascript bugs.
It would be difficult for a study to show that Rust's strong typing or borrow checker prevents bugs, but anyone who has used Rust extensively knows it to be true. It's similar to Haskell's strong types and immutability preventing bugs. Google "if it compiles it works".
I think you could do a study to prove this, but experiments aren't the only way to get knowledge. They are the best when possible but sometimes they are impractical. Programming language studies tend to focus on "get some students to spend a few hours writing code from scratch" tasks which can even lead to conclusions that are the opposite of reality for realistic programming. For example that static types hurt productivity.
> the vast majority of bugs can still occur and in practice, do occur.
Right, but they are much less likely to occur.
> anyone who has used Rust extensively knows it to be true.
I have used Rust extensively and have no idea whatsoever is that's true, and if you think you do, be prepared to be wrong about that, as humans are incredibly "good" at being sure that something is true until formal studies show you were wrong.
Haskellers have said "if it compiles it works" for a long time, but have never been able to prove that. Again: just saying/feeling something is true doesn't make it true.
> Right, but they are much less likely to occur.
No, there's just no evidence! Just go to any Rust project, they have as many bugs as anything else... I wish that were false, because then I would definitely just use Rust for everything myself... but that's not at all what I've seen so far.
Also, bear in mind that it's not just Rust's type system directly that prevents bugs, but also the patterns it enables. For example, the typestate pattern and its use in things like turning misuse of the HTTP protocol into a compile-time error in Hyper.