I'm not asking whether or not their approaches are different, but whether or not they're trying to solve the same problem(s). I get the sense that this is the case; they're both trying to be a "safe C(++)".
I'm not asking whether or not their approaches are different, but whether or not they're trying to solve the same problem(s). I get the sense that this is the case; they're both trying to be a "safe C(++)".
Can you elaborate as to other ways Rust has worse performance?
Could've sworn I've read somewhere the Rust maintainers stated that Rust will never have a switch to disable bounds checking. Has this changed?
> Can you elaborate as to other ways Rust has worse performance?
I don't know of any other specifics, but benchmarks usually show D outperform Rust (and often enough, everything else), e.g.:
https://togototo.wordpress.com/2013/07/23/benchmarking-level...
https://mail.mozilla.org/pipermail/rust-dev/2014-March/00921...
In some cases, bounds checking doesn't make sense, e.g. in video games running on consoles.
As if it couldn't be used to exploit and pirate the games, get extra lifes, buy in-game items without paying, automate bots...
"Many years later we asked our customers whether they wished us to provide an option to switch off these checks in the interests of efficiency on production runs. Unanimously, they urged us not to--they already knew how frequently subscript errors occur on production runs where failure to detect them could be disastrous. I note with fear and horror that even in 1980, language designers and users have not learned this lesson. In any respectable branch of engineering, failure to observe such elementary precautions would have long been against the law."
- C.A.R Hoare Turing Award speech, 1980
> automate bots...
This has nothing to do with memory safety.
That is the key issue, "given a choice".
I saw game developers being dragged into accepting C was replacing their beautiful Assembly, followed by game developers being dragged into accepting C++ was replacing their beautiful C.
You can be pretty sure if tomorrow Sony, Apple, Nintendo, Google or Microsoft said "you must use language X", they would do it.
So yeah, given the choice they can continue to lose money thanks to memory corruption exploits.
Signed code and NX don't protect against memory corruption. One can use ROP to work around it.
Of all the languages used to implement desktop and server software (these days, at least), C is pretty much unique in that it makes no effort whatsoever to help the programmer avoid or uncover these errors.
And don't get me wrong, I pretty much like C, even though I do not use it very often, but if there was one thing I could change about it, it is this. Given the amount of bugs that stem from this property of the language, I guess I am not alone. ;-)
Not having a switch to disable bounds checking is not the same as not being able to disable bounds checking.
> I don't know of any other specifics, but benchmarks usually show D outperform Rust (and often enough, everything else), e.g.:
It's been years since I saw those benchmarks. Those are benchmarks of the built-in library random number generator. Rust uses a more secure random number generator by default than most of the other languages, so it will go slower. If you really want to benchmark random number generation, pick a faster but less secure algorithm.
Not sure what you're getting at. Marking the entire program as unsafe is not realistic. Marking specific portions leaves a long tail of code that "rarely" runs, but still adds up.
> Those are benchmarks of the built-in library random number generator.
The levgen-benchmarks programs implement the XOR-shift RNG in both the Rust and D versions.
No, it doesn't. Bounds checks in cold code don't matter (and there aren't many to begin with). Even bounds checks in hot code frequently don't matter. Have you profiled Rust code and found bounds checks to be a problem?
> The levgen-benchmarks programs implement the XOR-shift RNG in both the Rust and D versions.
Oh, I see that they changed it. Still, that benchmark is from 2013 and doesn't compile anymore. It predates unboxed closures and many other performance improvements. And 15% is in the "different register allocation decisions will affect the outcome" range, where comparisons are fairly meaningless. The fact that D and Rust beat C doesn't mean that they're faster than C.
I've measured some D code yesterday and it was like you said, bounds check didn't change performance which was very suprising (this was for JPEG loading).
In reply to https://news.ycombinator.com/item?id=10139287 :
> No, it doesn't. Bounds checks in cold code don't matter (and there aren't many to begin with).
OK, I trust your expertise on this. The "long tail" applies to optimizations in general. I don't know why the benchmarks are slower for Rust then, could be the difference in backends.
> The fact that D and Rust beat C doesn't mean that they're faster than C.
I don't know what's going on there, but some languages allow writing fast code easier than in C, e.g. SIMD vector array operations.
In any case, when comparing language performance, it's generally good to avoid comparing backend performance, i.e. focus on the LLVM-based compilers in the pnoise benchmark. A GCC-based Rust compiler would almost certainly outperform the current LLVM-based one for that benchmark (as suggested by gcc vs. clang and gdc vs. ldc2). Of course, such a compiler doesn't currently exist, so if you care about getting your code to run as fast as possible right now, Rust isn't the best choice based on that benchmark, but Rust has several reasons that it's likely to outperform both C and D by default:
- better aliasing information (than both C and D), so compilers can optimise more aggressively
- lifetimes mean more aggressive stack allocation/ownership patterns can be used without risk (a bonus over C, and something that D solves via GC, which has its own upsides & downsides)
- the type system has been designed to be particularly good at concurrency & parallelism, so again this can be used more aggressively and in more situations (e.g. libraries can expose interfaces that allow mutating data directly on the stack of other threads without risk of data races or dangling pointers)
Instead of iterating over a range of indices and indexing, use the slice iterator, it's basically a drop-in replacement. The former can either be done with a manual loop & addition (which is not at all lazy and definitely can't be returned, i.e. Rust's current iterators are strictly more flexible), or it is done with the Range iterator. The two lazy forms are basically the same:
let v: &[T] = ...;
let x: iter::Map<ops::Range<usize>, _> = (0..n).map(|i| {
let elem = &v[i];
// do stuff
})
let y: iter::Map<slice::Iter<T>, _> = v.iter().map(|elem| {
// do stuff
});Being pragmatic is a very questionable claim. Its exact meaning varies from time to time, and a property that a certain people consider to be pragmatic can be considered to be not pragmatic by another people. It's mostly meaningless.
For example, for embedded devices D is a no-go from the beginning. I'm not just saying the reliance on GC. D has a few ridiculous behavior that may cost significantly on the low resource devices, such as http://forum.dlang.org/thread/mr6bl7$26f5$1@digitalmars.com D is never pragmatic in this area. But of course, D may be pragmatic in other areas.
> expressive
D and Rust have different kinds of expressiveness. While D has much better compile-time metaprogramming, how do you express affine type in D? In Rust affine type is very fundamental and we can utilize it to define the exact meaning of our programs.
> and generally has better performance (e.g. bounds checking is optional).
What? How can a GC'ed language be faster than a non-GC'ed language? Besides simple numeric evaluation, D is generally more heap-allocation-heavy than Rust, so it can never beat Rust. After all, there's no point in Rust if it's slower than a GC'ed language. The benchmarks you linked are questionable too, as their results say D is faster than C. Are you really sure D can outperform C in general cases?
I would be interested in benchmarks showing that. Do you have any?
A GC might only encourage you to use more heap allocations.
I disagree that it is meaningless. It's simply subjective. I've used D since 2006 and tried most new programming languages that appeared since, and it seems like they all tried to solve a specific problem (e.g. Go - concurrency, Rust - memory safety) while giving less attention to everything else.
> For example, for embedded devices D is a no-go from the beginning.
Existing D projects contradict that. They simply don't use the standard D runtime when it's not appropriate. Alternative runtimes exist. Rust users do the same thing: http://mainisusuallyafunction.blogspot.com/2015/01/151-byte-...
> D and Rust have different kinds of expressiveness. While D has much better compile-time metaprogramming, how do you express affine type in D? In Rust affine type is very fundamental and we can utilize it to define the exact meaning of our programs.
Can you provide a good example of affine types and how they help with expressiveness?
My view of expressiveness in D does not stop at compile-time metaprogramming. For example, here's an excerpt from one of my programs:
http://dump.thecybershadow.net/fb6c5a080c7a42ecd8d1f062b58a5...
> How can a GC'ed language be faster than a non-GC'ed language?
It is entirely plausible. For example, deallocation/destruction can run in a background thread, heap compaction removes heap fragmentation and improves cache locality, and of course throwing away all memory after the program is done executing is very practical if the program would otherwise do little deallocating overall.
> Are you really sure D can outperform C in general cases?
I don't know what's going on there, but "higher-level" languages outperforming C is not that uncommon, for example due to stricter aliasing rules or SIMD array vector operations, or simply due to language or standard library facilities being readily available (e.g. STL or associative arrays) which would be cumbersome or impractical to write/maintain in C versions.
For example, let's say there's an HTTP server handler that must send each header line first, and finally send a body. So you should:
conn.send_header("HTTP/1.1 200 OK");
conn.send_header("Connection: Close");
conn.send_header("Content-type: text/html");
conn.send_body("Hello, world!");
In Rust, you can construct the API so that you cannot call `send_body` before any occurrences of `send_header`. So if you do: conn.send_body("I'm sent first!");
conn.send_header("Content-type: text/html");
you will get a compile error. This greatly reduces the risk of having incorrect logic in your program. In this case Rust helps ensuring our intention: `send_body` can only be the last call of the request handler. I consider this a good example of expressiveness of Rust (and affine types).Unfortunately I haven't seen any comprehensive article that discusses affine types in terms of Rust. So I can't recommend a link to you, instead I'll try to explain them in my own words.
Affine types are those whose values can only be used at most once. They are implemented in Rust as ownership and borrowing. In my example above, `send_header` borrows the `conn` variable, whereas `send_body` takes the ownership of the `conn` variable. As you lost the ownership of `conn`, you cannot use it anymore, thus resulting in a compile error.
This is similar to C++'s move semantics, where you are not permitted to move the same value more than once. It is just that in C++ "move only once" is a convention, whereas in Rust it is enforced by the type system.
In Rust, if you pass an argument by value, you're automatically using affine types. You cannot use the passed variable anymore. To "borrow" instead, you need to opt-out using references. So, the hypothetical API I mentioned above would be written as:
fn send_header(&self, text: &str) { ... }
fn send_body(self, text: &str) { ... }
Note the difference of `&`.Finally, all of these aren't just hypothetical. The exactly same design is used in Hyper[1], which is an HTTP library, and in nickel[2], which is a web framework in Rust. If you look at the API doc you will see there is no '&' in the self parameter. That means it's passed by value, using affine types, and the ownership is transferred, so you cannot use the original variable anymore.
I hope this helps.
[1] http://hyper.rs/hyper/hyper/client/struct.RequestBuilder.htm...
[2] http://docs.nickel.rs/nickel/struct.Response.html#method.sen...
Why should a GC'ed language be slower than a non-GC'ed language?
The evidence [0,1,2] seems more like: Given enough memory, GC is faster.
Non-GC means explicit deterministic free at specific points in the source code. A garbage collector has the freedom to delay the free, which means potential for optimization.
[0] https://people.cs.umass.edu/~emery/pubs/gcvsmalloc.pdf [1] http://www.cs.princeton.edu/~appel/papers/cmljava.html [2] http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.14.1...
You really have to take Appel papers with a large grain of salt in 2015. Caches and multithreading have come to dominate nowadays, and assuming that all memory access is equal isn't reasonable.
Also, the first paper doesn't make the claim that GC is faster than manual memory management—in fact, manual memory management was faster.
> A garbage collector has the freedom to delay the free, which means potential for optimization.
People commonly say this, but I don't see how a malloc couldn't do the same thing if it actually helped. Malloc implementations typically don't do delayed reclamation because it doesn't help; if it did help, they would do it. But cache is king, and prompt reuse of space is most important nowadays.
I'm just repeating a critique that I read, I haven't evaluated them against one another.
Having a GC excludes you from some programming domains -- well controlled latency, bootstrapping/operating systems.
- The language has the @nogc attribute to ensure at compile time that the GC is not used in a function or an entire module.
- The number of standard library functions that have the @nogc attribute is increasing by the minute. :)
- There is some effort to port Sociomantic's proprietary multi-threaded GC to D (theirs was written for D1).
Oberon, Modula-3, Singularity designers don't share the same opinion
Even without the GC, D is still a good "better-C" language. The GC didn't stop people writing bare-metal / operating system kernels in D:
https://github.com/JinShil/D_Runtime_ARM_Cortex-M_study/wiki/1.1---Hello,-World!
https://github.com/xomboverlord/xombNote that the safety benefits must of course be weighed against the number of moving parts in the type system you desire for your project.
Of course D is interested in those concepts, simply not to the extent that Rust is. For example, D has:
- pure functions
- opt-in strict memory safety (@safe)
- sealed references (http://wiki.dlang.org/DIP25)
- transitive immutability
- shared types