News for hackers. Stuff that matters.
Re:The relative performance of C and Rust by vnorilo on Friday September 28, 2018 @11:16PM (#18098528)
This was a well-crafted benchmark, but what I found especially interesting and insightful was the "apples to oranges" part: C and Rust encourage distinct approaches, different data structures. I feel it is actually more interesting than implementing the same algorithm and comparing optimizer pipelines.
Re:The relative performance of C and Rust by cryptonector on Saturday September 29, 2018 @12:34AM (#18098729)
I agree. Many of us like it about C that we can build intrusive data structures easily enough. The price is more complex memory management and locking protocols, but in some cases the price is too steep in actual run-time cost. The apples to oranges comparison is not telling about whether Rust produces better code than C, but it shows how Rust's design choices force themselves on the coder and yield better results in the end. When you stay away from unsafe code, Rust makes one write code that is much easier to reason about, especially in a threaded environment. Performance is nice, but the nicer thing is correctness -- being able to get good performance along with correctness, now that is really nice.
Re:The relative performance of C and Rust by TheDong on Saturday September 29, 2018 @12:57AM (#18098783)
I think this is more a result of rust having good generics.

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"

Re:The relative performance of C and Rust by vnorilo on Saturday September 29, 2018 @01:31AM (#18098856)
Right. In C, correctness is by convention, not enforced. C++ allows "userland" enforcement via RAII. There are, however, pieces of compiler magic partially outside the type system (const, &, &&) - you can't specify such behavior within C++. Rust does more of this magic in ownership. If you just want a particular instruction soup, all of them will get you there. It's a question of ergonomics, convenience and the kinds of compiler guarantees that we want.
Re:The relative performance of C and Rust by cryptonector on Saturday September 29, 2018 @03:12AM (#18099108)
C is also expensive in that it's practically impossible to get correctness in large codebases.
Re:The relative performance of C and Rust by zbobet2012 on Saturday September 29, 2018 @04:29AM (#18099299)
The Linux Kernel would beg to differ. I love Rust, I love strong typing, but statements like this are what make people scoff at its evangelist.

People can and do get C right, in safety critical systems, at larges scale, all over the world.

Re:The relative performance of C and Rust by coldtea on Saturday September 29, 2018 @06:17AM (#18099617)
>The Linux Kernel would beg to differ

And it would be wrong in differing. Memory and synchronization bugs the kind of Rust prevents are found in the kernel all the time.

Re:The relative performance of C and Rust by bjourne on Saturday September 29, 2018 @12:44PM (#18101309)
Rust does no such prevention. You are free (and encouraged to) use the unsafe parts of the language for performance sensitive code. A kernel written in Rust would likely have memory leaks and overflow errors too.
Re:The relative performance of C and Rust by staticassertion on Saturday September 29, 2018 @02:11PM (#18101850)
It's all speculative whether a kernel in Rust would or would not have as many bugs as a kernel in C.

As for a kernel just ending up as a bunch of unsafe, doesn't seem grounded in reality.

https://os.phil-opp.com/minimal-rust-kernel/

> Note that there's an unsafe block around all memory writes. The reason is that the Rust compiler can't prove that the raw pointers we create are valid.

[..]

> I want to emphasize that this is not the way we want to do things in Rust! I

[..]

> So we want to minimize the use of unsafe as much as possible. Rust gives us the ability to do this by creating safe abstractions. For example, we could create a VGA buffer type that encapsulates all unsafety and ensures that it is impossible to do anything wrong from the outside. This way, we would only need minimal amounts of unsafe and can be sure that we don't violate memory safety. We will create such a safe VGA buffer abstraction in the next post.

As is always the response to the "A kernel in rust would be just as unsafe as a kernel in C", the goal is to build safe abstractions.

So you have unsafe, plus some constraints that you enforce at a module level, which wraps the unsafety.

This is not perfect - you'll still write vulnerable unsafe code, I'm sure, sometimes. But the rest of your code can be built on top of it, and now your unsafe code can be the main target of your testing and verification.

We have no real rust kernel the size of Linux to compare to but I think there's plenty of reason to believe that it would have considerably fewer vulnerabilities and would make auditing far simpler.

Re:The relative performance of C and Rust by bjourne on Monday October 01, 2018 @10:29AM (#18112798)
Wouldn't the exact same arguments work for C++ too? Modern C++ is just as safe as Rust.
Re:The relative performance of C and Rust by steveklabnik on Monday October 01, 2018 @10:46AM (#18112948)
That’s just not correct.
Re:The relative performance of C and Rust by iopq on Monday October 08, 2018 @09:13AM (#18167059)
Nice try, Stroustrup
Re:The relative performance of C and Rust by lifthrasiir on Saturday September 29, 2018 @01:20PM (#18101556)
> You are free (and encouraged to) use the unsafe parts of the language for performance sensitive code.

The point of the Unsafe Rust is the containment of relative "unsafeness" (that is, however, not equivalent to C/C++'s UB [1]) within a certain boundary. Having less things to audit surely helps.

[1] https://doc.rust-lang.org/nomicon/safe-unsafe-meaning.html

That said, of course Rust is not a panacea for memory and synchronization bugs. Rust helps a lot but does not fully solve logical memory leaks and deadlocks for example.

Re:The relative performance of C and Rust by pjmlp on Saturday September 29, 2018 @07:00AM (#18099746)
On the contrary, you should spend a couple of hours watching the Linux Kernel Security Summit 2018.

Kernel devs are of a different opinion than yours.

Hence the Linux Kernel Self Protection Project initiative.

EDIT: corrected as suggested.

Re:The relative performance of C and Rust by majewsky on Saturday September 29, 2018 @07:09AM (#18099768)
Re:The relative performance of C and Rust by pjmlp on Saturday September 29, 2018 @07:12AM (#18099781)
Thanks
Re:The relative performance of C and Rust by bboreham on Saturday September 29, 2018 @04:38AM (#18099328)
What definition of “correctness” are you using here? Bugs are found and fixed in Linux all the time.
Re:The relative performance of C and Rust by jacquesm on Saturday September 29, 2018 @05:16AM (#18099455)
Bugs would be found in a Rust codebase all the time as well. The difference is that a certain class of bugs is going to be absent from safe usage of Rust. (Unsafe Rust gives you just as much rope to hang yourself with as C).
Re:The relative performance of C and Rust by newacctjhro on Saturday September 29, 2018 @10:05AM (#18100385)
Unsafe Rust has a little less rope than C because there are less cases that can lead to undefined behavior (see https://doc.rust-lang.org/nomicon/what-unsafe-does.html and https://doc.rust-lang.org/reference/behavior-considered-unde...). However, once you trigger UB, it's the same.

By the way, Rust devs are working on a mathematical formalization of those rules. Here's a blog post about a part of this effort: https://www.ralfj.de/blog/2018/07/24/pointers-and-bytes.html. (There are similar formalization efforts for subsets of C, that led to software like the CompCert verified compiler)

Re:The relative performance of C and Rust by quotemstr on Saturday September 29, 2018 @05:10AM (#18099428)
Rust and C may encourage different data structures, but a benchmark of two programs that happen to use two different data structures doesn't demonstrate this difference. To demonstrate what you claim, you'd have to perform a more holistic language analysis than we see in the article, and you'd in practice have to talk about C++ too.
Re:The relative performance of C and Rust by kiriakasis on Saturday September 29, 2018 @03:37PM (#18102336)
That Rust and C use different data structure is not a thesis in the article, it is used as a fact in an explanation. To support this fact the author explain how intrusive data structure are easier in C thanks to a lack of static ownership (for the same reason they are way harder in Rust).

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.

Re:The relative performance of C and Rust by stochastic_monk on Saturday September 29, 2018 @08:57AM (#18100075)
How is Rust encouraging a different data structure besides providing a btree set and not an avl tree set?

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.

Re:The relative performance of C and Rust by kbsletten on Saturday September 29, 2018 @10:42AM (#18100612)
I think he mentioned this briefly in the article. In C it's common to use intrusive data structures like linked lists and AVL trees because that's how the language works, you can embed data structures easily. This wouldn't work with a B tree because it's allocation pattern is very specific. Rust on the other hand is very pro-generics where you can write a B-tree and painlessly have it store whatever anybody wants it to. It's more about what the language makes easy and idiomatic than anything.
Re:The relative performance of C and Rust by bjourne on Saturday September 29, 2018 @12:56PM (#18101407)
The comment is wrong for C, but could be right for C++. The data structures in STL are all implemented as red-black trees. They can't be rewritten as B-trees because of some iterator/synchronization/backwards compatibility mess.

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

Re:The relative performance of C and Rust by stochastic_monk on Saturday September 29, 2018 @01:24PM (#18101580)
There are STL-compatible B-tree implementations in C++, so I would still use one of those instead of std::map. Besides, std::*map implementations are all rather slow.
Re:The relative performance of C and Rust by marcosdumay on Saturday September 29, 2018 @10:25AM (#18100513)
Rust encourages the creation of more generic code, that makes switching into non-DIY more complex data structures easier. The difference is even larger after the code is written and you are able to profile it.
Re:The relative performance of C and Rust by stochastic_monk on Saturday September 29, 2018 @10:36AM (#18100572)
Then Heng Li’s kbtree.h from klib is your generic, fast implementation. I’d love to see this repeated using it for a fair comparison.

For example, the benchmark shootout recommends his khash for a hash table.

Re:The relative performance of C and Rust by marcosdumay on Saturday September 29, 2018 @01:08PM (#18101479)
The point is entirely that it is harder to take a typical C code and change it to use something like this than it is to do the same in Rust. Much harder.

I don't think anybody will find any consistent performance enhancement in Rust that can not be replicated in C. The problem is how to run it out on real code.

Re:The relative performance of C and Rust by stochastic_monk on Saturday September 29, 2018 @01:47PM (#18101711)
That’s fair. klib (I assume) was not easy to develop, but it easy to use.
Re:The relative performance of C and Rust by marcosdumay on Saturday September 29, 2018 @10:59PM (#18104221)
It's easy to use. What is not easy is to take a large code-base that doesn't use it and change it so it now uses.
Re:The relative performance of C and Rust by ummonk on Saturday September 29, 2018 @10:03AM (#18100377)
Yes, especially when b trees tend to be easier to implement too.
Re:The relative performance of C and Rust by vmchale on Saturday September 29, 2018 @02:51AM (#18099062)
Agreed. Comparing compiler pipelines is a poor way to judge how your code will perform in practice, and it's even not that useful for compiler authors, where optimizations that matter in practice are not the same.