rust-lang / rust-lang/rust

Byte count has poor codegen with autovectorization

Open
#136,500 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-autovectorization A-LLVM C-optimization T-compiler
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.

https://rust.godbolt.org/z/6b5bTKoa9

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.