rust-lang / rust-lang/rust

Possibly sub-optimal optimization of few bytes copy

Open
#130,304 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

This code contains three examples. foo0 and foo2 are optimized well (with a mov + 4 bytes istruction), while foo1 shows single byte copies.

pub fn foo0(data: &mut [u8]) -> &[u8] {
    if data.len() >= 5 {
        data[0] = b'F';
        data[1] = b'a';
        data[2] = b'l';
        data[3] = b's';
        data[4] = b'e';
        &data[.. 5]
    } else {
        &[]
    }
}

pub fn foo1(data: &mut [u8]) -> &[u8] {
    if data.len() >= 5 {
        data[0] = b'F';
        data[1] = b'a';
        data[2] = b'l';
        data[3] = b's';
        data[4] = b'e';
        &data[.. 5]
    } else if data.len() >= 4 {
        data[0] = b'T';
        data[1] = b'r';
        data[2] = b'u';
        data[3] = b'e';
        &data[.. 4]
    } else {
        &[]
    }
}

pub fn foo2(data: &mut [u8]) -> &[u8] {
    if data.len() >= 5 {
        data[.. 5].copy_from_slice(&[b'F', b'a', b'l', b's', b'e']);
        &data[.. 5]
    } else if data.len() >= 4 {
        data[.. 4].copy_from_slice(&[b'T', b'r', b'u', b'e']);
        &data[.. 4]
    } else {
        &[]
    }
}

fn main() {}

Using the godbolt site with:

rustc 1.83.0-nightly (8d6b88b16 2024-09-11)
binary: rustc
commit-hash: 8d6b88b168e45ee1624699c19443c49665322a91
commit-date: 2024-09-11
host: x86_64-unknown-linux-gnu
release: 1.83.0-nightly
LLVM version: 19.1.0

Compilation using -C opt-level=3 and other aggressive optimization flags.

The asm is:

foo0:
        cmp     rsi, 4
        jbe     .LBB0_1
        mov     rax, rdi
        mov     dword ptr [rdi], 1936482630
        mov     byte ptr [rdi + 4], 101
        mov     edx, 5
        ret
.LBB0_1:
        mov     eax, 1
        xor     edx, edx
        ret


foo1:
        mov     rax, rdi
        cmp     rsi, 4
        jbe     .LBB1_1
        mov     word ptr [rax], 24902
        mov     edx, 5
        mov     ecx, 4
        mov     sil, 115
        mov     edi, 3
        mov     r8b, 108
        mov     r9d, 2
        mov     byte ptr [rax + r9], r8b
        mov     byte ptr [rax + rdi], sil
        mov     byte ptr [rax + rcx], 101
        ret
.LBB1_1:
        jne     .LBB1_2
        mov     byte ptr [rax], 84
        mov     edx, 4
        mov     ecx, 3
        mov     sil, 117
        mov     edi, 2
        mov     r8b, 114
        mov     r9d, 1
        mov     byte ptr [rax + r9], r8b
        mov     byte ptr [rax + rdi], sil
        mov     byte ptr [rax + rcx], 101
        ret
.LBB1_2:
        mov     eax, 1
        xor     edx, edx
        ret


foo2:
        mov     rax, rdi
        cmp     rsi, 4
        jbe     .LBB2_1
        mov     byte ptr [rax + 4], 101
        mov     dword ptr [rax], 1936482630
        mov     edx, 5
        ret
.LBB2_1:
        jne     .LBB2_2
        mov     dword ptr [rax], 1702195796
        mov     edx, 4
        ret
.LBB2_2:
        mov     eax, 1
        xor     edx, edx
        ret

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

The payload names no repository files or tests. Reproduce foo0, foo1, and foo2 with the stated nightly rustc version, opt-level=3, and aggressive flags on Godbolt, then inspect the generated assembly; done means determining whether foo1's byte-copy code generation is an actionable compiler issue.

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
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.