8 ms·
I think the first question to ask is whether we need a language that does away with array bounds checking. If, like C, you think you do, you accept (on current
by Someone 5y ago
I think the first question to ask is whether we need a language that does away with array bounds checking.
If, like C, you think you do, you accept (on currently popular CPUs) that code may read from and write to arbitrary memory, and that means you have to define that as “anything can happen”.
If you don’t, I think you can get rid of the most common causes of UB.
So, with modern compiler technology, are languages that check array bounds necessarily slower than those that aren’t? If so, is the difference large enough?
- spacechild1 5y ago> So, with modern compiler technology, are languages that check array bounds necessarily slower than those that aren’t? If so, is the difference large enough? For performance sensitive tasks - definitely! I certainly don't want array bound checking in my (soft)realtime audio processing code. As a side note: when I write an audio plugin, it's the responsibility of the host application to tell me the correct buffer sizes, so I basically deal with trusted input. It's still possible to mess up, of course, but personally I found writing DSP code to be pretty safe.
- zppln 5y agoWell, if you're in aerospace (aviation) your compiler inserted code is just an annoyance to you so I'd say that yes, as it stands today, you definitely need those languages.
- pornel 5y agoThe difference is very it depends. In Rust, if you use iterators or make loops simple enough for LLVM to understand, then redundant bounds checks are optimized out, so the safety is essentially free. However, there are certain types of hot loops, like matrix operations, that are too cumbersome to write as 1d iterators, and are too complex for LLVM to prove safe. In that case bounds checks hurt performance a lot. Not because of checks themselves, but because added control flow prevents autovectorization and other optimizations that depend on reordering code. We need smarter optimizers that understand they can collapse and move bounds checks. Currently `a[1], a[2]` has 2 bounds checks, but `a[2], a[1]` has 1, because the optimizer thinks you care exactly which one of them will fire, rather than merely ensuring the safety.