5 ms·
In Rust, ownership is a compile-time issue. This puts constraints on how you can use references, how ownership is passed around and so on. This programming mode
by cletus 6y ago
In Rust, ownership is a compile-time issue. This puts constraints on how you can use references, how ownership is passed around and so on. This programming model has a cost to the user and is probably inherently slower to compile just because the compiler has to do more.
But at runtime it's essentially free.
C++ on the other hand added a set of primitives starting in C++11 to be explicit about ownership through std::unique_ptr and such. These have a runtime cost and ultimately mean getting locks. I've seen estimates of this cost in the single digit microseconds. Not exactly super-expensive but not free either.
There are simply some assumptions Rust can make that C++ can't because certain things aren't possible in Rust.
Another example: Rust doesn't have exceptions. Over the years I personally have some to believe that exceptions are largely a false economy (particularly the failed experiment that is/was checked exceptions in Java).
Instead Rust has match expressions and enumerated types (with state!) that force you to deal with return types. This BTW is very much what Google C++ looks like. It's even debated if you can ever write truly exception-safe C++ code.
This is another problem Rust doesn't have by virtue of not having the feature.
Are you starting to see the pattern? C++ has everything in it. That comes at a cost and there's no getting around that.
- quietbritishjim 6y ago> ... std::unique_ptr and such. These have a runtime cost and ultimately mean getting locks. I've seen estimates of this cost in the single digit microseconds. You're no doubt thinking of shared_ptr (which I guess is what you meant by "and such"). That has a reference count, which does not lock but does use an atomic counter with the usual costs that involves. unique_ptr has no such thing and is essentially free.
- eska 6y agoIt's not essentially free https://m.youtube.com/watch?v=rHIkrotSwcc https://m.youtube.com/watch?v=rHIkrotSwcc 17:40
- quietbritishjim 6y agoI'd rather you'd just stated your objection instead of making me watch a bit of video. Thanks for at least including a time stamp. For the benefit of others: Using a std::unique_ptr adds exception handling code to the functions that use it to guarantee cleanup when exceptions are thrown. Of course, that extra code is not actually executed if the exception is not thrown. I was responding to a comment that claimed that std::unique_ptr used locks to transfer ownership, and my objection to that is not disputed by what your saying. You're making the point that exception-safe code is less efficient than exception-unsafe code. I do not know enough about that topic to comment on that matter, I was never trying to say anything about it in my previous comment. But I would like to point out that Rust has an analogue of exceptions: panics (although you can change their behaviour to abort() instead of unwind). So it must have something similar to unique_ptr's unwind handler anyway.
- eska 6y agoNah, you missed the main point, it doesn't deal with exceptions. But since you misuse the downvote feature after I looked for the video and timestamp for you, I guess you don't want to be proven wrong anyway. Have a nice day.
- quietbritishjim 6y agoYou can't downvote direct replies to your comments, you'll notice the downvote button is missing for you on this comment of mine. That must be someone(s) else. What do you mean by "it" in "it doesn't deal with exceptions"? Rust? Or the C++ snippet on the left hand side in the presentation with the shorter assembly output? Edit: Since I'm still apparently not clear on your main point, this might be irrelevant, but here [1] is a Godbolt link showing that Rust functions have similar panic-safe unwind code to the C++ exception unwind code shown in the presentation. Admittedly the Rust version does seem a bit shorter (comparing just the actual function, at the bottom of the Godbolt output) and I'm afraid I don't know enough assembly to be able to analyse the difference. But I think the principle is similar. [1] https://godbolt.org/z/K1xPr3 https://godbolt.org/z/K1xPr3
- deleted 6y ago[deleted]