Re:Does Rust's array bounds checking affect performance (2017)?
by expnkx on Monday July 12, 2021 @04:58PM
(#27815046)
If you want safety. you can just use _GLIBCXX_ASSERTIONS
https://godbolt.org/z/eThdeW3Mv
It gives you all bounds checking you need.
In fact, a lot of Linux distributions (like Fedora) built all packages with _GLIBCXX_ASSERTIONS by default.
Re:Does Rust's array bounds checking affect performance (2017)?
by matesz on Monday July 12, 2021 @05:01AM
(#27808412)
That is amazing, thanks!
Re:Does Rust's array bounds checking affect performance (2017)?
by mlindner on Monday July 12, 2021 @06:41AM
(#27808945)
Here's the identical result to the C++ output, for reference. https://godbolt.org/z/6sa3zf555
Here's a fair comparison with C++:
https://godbolt.org/z/qh5n49Y69 https://godbolt.org/z/rdET7aT5d
As you can see the Rust version is faster (the only major difference is Rust embeds more information about the panic locations rather than having a single shared message for the exception).
In either case though I wouldn't write the Rust or C++ that way, I'd write a single expression that filled in all the values at once rather than doing them line by line after checking the size a single time. Rust can then elide the bounds check if it knows that you already checked. In Rust and C++ you would should use iterators via for-each loops or a single statement initializer.
(If you want, you can use unsafe and get identical performance to the C++ version using .get_unchecked())