rust-lang / rust-lang/rust

`MaybeUninit::uninit` zero initializes large uninit array besides small other field

Open
#152,541 8 comments 0 reactions 1 assignee View on GitHub

@veera-sivarajan is already working on this.

Since Feb 16, 2026.

A-codegen A-LLVM C-bug C-optimization llvm-fixed-upstream P-medium regression-from-stable-to-stable S-has-bisection T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

The code is slightly minimized from this forum thread.

use std::mem::MaybeUninit;

#[derive(Debug)]
pub struct Stack<T, const CAP: usize> {
    len: usize,
    v: MaybeUninit<[T; CAP]>,
}

impl<T, const CAP: usize> Stack<T, CAP> {
    /// Create a new empty `Stack`.
    pub fn new() -> Self {

        Stack {
            len: 0,
            v: MaybeUninit::uninit(),
        }
    }
}

type MyStack = Stack<i32,1000>;

pub struct C {
    a: MyStack,
    b: MyStack,
}

impl C {
    #[inline(never)]
    pub fn new() -> C
    {
        C { a:MyStack::new(), b:MyStack::new() }
    }
}

This produces a large memset since 1.93

example::C::new::h966d6519f96c8d05:
        push    rbx
        mov     rbx, rdi
        mov     edx, 8016
        xor     esi, esi
        call    qword ptr [rip + memset@GOTPCREL]
        mov     rax, rbx
        pop     rbx
        ret

previously (1.92) the asm was much more reasonable:

example::C::new::hb6aa5d89c35231d2:
        mov     rax, rdi
        mov     qword ptr [rdi + 4000], 0
        mov     qword ptr [rdi + 8008], 0
        ret

(same comparison on Compiler Explorer)


Bisection points to:

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.