4 ms·
People keep being surprised that bounds-checking doesn’t really seem to incur that much cost but frankly it seems pretty straightforward to me. In the years of
by stouset 3y ago
People keep being surprised that bounds-checking doesn’t really seem to incur that much cost but frankly it seems pretty straightforward to me.
In the years of Rust code I’ve written, I don’t think I’ve ever actually indexed into an array manually. If I have it’s been an incredibly small number of cases. I’m almost always iterating, which makes bounds checks essentially unnecessary.
- cmrdporcupine 3y agoI suspect it's more that bounds checking actually helps performance in many circumstances in that it can improve branch prediction. Not always, but sometimes.
- menaerus 3y agoInserting branches to your code can only make it slower or equally ~fast but not faster than branchless code so what you say doesn't make a lot of sense.
- cmrdporcupine 3y agoBounds check out of the tight loop can give the possibility of the compiler dropping bounds check and/or related branches later.
- menaerus 3y agoThis sounds purely theoretical. And still even if compiler would be able to coalesce multiple branches into less, such branchy code still cannot be faster than the branchless one.
- tyg13 3y agoNot necessarily. If the branch is predictable, it may be better than branchless, particularly if the alternative is a cmov. From Agner: > As a rule of thumb, we can say that a conditional jump is faster than a conditional move if the code is part of a dependency chain and the prediction rate is better than 75%. A conditional jump is also preferred if we can avoid a lengthy calculation [...] when the other operand is chosen
- pjmlp 3y agoAs usual, I recall the remarks of C.A.R Hoare on his 1980's Turing Award speech, regarding bounds checking on Algol compilers and customers point of view on how it should be unlawful to do otherwise. Never, ever, since 1986, have bounds checking been the major source of performance issues on applications I have written. Rather ill chosen algorithms or data structures.