# Buffer initialization does not get optimized out

**URL:** <https://users.rust-lang.org/t/buffer-initialization-does-not-get-optimized-out/143074>\
**Category:** help\
**Created:** [October 16, 2026, 5:23am UTC](https://users.rust-lang.org/t/buffer-initialization-does-not-get-optimized-out/143074 "2026-10-16T05:23:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![newpavlov](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/newpavlov/32/6207_2.png) [@newpavlov](https://users.rust-lang.org/u/newpavlov)\
**Post date:** [October 16, 2026, 5:23am UTC](https://users.rust-lang.org/t/buffer-initialization-does-not-get-optimized-out/143074/1 "2026-10-16T05:23:46Z")

</div>

I have the following snippet (minimization of a similar problem encountered in real code):

```rust
const N: usize = 32;

fn read(block: &[u8; 8 * N]) -> [u64; N] {
    let (chunks, tail) = block.as_chunks();
    assert!(tail.is_empty());

    let mut block = [0u64; N];
    for i in 0..N {
        block[i] = u64::from_be_bytes(chunks[i]);
    }
    block
}

```

It gets compiled into the following assembly: [Compiler Explorer](https://rust.godbolt.org/z/W8Y6Yqf15) (RISC-V is used for simplicity, the same issue is present on x86 as well)

As can be clearly seen, the compiler keeps initialization of the buffer (the `memset` part) despite it being fully overwritten shortly after. Is there a reason for it? On the first glance, it looks like a trivial optimization for the compiler. Interestingly, this `memset` persists even after the function gets inlined, so it's not some ABI peculiarity.

Note that I know that we can write the function using `core::array::from_fn`. In my code it can not be used, since next buffer values depend on previously computed buffer values. Also, using it still produces suboptimal code: [Compiler Explorer](https://rust.godbolt.org/z/4jh4Yx3xM) (note the `memcpy` part)

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/scottmcm/32/4286_2.png) [@scottmcm](https://users.rust-lang.org/u/scottmcm)\
**Post date:** [October 16, 2026, 5:35am UTC](https://users.rust-lang.org/t/buffer-initialization-does-not-get-optimized-out/143074/2 "2026-10-16T05:35:44Z")

</div>

> [@newpavlov](#):
>
> Also, using it still produces suboptimal code

Looks fine in x64-v3 at least: No `memcpy`, no `alloca` [https://rust.godbolt.org/z/1MxznsxYd](https://rust.godbolt.org/z/1MxznsxYd)

---

<div class="post-metadata">

**Author:** ![newpavlov](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/newpavlov/32/6207_2.png) [@newpavlov](https://users.rust-lang.org/u/newpavlov)\
**Post date:** [October 16, 2026, 5:56am UTC](https://users.rust-lang.org/t/buffer-initialization-does-not-get-optimized-out/143074/4 "2026-10-16T05:56:13Z")

</div>

> [@scottmcm](#):
>
> Looks fine in x64-v3 at least: No `memcpy`, no `alloca`

Increasing `N` still results in `memcpy`: [Compiler Explorer](https://rust.godbolt.org/z/P8PxaPa8z) (granted, it's a 2 KiB buffer, but still) Simply enabling AVX2 produces the same result, so the difference is likely to be more about being able to use YMM registers and to fully unroll the loop.

> [@Schard](#):
>
> `chunks` will be `&[[T; N]]`, so the length of `chunks` cannot be known at compile time.

No, it's known at compile time to the compiler. The bound checks get properly eliminated as can be easily seen in the generated assembly. Using `let chunk = block[8 * i..][..8].try_into().unwrap();` produces the same result.

---

<div class="post-metadata">

**Author:** ![khimru](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/khimru/32/31344_2.png) [@khimru](https://users.rust-lang.org/u/khimru)\
**Post date:** [October 16, 2026, 9:53am UTC](https://users.rust-lang.org/t/buffer-initialization-does-not-get-optimized-out/143074/5 "2026-10-16T09:53:58Z")

</div>

> [@newpavlov](#):
>
> On the first glance, it looks like a trivial optimization for the compiler.

How many compilers have you wrote or worked with? You are asking compiler to notice that two regions of memory are overwritten and that new values don't depend on old values… pretty demanding task. Not impossible, but somewhere near “the bleeding edge” of how compilers work.

Compiler work perfectly fine with values that may be lifted to registers, if values are too big for that then it's entirelly different kettle of fish.

> [@newpavlov](#):
>
> the difference is likely to be more about being able to use YMM registers and to fully unroll the loop.

Yes. You are converting bleeding edge, ultra-hard, something that's still “state of the art” into something that was explored for decades. Resutls are radically different.

It's chicken-end-the-egg problem: before Rust LLVM had little incentive to base in-memory optimizations on the unique references, because C and C++ don't have them, in practice (yes, [restrict](https://en.cppreference.com/c/language/restrict) exists, no it's not a replacement) — and Rust team is not yet established enough to explore that on Rust side, they are busy with many other things.

> [@newpavlov](#):
>
> The bound checks get properly eliminated as can be easily seen in the generated assembly.

Bounds checks are easy. They don't touch memory.

---

<div class="post-metadata">

**Author:** ![tczajka](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/tczajka/32/24743_2.png) [@tczajka](https://users.rust-lang.org/u/tczajka)\
**Post date:** [October 17, 2026, 12:38am UTC](https://users.rust-lang.org/t/buffer-initialization-does-not-get-optimized-out/143074/6 "2026-10-17T00:38:29Z")

</div>

It's just a tricky optimization. The compiler has to prove that you always write _all_ the elements, and never use the previous ones. But I guess it should be doable for simple in-order loops like this.

There is a ticket for a similar example: [[DSE] Stores not eliminated if they are overwritten in a loop.](https://github.com/llvm/llvm-project/issues/45805)

---

<div class="post-metadata">

**Author:** ![ProgramCrafter](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/programcrafter/32/45916_2.png) [@ProgramCrafter](https://users.rust-lang.org/u/ProgramCrafter)\
**Post date:** [October 17, 2026, 2:07am UTC](https://users.rust-lang.org/t/buffer-initialization-does-not-get-optimized-out/143074/7 "2026-10-17T02:07:57Z")

</div>

> [@newpavlov](#):
>
> It gets compiled into the following assembly: [Compiler Explorer](https://rust.godbolt.org/z/W8Y6Yqf15) (RISC-V is used for simplicity, the same issue is present on x86 as well)

Optimizes well on `-C opt-level=3`. If you could make an example with dependent initialization, though, I'd appreciate it; I do have a sketch of an idea (instead of referencing previous values in the destination array, recalculate whatever would be written there, and mix with some optimization hints).

---

<div class="post-metadata">

**Author:** ![tczajka](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/tczajka/32/24743_2.png) [@tczajka](https://users.rust-lang.org/u/tczajka)\
**Post date:** [October 17, 2026, 2:29am UTC](https://users.rust-lang.org/t/buffer-initialization-does-not-get-optimized-out/143074/8 "2026-10-17T02:29:58Z")

</div>

> [@ProgramCrafter](#):
>
> Optimizes well on `-C opt-level=3`.

The memset comes back when you increase `N` so the loop doesn't get unrolled.
