What's the big deal with bounds checking?

27 points ArmoredDragonIMO

I keep reading on some blogs and some other social media sites from people who claim to be seasoned programmers that complain about rust being "too slow" for <insert problem here> because of, among other things, bounds checking (with the implication that you'd have to defeat rust's safety guarantees in that case,) which doesn't make a whole lot of sense to me as I think there probably isn't very much arithmetic involved in it. I don't know exactly how it works, but I suspect that it's just checking if the index is greater than the length indicated by the fat pointer.

I was in the middle of teaching myself how to pass closures to functions and experimenting with generic integer types (hence why the code is needlessly hard to read,) and as part of that I decided to benchmark checked and unchecked reads from a slice. I threw a lot of randomness in here, and some unnecessary outputs, to try to defeat any compiler optimizations. There only seems to be a very tiny difference (sometimes the unchecked reads are faster even.)

Here's what I came up with.

https://play.rust-lang.org/?version=stable&mode=release&edition=2021&gist=4ca0791efb95fa693d15df01ee8e79b1

So am I right in thinking those statements are way overblown, or did I not do enough to defeat compiler optimizations here?