# Performance of Fields with Union Type

**URL:** <https://discourse.julialang.org/t/performance-of-fields-with-union-type/88863>\
**Category:** General Usage\
**Tags:** performance, type\
**Created:** [October 17, 2022, 6:43pm UTC](https://discourse.julialang.org/t/performance-of-fields-with-union-type/88863 "2022-10-17T18:43:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![HashBrown](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hashbrown/32/43601_2.png) [@HashBrown](https://discourse.julialang.org/u/HashBrown)\
**Post date:** [October 17, 2022, 6:43pm UTC](https://discourse.julialang.org/t/performance-of-fields-with-union-type/88863/1 "2022-10-17T18:43:35Z")

</div>

The `Manual > Performance Tips` documentation says that one should not use a field with an abstract type.

This makes sense to me as otherwise a lot of type lookup and method resolution would have to happen at runtime whenever you used that field.

On the other hand in the `Manual > Types > TypeUnions` says that Unions with a small number of types are still very efficient (Union is an abstract type however).

I have read [Union-splitting: what it is, and why you should care](https://julialang.org/blog/2018/08/union-splitting/) which talks about Union Splitting. Will the compiler only do this optimization in the case of explicitly written Union types? Or will the compiler do so for a small type hierarchy as well?

For example one could write:

```julia
abstract type Side end
struct Bid <: Side end
struct Ask <: Side end

struct Trade
    ...
    s :: Side
end

```

Or alternatively write something very similar

```julia
struct Bid end
struct Ask end
const Side = Union{Bid, Ask}

struct Trade
    ...
    s :: Side
end

```

Would the performance of these be different? I.e would only the latter one use Union Splitting when you access the `s` field of a `Trade` or would both of them do so?

P.S

I understand that

```julia
struct Trade{T <: Side}
    ...
    s :: T
end

```

is the most efficient option, but I don’t believe that fits my use case as that would just cause type instability elsewhere in my code.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [October 17, 2022, 6:45pm UTC](https://discourse.julialang.org/t/performance-of-fields-with-union-type/88863/2 "2022-10-17T18:45:20Z")

</div>

The performance will be different because in the first case, the compiler can’t prove that no one will ever add more subtypes to `Side`.

---

<div class="post-metadata">

**Author:** ![HashBrown](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hashbrown/32/43601_2.png) [@HashBrown](https://discourse.julialang.org/u/HashBrown)\
**Post date:** [October 17, 2022, 6:49pm UTC](https://discourse.julialang.org/t/performance-of-fields-with-union-type/88863/3 "2022-10-17T18:49:09Z")

</div>

@Oscar_Smith What would happen if I hadn’t declared `Side` to be const? Would that also hinder performance then since someone could change `Side`?

And when you say the performance would be different, which one is better? The Union type one?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [October 17, 2022, 6:53pm UTC](https://discourse.julialang.org/t/performance-of-fields-with-union-type/88863/4 "2022-10-17T18:53:31Z")

</div>

It doesn’t matter here if `Side` is a `const`. If `Side` isn’t `const` and gets updated after you create the `struct`, the `struct` definition won’t change. The `Union` will be faster.
