3 ms·
Is there any room for a form of C with immutable semantics which forced you to jump through special hoops to get mutable data access (solely for performance rea
by pmarreck 10y ago
Is there any room for a form of C with immutable semantics which forced you to jump through special hoops to get mutable data access (solely for performance reasons)... Or does Rust basically have this design goal already?
The limitation of programming (from a bug-minimization perspective) is the programmer's ability to understand all the possible states his code and data can get into which means that the key to bug and security hole elimination is any pattern which reduces cognitive load
- pjc50 10y agoWell, you can sort of achieve that by writing C for a C++ compiler and sprinkling it with 'const'. Use of the standard library can then be inconvenient in places.
- saghm 10y agoI'm not sure I'd characterize it as "jumping through hoops", but Rust does lean strongly towards preferring immutabillity. Variables are immutable unless you give them the "mut" keyword when you declare them, and in cases where you need multiple pointers to the same data, it's impossible (outside of `unsafe` blocks, of course) to make one of them mutable unless you use a safe wrapper type like a`Cell` or `RefCell` (for single-threaded cases) or a `Mutex` or `RwLock` (for when you need to share access across multiple threads). But yeah, there's a strong bias towards immutability in Rust, although from what I understand this is more for safety than performance reasons.
- sidlls 10y agoI'm struggling to understand how requiring explicit mutability has anything to do with performance or "hoops." I mean, getting mutable access is trivial: let mut x = ... fn frobnicate(x: &mut...) The performance "problems" in Rust requiring "hoops" are in very narrow applications (e.g. certain applications of high performance numerical computing). I'm not a fan of the "unsafe-offset" idiom required so that every time access into a vector occurs doesn't waste cycles on a pointless boundary check (for example). But I must say for a given chunk of Rust code the equivalent C++ code is generally more bloated, because unless you're writing code begging for abuse you're going to have "const" and move operations everywhere, which is sort of inverted from Rust's model.
- steveklabnik 10y agoBounds checks are often elided, so many access of vectors won't waste cycles on pointless checks. Or at least, if they are truly pointless, and they're not elided, that's an LLVM bug.
- ArkyBeagle 10y agoI don't write huge systems with dozens of other people. So I can get away with basically "almost everything is a state machine" in C. The parts that are not a state machine are encoded patterns that are matched against in tables, for simple command parsing and the like ( for when an fsm is overkill ). The only cognitive load is generating test vectors, and I use scripting languages for that.