# Mutable struct vs Ref in an immutable one?

**URL:** <https://discourse.julialang.org/t/mutable-struct-vs-ref-in-an-immutable-one/92846>\
**Category:** General Usage\
**Tags:** question, type, struct, mutable-structure\
**Created:** [January 12, 2023, 5:07am UTC](https://discourse.julialang.org/t/mutable-struct-vs-ref-in-an-immutable-one/92846 "2023-01-12T05:07:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [January 12, 2023, 5:07am UTC](https://discourse.julialang.org/t/mutable-struct-vs-ref-in-an-immutable-one/92846/1 "2023-01-12T05:07:36Z")

</div>

I want to define a struct where one field is mutable, and one isn’t,. I see two ways to approach this:

```julia
struct A
     x :: Int
     y :: typeof(Ref(1))
end

```

and

```julia
mutable struct B
     const x :: Int
     y :: Int
end

```

What are the differences between the two from a compiler optimization point-of-view? Which style is to be preferred in what situation?

---

<div class="post-metadata">

**Author:** ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)\
**Post date:** [January 14, 2023, 12:47pm UTC](https://discourse.julialang.org/t/mutable-struct-vs-ref-in-an-immutable-one/92846/2 "2023-01-14T12:47:49Z")

</div>

I’m pretty sure I saw Jameson Nash recommend the second approach (using `mutable struct`), but unfortunately I can’t find a reference right now.

---

<div class="post-metadata">

**Author:** ![vchuravy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vchuravy/32/8_2.png) [@vchuravy](https://discourse.julialang.org/u/vchuravy)\
**Post date:** [January 14, 2023, 2:25pm UTC](https://discourse.julialang.org/t/mutable-struct-vs-ref-in-an-immutable-one/92846/3 "2023-01-14T14:25:50Z")

</div>

The `const` annotation requires Julia 1.8. Which one to use is depend on how you want to use them. For struct `A` the access `y[]` will almost always require a pointer dereference. `B` has a nicer layout in memory and the compiler can load both `x` and `y` simultaneously.

A downside is the layout when the `A` and `B` are contained in other structures. As an example inside an array `A` can be stored inline whereas for `B` a reference will be stored.

---

<div class="post-metadata">

**Author:** ![torrance](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torrance/32/38990_2.png) [@torrance](https://discourse.julialang.org/u/torrance)\
**Post date:** [January 16, 2023, 4:24am UTC](https://discourse.julialang.org/t/mutable-struct-vs-ref-in-an-immutable-one/92846/4 "2023-01-16T04:24:34Z")

</div>

> [@vchuravy](#):
>
> A downside is the layout when the `A` and `B` are contained in other structures. As an example inside an array `A` can be stored inline whereas for `B` a reference will be stored.

@vchuravy This [touches on a question](https://discourse.julialang.org/t/on-arrays-with-inline-elements-vs-pointer-indirection/88211) I asked some time ago about determining when a type is stored inline in an array versus as pointers.

I understood that only `isbits` types were stored inline, and that therefore in this case `Struct A` would not be stored inline.

Is this wrong? And if so (as I asked in the other thread), how can I determine when a type will be stored inline or not?

---

<div class="post-metadata">

**Author:** ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)\
**Post date:** [January 16, 2023, 4:39am UTC](https://discourse.julialang.org/t/mutable-struct-vs-ref-in-an-immutable-one/92846/5 "2023-01-16T04:39:41Z")

</div>

Mutable types cannot be stored inline. This is why B is superior to A, since A has to have a costly indirect reference `y` allocated elsewhere the heap, while B is a cheap simple pointer to fast immediate heap memory. The `const` is a bit of a red-herring there, since generally it has no performance impact, but just clarifies intent and invariants.

---

<div class="post-metadata">

**Author:** ![torrance](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torrance/32/38990_2.png) [@torrance](https://discourse.julialang.org/u/torrance)\
**Post date:** [January 16, 2023, 5:19am UTC](https://discourse.julialang.org/t/mutable-struct-vs-ref-in-an-immutable-one/92846/6 "2023-01-16T05:19:43Z")

</div>

There are lots of comments elsewhere that stated storing something inline in an array (or on the stack, for that matter) required the type to be `isbits`, which is a strictly stronger requirement than being simply immutable. My previous understanding was that an immutable struct with any GC’ed element could not be stored contiguously in an array.

However, my own tests just now using `pointer()` to count the spacing between addresses to array elements confirm that struct `A` in the above example is stored contiguously.

Is this a change in more recent versions of Julia? Is it solely a property of the mutability of the type that allows/forbids it to be stored contiguously?

---

<div class="post-metadata">

**Author:** ![filchristou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/filchristou/32/26760_2.png) [@filchristou](https://discourse.julialang.org/u/filchristou)\
**Post date:** [November 28, 2024, 6:01pm UTC](https://discourse.julialang.org/t/mutable-struct-vs-ref-in-an-immutable-one/92846/7 "2024-11-28T18:01:58Z")

</div>

what about making the mutable field a separate mutable struct ?  
e.g.

```julia
mutable struct Bx
    x::Int
end

struct B
     x::Bx
     y::Int
end

b = B(Bx(1), 2)
@show b
# b = B(Bx(1), 2)

b.x.x = 2
@show b
# b = B(Bx(2), 2)

```

what would that mean performance and memory wise ?

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [November 28, 2024, 6:04pm UTC](https://discourse.julialang.org/t/mutable-struct-vs-ref-in-an-immutable-one/92846/8 "2024-11-28T18:04:24Z")

</div>

How’s that different from using a `RefValue` field in an immutable struct, which is what was suggested in the original post already?
