# Can declaring a field as const in a mutable struct require less memory?

**URL:** <https://discourse.julialang.org/t/can-declaring-a-field-as-const-in-a-mutable-struct-require-less-memory/108914>\
**Category:** Internals & Design\
**Tags:** question\
**Created:** [January 17, 2024, 3:09pm UTC](https://discourse.julialang.org/t/can-declaring-a-field-as-const-in-a-mutable-struct-require-less-memory/108914 "2024-01-17T15:09:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tortar](https://avatars.discourse-cdn.com/v4/letter/t/6bbea6/32.png) [@Tortar](https://discourse.julialang.org/u/Tortar)\
**Post date:** [January 17, 2024, 3:09pm UTC](https://discourse.julialang.org/t/can-declaring-a-field-as-const-in-a-mutable-struct-require-less-memory/108914/1 "2024-01-17T15:09:29Z")

</div>

Currently declaring a field as `const` in a mutable struct doesn’t seem to do anything:

```julia
julia> mutable struct A
           x::Int
       end

julia> mutable struct B
           const x::Int
       end

julia> @allocated A[A(i) for i in 1:1000] # all ints are different
24128

julia> @allocated B[B(i) for i in 1:1000] # all ints are different
24128

julia> @allocated A[A(1) for _ in 1:1000] # all ints are 1
24128

julia> @allocated B[B(1) for _ in 1:1000] # all ints are 1
24128

```

Intuitively, I would think that the fourth case could reuse the memory for the 1s and so should allocate less. Is this something feasible?

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [January 17, 2024, 3:20pm UTC](https://discourse.julialang.org/t/can-declaring-a-field-as-const-in-a-mutable-struct-require-less-memory/108914/2 "2024-01-17T15:20:55Z")

</div>

The `const` field doesn’t imply that it is a constant at compile-time, which means that the compiler is not expecting that the values are all the same here and doesn’t try to do such optimizations.

Technically, you might be able to do that with the `Val` type, which is constant at compile-time.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [January 17, 2024, 3:25pm UTC](https://discourse.julialang.org/t/can-declaring-a-field-as-const-in-a-mutable-struct-require-less-memory/108914/3 "2024-01-17T15:25:15Z")

</div>

> [@Tortar](#):
>
> Intuitively, I would think that the fourth case could reuse the memory for the 1s and so should allocate less. Is this something feasible?

No matter whether all fields are `const` or not, object identity between mutable instances must still be distinct. That is their defining feature after all!

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [January 17, 2024, 3:26pm UTC](https://discourse.julialang.org/t/can-declaring-a-field-as-const-in-a-mutable-struct-require-less-memory/108914/4 "2024-01-17T15:26:09Z")

</div>

If it reused the memory it would have to fetch the value using the reference all the time, and the struct would have to carry the reference. Actually declaring it const more about the opposite: you promise to the compiler that the value does not change, and it can do whatever it thinks is more appropriate with it’s storage to improve the performance of the code.

---

<div class="post-metadata">

**Author:** ![Tortar](https://avatars.discourse-cdn.com/v4/letter/t/6bbea6/32.png) [@Tortar](https://discourse.julialang.org/u/Tortar)\
**Post date:** [January 17, 2024, 7:09pm UTC](https://discourse.julialang.org/t/can-declaring-a-field-as-const-in-a-mutable-struct-require-less-memory/108914/5 "2024-01-17T19:09:52Z")

</div>

thanks guys, seems like my intuition was off 🙂
