# Missed layout optimization

**URL:** https://internals.rust-lang.org/t/missed-layout-optimization/20045
**Category:** compiler
**Created:** [December 17, 2023, 9:40pm UTC](https://internals.rust-lang.org/t/missed-layout-optimization/20045 "2023-12-17T21:40:48Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![kpreid](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kpreid/32/8484_2.png) [@kpreid](https://internals.rust-lang.org/u/kpreid)
#### Post date: [December 17, 2023, 9:52pm UTC](https://internals.rust-lang.org/t/missed-layout-optimization/20045/2 "2023-12-17T21:52:03Z")

</div>

Padding bytes cannot contain niches, because:

Suppose you have an `&mut Foo` pointing at the value of an `Option<Foo>`. It's allowed to assign to this `Foo` by copying `mem::size_of::<Foo>()` bytes (4 bytes), which would then overwrite the discriminant if it were stored there.

In general, only byte values that _cannot be a part of a valid value of `Foo`_ can be used as a niche in `Foo`. Padding bytes are allowed to be anything, so they cannot make the value invalid, so the `Option` can't rely on the `Foo` user not writing the `None`-niche-value to them.

`Bar` gets optimized because the `c` byte has two valid values (0 and 1) and 254 invalid values, so `Option` gets to pick any one of the invalid values for its use, and rely on it not being overwritten by writing a valid `bool` because a valid `bool` can never have anything but 0 or 1 there.

---

_[View the full topic](https://internals.rust-lang.org/t/missed-layout-optimization/20045)._
