News for hackers. Stuff that matters.
Re:Why is Rust slightly slower than C? by andolanra on Wednesday September 11, 2019 @08:13PM (#20946193)
Others have pointed out that Rust's type-checking is all done at compile time. Rust also does not have exception handling.

To be more specific: errors in Rust are either propagated by function return values (usually in the form of `Result` or `Option` types) which do not require any kind of runtime support, or by panicking, which unwinds the stack for clearing-up of resources and then halts the running thread. (Panics cannot be caught, although they can [EDIT: sometimes but not always; see burntsushi's comment below] be observed by a parent thread if they have occurred in a child thread.) Rust's "runtime", therefore is negligible, much like C's: it consists mainly of functions which can be called for things like stack unwinding or backtrace support.

Re:Why is Rust slightly slower than C? by burntsushi on Wednesday September 11, 2019 @08:30PM (#20946297)
The thread boundary restriction for catching panics was lifted quite a while ago with std::panic::catch_unwind: https://doc.rust-lang.org/std/panic/fn.catch_unwind.html

Do note the caveats in the docs though. It isn't guaranteed to catch all panics. Indeed, unwinding can be disabled completely with a compiler option (instead, panics will abort).

Re:Why is Rust slightly slower than C? by jadbox on Wednesday September 11, 2019 @09:13PM (#20946629)
AFAIK, some Rust types are checked at runtime. For example, RefCell:

"Because RefCell<T> allows mutable borrows checked at runtime, you can mutate the value inside the RefCell<T> even when the RefCell<T> is immutable."

*source https://doc.rust-lang.org/book/ch15-05-interior-mutability.h...

EDIT: Sorry, I don't mean 'runtime type checks' are slowing down Rust, but rather that Rust performs more general runtime safety checks (like RefCell).

Re:Why is Rust slightly slower than C? by pknopf on Wednesday September 11, 2019 @09:37PM (#20946829)
That isn't "type checking", that is just a guard to prevent simultaneous writes to the same data.
Re:Why is Rust slightly slower than C? by jadbox on Wednesday September 11, 2019 @09:52PM (#20946951)
You're correct- what I had meant to say is Rust is smarter with more runtime checks over C for several cases, like RefCell. Therefor, Rust can be slower than C because of safety (but not because of type checking itself).
Re:Why is Rust slightly slower than C? by pknopf on Thursday September 12, 2019 @12:07AM (#20947862)
RefCell has an internal semaphore. It's used specifically for multi threaded scenarios.

If someone is writing a multi threaded C app, they will likely be using semaphores as well. At least, they should be. Rust just enforces it.

So, I wouldn't say rust is "slower" in the regard.

Re:Why is Rust slightly slower than C? by PrototypeNM1 on Thursday September 12, 2019 @04:12AM (#20949160)
Iirc RefCell is marked !Sync, I thought Mutex was the multithreadong analog?
Re:Why is Rust slightly slower than C? by steveklabnik on Thursday September 12, 2019 @08:50AM (#20950820)
That's correct.