Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.


Rust 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/f0b58fcf03391a91f7422...


There'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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: