woodruffw / mollusc

Pure-Rust libraries for parsing, interpreting, and analyzing LLVM
MIT License
77 stars 6 forks source link

Help the compiler optimize our mapping code #19

Open woodruffw opened 2 years ago

woodruffw commented 2 years ago

rustc can recognize the following pattern:

assert!(slice.len() >= SOMETHING);

let _ = slice[0];
let _ = slice[1..N];

...and emit only one bounds check (for the initial assert!) instead of one per index/slice operation. We make heavy use of individual/slice indexing throughout the mapper, so we should use assert! judiciously wherever we can.

That being said, we should make sure that our current pattern isn't already providing this optimization, namely:

        if fields.len() < 10 {
            return Err(FunctionError::TooShort(fields.len()));
        }