4 ms·
The reason why Rust and other languages have bounds checking that approaches C’s speed is less to do with the compiler being able to prove away bounds checks an
by pizlonator 3y ago
The reason why Rust and other languages have bounds checking that approaches C’s speed is less to do with the compiler being able to prove away bounds checks and more to do with those languages making the bounds easy to carry around.
Bounds checks themselves aren’t that expensive, if you already have the length to check against in a register or some other convenient spot.
And - if the bounds are convenient to find and known to be immutable then it does make it easier for the compiler to eliminate those checks using the same techniques it would use to eliminate all kinds of redundancies. C has many redundancies that we don’t have to think about because they’re straightforward for the compiler to reason about.
- ninepoints 3y agoYou'll have to enlighten me on how Rust makes passing bounds any easier. Spans and views are pretty common in C++ and C codebases these days.
- steveklabnik 3y agoLike many things in Rust vs C/C++ comparisons, it’s about defaults: Rust has dedicated syntax and everyone uses it, in C and C++ it’s more manual and not everyone does.
- pizlonator 3y agoSpans and views make it easier, too. But C/C++ pointers don’t. You can add bounds checking to all C/C++ pointers and there are many projects that do that (CCured, SoftBound, CHERI, Fil-C, CheckedC, -fbounds-checked) but they all come at cost (new hardware, language changes, or reduced perf).