326 points · by dmpk2k · 8y ago · 148 comments
If c were built with good generics and memory management (not that garbage preprocessor stuff), I expect similarly expressive and good data-structures would be built.
C++ is an affirmation of this because it happens to have good libraries of data-structures which are not really very different from rust's.
I don't think this is as much "rust's design forces the coder to do this" and more "c makes it hard to do as nice of a design"
People can and do get C right, in safety critical systems, at larges scale, all over the world.
To demonstrate the claim that C and Rust have different idiomatic data structures you need to rewrite software from C to Rust and to Rust from C and see what is hard and what is less hard.
The C stdlib does not come with an AVL tree. Why would one not implement a b-tree in C for this comparison? It’s not as if K&R has “Use binary, not b-trees!” on the front cover.
I once did a similar benchmark to the one in the article and found that Rust outperformed C++ because the C++ code was using the builtin std::map. But for reeeeeeally big datasets PostgreSQL beats them both so who cares. :p
For example, the benchmark shootout recommends his khash for a hash table.
A person should understand that no language(programming or otherwise) is perfect. Therefore there is some criticism of any language that is valid. Therefore some criticism should be taken seriously as there may be a real point to the criticism.
There are many posts like this, where there are comparisons of Rust to C. Realistically they are all a little biased towards Rust as Rust is a C++ replacement really. A more proper comparison would be Zig or D as a better C against C.
Just understand that a person defending C is not always an idiot, and maybe they have point. Consider the excessive memory use of any working Rust compiler. That will probably not be remedied anytime soon and is a legitimate complaint. The ideal of how something could be is not how something is. The reality is that C works pretty well most of the time, Rust works well most of the time. They both fail at some things.
You see a lot of people holding up Rust precisely because we've been there for the last 5/10/15/20 years. The day I don't have to write a makefile or build yet another CMakeLists.txt is the day I rejoice.
What Rust offers is another option in the native, non-GC'd language space. A space that has very few languages and even fewer yet that are shipped at scale. Rusts inclusion w/ FF means that the have to address the robustness, security, performance and usability of the language to a degree that you don't commonly see.
Having just blown 4+ hours today dealing with the linker on a mixed C/C++ project I don't really miss a lot of the baggage that comes with native development these days. Rust gives you the option of dropping down to that level while still preserving a set of sane, opinionated defaults that are pretty well thought out.
https://github.com/elrnv/ipopt-rs/blob/master/ipopt-sys/buil...
Others just assume that the libraries are there, but they still require a program to compile them:
https://github.com/cmr/openblas-src/blob/master/build.rs
As long as all of the dependencies are in Rust, things appear nice. At the same time, unless I'm missing something, it seems like crates manages multilanguage projects poorly. Though I have some major gripes with CMake, I've managed multilanguage projects mostly well.
Is there a sane way outside of CMake to manage a multilanguage project with Rust?
Ironically, cargo is one of the areas the rust ecosystem falls down completely. Not to mention, there's no ABI...
> What Rust offers is another option in the native, non-GC'd language space.
True, and while this is great in principle for embedded development, not using a GC is overrated.
> Rust gives you the option of dropping down to that level
Well, if you're not dealing with the linker, you're not at "that level"
well, which language of C and C++ has the possibility of having traits, structs with function definitions, generics which work through monomorphism, variants, optional, reference-counted pointer and pointer ownership semantics ?
> If you tracked communities so far, you'd see that Rust community is very sensitive on the issue of memory print, performance impact and asm implicitness of Rust.
yeah... like C++ ?
ATS is far closer and indeed compiles to C. Rust's type system is still less expressive than that of ATS.
I think they've also figured out that with time C will be squeezed into smaller and smaller areas of use, as is already happening.
Writing robust code hasn't really found a space to live in the mental model of C programmers. Using C outside a kernel or existing C project today is professional negligence.
if you think like this, you will never replace C...
> Using C outside a kernel or existing C project today is professional negligence.
or embedded development lol
Here's a silly prediction: in the next 10 years a significant number of C programmers will retire without replacement. The new generations will rather learn Go, C++ and Rust for low-level stuff and will banish C to pure legacy projects.
Every body whole who creates a new language would like to see it more and more used thus replacing others.
Just one tiny gripe, don't use a blue and green next to each other in graphs if avoidable. I'm not even blue green colorblind, and I found them hard to distinguish.
Thanks for the great read.
My father can see the two colours just fine, but if they're adjacent to each other and about the same kind of tone, they start to blur into grey.
One particular example I remember growing up. He hated the board game "Game of Life" because there's all these greens (ground) and blues (rivers, lakes, ocean etc) right next to each other all over the board. It was not a pleasant experience to be looking at it.
All this said, you may be correct that there's no such thing as blue-green color-blind, and maybe that's not the best word to describe what I'm trying to say.
I'm not sure that comparing "Rust" and "C" performance is even meaningful. The latter is a multi-implementation language, and in most implementations, can be written in a way that gets the compiler to generate the same machine code that the Rust compiler does. (Frequently, they both just go through LLVM.)
when comparing languages you need to include a measure of how idiomatic your programs are.
Of course it is entirely subjective (the author argues for how that is due to a static vs dynamic ownership model) and there is a single data point, which is the author rewriting the code. (two is you consider when he/she swaps one of the data structure)
Then it's not a performance benchmark, it's a style and usability benchmark.
On instance of this becoming a complication would be in the GTK+ world with the Rust bindings to GObject. There's some really good articles about their adventures in that realm you could find.
Meanwhile Rust, in this user's opinion, offers a superior toolset for modeling data and building behavior, namely: sum types and traits.
So if you're looking to extend or integrate with a C codebase, barring some exotic target platform and all else equal, I don't know why you'd opt for C++ over Rust.
where "as much work" means putting `extern "C"` in front of your functions and having a few `static_assert(is_pod_v<type_that_i_pass_in_argument_to_my_c_api>)` or only using opaque typedefs, ie it frankly amounts to nothing
If you are writing C++ in such a way that this does not matter, then you are basically writing C already and `extern C` is all you have to do though, but then you might as well just have been using C this whole time.
But when your API uses templates/generics and other types that C can't comprehend, you need to write additional wrapper functions operating behind opaque C types. It's not rocket science, and tooling/macros can help a lot, but it's still tedious and error prone (you're erasing types, redoing error handling, adding manual memory management, etc.).
OTOH interfacing with C++ is very hard. D might be the only language that has seriously tried it, but from Rust's perspective C++ has largely overlapping, but incompatible set of features, and it's a weird dead-end.
That didn't happen. The data structure difference is discussed and the possibility of the performance difference being down to this is clearly acknowledged. Also discussed is the reason for the different data structures, which is actually the most important take-away.
I agree; this would have been a great stand-alone piece had it focused on this instead of performance.
It's one of the few high-quality technical articles that pass through HN... if you don't get hung up on the title.
Normally, "read the article" is a bit not-HN-friendly, but when you call someone out as disingenuous because they didn't say things that they in fact do clearly call out and have a multi-paragraph discussion of, you are earning a "please read the article" response. They're not being disingenuous about what they are doing if they clearly explain what that is and then have a (IMHO) balanced pro-and-con discussion about it.
Your statement that they simply are "chalking this up to language differences" is false. It's not surprising that you received a negative reaction.
I can't speak for the author but at my work we found -O2 to give better results on our OpenGL ES rendering engine.
cargo build —release —verbose would tell you for sure; I’m not at a computer right now.
There are a lot of good books from these eras, and there are a lot of good books that teach with the uniform memory model. Most algorithms classes don't talk about the hierarchy at all (even though it distorts the true asymptotic runtime of many algorithms).
A silly idea I had, wonder if it would be possibly to auto-adjust the B factor of a BTree (the number of nodes) at runtime based the cache line size.
The point really is that Rust foists better design choices on the programmer.
1. Storing an array and its length together and always checking it correctly such that you don't have a buffer overrun.
2. Freeing memory, not leaking memory.
3. Using a union correctly, e.g. always accessing the right variant.
4. Printing unicode characters.
5. Macros which dont' conflict with their scope (are hygenic)
All of those problems seem like they should be easy, are incredibly easy in rust, and are not fully solved problems in C.
I don't think rust makes easy problems harder just because its type-system and macro system and such are all so good compared to C.
It turns out having proper sum types instead of C's garbage unions, and having proper string support, and so on make easy problems easy too!
https://github.com/attractivechaos/klib/blob/master/kbtree.h
https://old.reddit.com/r/programming/comments/9jsqb3/the_rel...
Isn't rust based on LLVM?
It would make more sense, for a true apples to apples comparison, to use clang instead of GCC.
Not that I'm pro-C or anti-rust or anything but the beginning of the article goes on about previous tests not being fair.
At the end the author never states that one is faster (yes, the title is misleading, but it is then clearly clarified in the text) the main part is the discussion about intrusive data structures.