Couldn't find an issue for this and don't know if it counts but filing anyway.
If you have
fn foo(s: &[i8]) -> Vec<u8> {
s.iter()
.map(|&x| x as u8)
.collect()
}
the SpecExtend machinery ensures that the vector has s.len() space reserved in advance. However if you change it to return a result
fn foo(s: &[i8]) -> Result<Vec<u8>> {
s.iter()
.map(|&x| if x < 0 { Err(...) } else { Ok(x as u8) } )
.collect()
}
then (based on examining the LLVM IR and heaptracker's "Temporary" measurements) that optimization has quietly been lost.
This is technically correct in the sense that the first element yielded could be an Err of course (the size hint for the Adapter in Result's FromIterator impl has a lower bound of 0). But this pessimizes the good path to take more memory and be slower in favor of possibly saving memory on the bad one, which seems backwards.
Is there a specialization that could be added to fix this?
Couldn't find an issue for this and don't know if it counts but filing anyway.
If you have
the
SpecExtendmachinery ensures that the vector hass.len()space reserved in advance. However if you change it to return a resultthen (based on examining the LLVM IR and heaptracker's "Temporary" measurements) that optimization has quietly been lost.
This is technically correct in the sense that the first element yielded could be an
Errof course (the size hint for theAdapterinResult'sFromIteratorimpl has a lower bound of 0). But this pessimizes the good path to take more memory and be slower in favor of possibly saving memory on the bad one, which seems backwards.Is there a specialization that could be added to fix this?