rust-lang / rust-lang/rust

Pattern matching on byte slices produces sub-optimal ASM

Open
#146,511 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-optimization T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

#[unsafe(no_mangle)]
fn test1(data: &[u8]) -> Option<Value> {
    match data {
        [.., 100, 0, 2, 111, 112, 97, 10] => Some(Value::Test0),
        [.., 45, 254, 12, 74, 13, 65, 10] => Some(Value::Test1),
        [.., 75, 11, 64, 12, 128, 65, 11] => Some(Value::Test2),
        [.., 101, 55, 32, 77, 12, 66, 88] => Some(Value::Test3),
        [.., 75, 234, 56, 23, 86, 45, 86] => Some(Value::Test4),
        [.., 51, 35, 75, 14, 65, 45, 155] => Some(Value::Test5),
        [.., 66, 23, 54, 123, 76, 23, 66] => Some(Value::Test6),
        [.., 60, 55, 87, 235, 67, 14, 88] => Some(Value::Test7),
        [.., 69, 22, 4 , 1  , 0, 55, 129] => Some(Value::Test8),
        [.., 99, 23, 77, 13, 66, 86, 193] => Some(Value::Test9),
        _ => None
    }
}

#[unsafe(no_mangle)]
fn test2(data: &[u8]) -> Option<Value> {
    match data {
        data if data.ends_with(&[100, 0, 2, 111, 112, 97, 10]) => Some(Value::Test0),
        data if data.ends_with(&[45, 254, 12, 74, 13, 65, 10]) => Some(Value::Test1),
        data if data.ends_with(&[75, 11, 64, 12, 128, 65, 11]) => Some(Value::Test2),
        data if data.ends_with(&[101, 55, 32, 77, 12, 66, 88]) => Some(Value::Test3),
        data if data.ends_with(&[75, 234, 56, 23, 86, 45, 86]) => Some(Value::Test4),
        data if data.ends_with(&[51, 35, 75, 14, 65, 45, 155]) => Some(Value::Test5),
        data if data.ends_with(&[66, 23, 54, 123, 76, 23, 66]) => Some(Value::Test6),
        data if data.ends_with(&[60, 55, 87, 235, 67, 14, 88]) => Some(Value::Test7),
        data if data.ends_with(&[69, 22, 4 , 1  , 0, 55, 129]) => Some(Value::Test8),
        data if data.ends_with(&[99, 23, 77, 13, 66, 86, 193]) => Some(Value::Test9),
        _ => None
    }
}

enum Value {
    Test0,
    Test1,
    Test2,
    Test3,
    Test4,
    Test5,
    Test6,
    Test7,
    Test8,
    Test9
}

I expected to see this happen: test1 and test2 are equivalent, and should generate same compiled code.

Instead, this happened: the generated code of test1 is inferior to test2

Meta

rustc --version --verbose:

rustc 1.89.0 (29483883e 2025-08-04)
binary: rustc
commit-hash: 29483883eed69d5fb4db01964cdf2af4d86e9cb2
commit-date: 2025-08-04
host: x86_64-unknown-linux-gnu
release: 1.89.0
LLVM version: 20.1.7

Godbolt link: https://rust.godbolt.org/z/76aT93b1z
Related RPLCS discussion: https://discord.com/channels/273534239310479360/1388176726994980925

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 supplied test1 and test2 reproducer and compare their generated assembly using the linked Godbolt example. Investigate why the byte-slice pattern match produces inferior code, then verify that equivalent patterns generate comparable optimized assembly while preserving the reported matching behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
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.