https://godbolt.org/z/y-GVc2
const INPUT: usize = 10;
const OUTPUT: usize = 3;
pub fn get_output1(input: &[u8; INPUT], offset: usize) -> Option<&[u8]> {
if offset <= INPUT - OUTPUT {
// This version optimizes out the extra bounds checks for the slice.
Some(&input[offset..offset + OUTPUT])
} else {
None
}
}
pub fn get_output2(input: &[u8; INPUT], offset: usize) -> Option<&[u8]> {
if offset <= INPUT - OUTPUT {
// This version fails to optimize bounds checks.
Some(&input[offset..][..OUTPUT])
} else {
None
}
}
This is a simplified version of a pessimization that came up when I was trying to optimize the array_ref! macro: droundy/arrayref#16
https://godbolt.org/z/y-GVc2
This is a simplified version of a pessimization that came up when I was trying to optimize the
array_ref!macro: droundy/arrayref#16