Rust Faster Than C? Not So Fast…
dennisforbes.ca
dennisforbes.ca
At a macro scale, I have no doubts that Rust's benefits will win out. Understandable abstractions, fearless concurrency, etc. At a micro level, I would be surprised if some ugly, reprehensible C wasn't faster.
I think Rust's approach to systems programming is superior in many ways. I just won't be surprised if it loses a microbenchmark.
While this is also true (there are certainly correct programs that aren't writable in rust without unsafe{}), in this context I'm guessing you mean "incorrect programs".
The same is true of C; some ASM programs can't be transcribed in a way that pleases a C compiler.
I didn't say that Rice's theorem wasn't true. I said that in this case it doesn't matter. If program fails to compile, you don't give up because of Rice's theorem, you modify program to make it compile.
Will it make program less efficient? Who knows. There's no theorem about that. Most likely it will.
Then again, I don't really believe that the optimization opportunities are likely to ever matter much. At least not at this level. Perhaps for much more complicated things, e.g. a kind of compile-time sql or string-matcher the greater safety and the symbolic nature of the macros will let programmers write libraries that practically reason about instructions you give them and choose optimal strategies where C would need to do so at run time or with unmaintainable and unusable macros, but if it's just aliasing and some threading gains - well, those aren't huge often, and where they are, C can use em unsafely too (and that's often quite doable too!).
Safety and maintainability sound like more realistic selling points to me - it may never actually beat C in real-world performance; but it doesn't have to - it just needs to come close.
When performance really matters, seasoned C programmers more often write task-specific implementations or choose macro-based libraries. In this particular benchmark, the C implementation is using a macro-based hash table which does not have the penalty of void*.
< #define __ac_isempty(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&2)
< #define __ac_isdel(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&1)
< #define __ac_iseither(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&3)
< #define __ac_set_isdel_false(flag, i) (flag[i>>4]&=~(1ul<<((i&0xfU)<<1)))
< #define __ac_set_isempty_false(flag, i) (flag[i>>4]&=~(2ul<<((i&0xfU)<<1)))
< #define __ac_set_isboth_false(flag, i) (flag[i>>4]&=~(3ul<<((i&0xfU)<<1)))
< #define __ac_set_isdel_true(flag, i) (flag[i>>4]|=1ul<<((i&0xfU)<<1))
---
> #define __ac_isempty(flag, i) (flag[i]&2)
> #define __ac_isdel(flag, i) (flag[i]&1)
> #define __ac_iseither(flag, i) (flag[i]&3)
> #define __ac_set_isdel_false(flag, i) (flag[i]&=~1)
> #define __ac_set_isempty_false(flag, i) (flag[i]&=~2)
> #define __ac_set_isboth_false(flag, i) (flag[i]=0)
> #define __ac_set_isdel_true(flag, i) (flag[i]|=1)This particular example was a bit of a pain point for Rust, because (IIRC) the rules were such that Rust needed to use its default hash function in its hash table, but the C implementation could optimize for the data that the example uses.
It's not an argument to be used against Rust; people who understand programming know this already. It's an argument to be used against Rust's fanboys, who are especially numerous for this language.
Since Sep 10 2016 the Rust k-nucleotide #2 program has been shown using FnvHasher instead of the default hash function.
http://benchmarksgame.alioth.debian.org/u64q/program.php?tes...
In practice, it rarely happens even in micro-benchmarks like these. Though pointer aliasing rules are a special pain point. If you're sure you know what you're doing, restrict exists as a band-aid over this. Yay archaic language baggage!
LLVM's optimizer also doesn't help, as the post mentions—I have real-world code that runs at 1/3rd the speed just from compiling with clang instead of gcc. I love LLVM for what it's let people build on top of it, but it isn't remotely state-of-the-art in this department.
I used to believe this, but I think the same level of theory that says a JIT can be sufficiently smart enough to figure everything out means that a compiler can be sufficiently smart enough to figure everything out as well. This is one of those areas that theory and practice are so far apart that theory should be accompanied with a context of "some day, in the future, if we're lucky..."
What we've seen in reality is that as JIT engines get better, compilers also get better, and retain their lead.
In theory your could still benefit from JIT if, say, you have a Java application that's half written in XML config files and always heavily customized for each deployment. But probably that application also needs 8 modern cores and 16GB RAM to start for reasons that aren't even Java's fault.
But I try not to write code like that.
If the compiler is allowed to embed a JIT or other runtime system, then all bets are off.
I don't really buy the whole safety issue. Granted, fighting against unsafe programming is hard, but I will always prefer a language that is easy to learn and lets you do mistakes so you can just fix them.
[1] shameless plug: https://github.com/duneroadrunner/SaferCPlusPlus-BenchmarksG...
Fortunately, we don't have too. We can always a) just write NEW stuff in Rust, or b) just use Rust to extend and piecemeal replace old apps (since it supports linking to C and/or C++ libs).
Yes, Rust's FFI support is nice, but rewriting piecemeal is still rewriting. (And retesting.) It should be faster and easier to just replace unsafe elements in existing code. Of course, that doesn't conflict with choosing Rust for new components and rewrites of existing components when the time and resources are available.
Btw, I haven't investigated lately, what is the state of Rust interoperability with C++ interfaces?
Not that I'm an expert on the matter, I think it's still through C interop both directions. There was a github project for a Rust module submitted to HN recently[1] to correctly deal with C++ name mangling, and I think it was mentioned that the plan is for it to eventually be used as part of the future C++ interop plans, but I could be misremembering.
Rust might be safer, but it really lacks entry. If we could just have something a little simpler than C++ and with nicer features than C, it would be great.
If microsoft adopts D, I would be so happy.