Skip to content

Missed optimization/perf oddity with allocations #128854

@peterwmwong

Description

@peterwmwong

Consider the following minimized example:

pub fn test() {
    for _ in 0..128 {
        let _ = vec![0; 32];
    }
}

Expected output (rustc 1.81.0 eeb90cd 2024-09-04):

example::test::hbbde2d255ea2eb40:
        ret

Actual output (rustc 1.91.0-nightly 040a98a 2025-08-20):

Note: Starting in 1.89 with #141061, unexpected calls replace the unexpected movzxs seen in version 1.83 to 1.88.

example::test::h8c11ad83f7845e4c:
        push    rbx
        mov     rbx, qword ptr [rip + __rustc[861857bac794ed70]::__rust_no_alloc_shim_is_unstable_v2@GOTPCREL]
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        mov     rax, rbx
        pop     rbx
        jmp     rax
Actual output with unexpected `movzx`s (rustc 1.88.0 6b00bc3 2025-06-23):
example::test::ha559be6b680d7ef2:
        mov     rax, qword ptr [rip + __rust_no_alloc_shim_is_unstable@GOTPCREL]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   ecx, byte ptr [rax]
        movzx   eax, byte ptr [rax]
        ret

Godbolt: https://www.godbolt.org/z/x458Pv8P5

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-rust-for-linuxRelevant for the Rust-for-Linux projectC-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchT-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions