Byte count has poor codegen with autovectorization
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
I'm currently writing some code that counts the amount of a certain byte (newlines in this example) in a large byte slice (>1GB):
fn count(b: &[u8]) -> usize {
b.iter().filter(|&&x| x == b'\n').count()
}
When compiled with -C target-feature=+avx2, avx2 instructions are emitted from autovectorization, but is still around 2x slower than bytecount.
Using portable_simd, the code can be made faster:
fn count_simd(b: &[u8]) -> usize {
let (begin, mid, end) = b.as_simd::<64>();
count(begin)
+ count(end)
+ mid
.iter()
.map(|x| {
x.simd_eq(Simd::splat(b'\n'))
.select(Simd::splat(1u8), Simd::splat(0u8))
.reduce_sum()
})
.map(|x| x as usize)
.sum::<usize>()
}
This has similar performance with bytecount::count.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the count and count_simd examples, compiling with -C target-feature=+avx2 and comparing their generated instructions on the linked Godbolt reproducer. Compare the result with bytecount::count to identify why autovectorization is slower, then verify any compiler change against the same benchmark and generated code.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers, performance
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100