3 ms·
As an aside, I seem to recall .NET making an interesting optimisation here, such that if you access an array using, for example, `data[20]`, bounds checks are o
by GordonS 7y ago
As an aside, I seem to recall .NET making an interesting optimisation here, such that if you access an array using, for example, `data[20]`, bounds checks are omitted for lower indexes.
- shepmaster 7y agoRust and/or LLVM will make this optimization as well. The easiest example I can find of this is in the buffered IO code [1], but there have been others [1]: https://github.com/rust-lang/rust/blob/f0b58fcf03391a91f74224fe38a696d5a5b789d9/src/libstd/io/buffered.rs#L277-L278 https://github.com/rust-lang/rust/blob/f0b58fcf03391a91f7422...
- exrook 7y agoThere's a similar pattern in rust: fn loop(a: &[u8]) { assert!(a.len >= 20); for x in (0..20) { let y = a[i]; //Do something with y } } Adding the assert before the loop will remove the bounds checks happening on each iteration.