jhorstmann commented on issue #6034:
URL: https://github.com/apache/arrow-rs/issues/6034#issuecomment-2222749163
Another pattern that works well for small copies and generates much smaller
code than full specialization, is using divide and conquer, copy as large a
chunk as possible followed by consecutive smaller chunks. Would look something
like this on a simplified example:
```rust
pub fn make_view2(mut data: &[u8], block_id: u32, offset: u32) -> u128 {
let len = data.len() as u32;
let mut tmp = [0_u8; 16];
tmp[..4].copy_from_slice(&len.to_le_bytes());
let mut i = 4_usize;
if data.len() >= 8 {
tmp[i..i+8].copy_from_slice(&data[..8]);
i += 8;
data = &data[8..];
}
if data.len() >= 4 {
// unsafe { std::intrinsics::assume(i+4 <= tmp.len()); }
tmp[i..i+4].copy_from_slice(&data[..4]);
i += 4;
data = &data[4..];
}
if data.len() >= 2 {
// unsafe { std::intrinsics::assume(i+2 <= tmp.len()); }
tmp[i..i+2].copy_from_slice(&data[..2]);
i += 2;
data = &data[2..];
}
if data.len() >= 1 {
// unsafe { std::intrinsics::assume(i < tmp.len()); }
tmp[i] = data[0];
i += 1;
data = &data[1..];
}
u128::from_le_bytes(tmp)
}
```
This also avoids bounds checks on `data`, but unfortunately not on the last
accesses to `tmp` array.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]