# Is UnionAll abstract? Is it bad for performance?

**URL:** <https://discourse.julialang.org/t/is-unionall-abstract-is-it-bad-for-performance/48653>\
**Category:** Performance\
**Created:** [October 19, 2020, 9:09pm UTC](https://discourse.julialang.org/t/is-unionall-abstract-is-it-bad-for-performance/48653 "2020-10-19T21:09:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![JamesK](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jamesk/32/8989_2.png) [@JamesK](https://discourse.julialang.org/u/JamesK)\
**Post date:** [October 19, 2020, 9:09pm UTC](https://discourse.julialang.org/t/is-unionall-abstract-is-it-bad-for-performance/48653/1 "2020-10-19T21:09:37Z")

</div>

Sorry in advance if this is covered somewhere; I’m still a beginner.

I’m concerned about the two performance tips as they relate to UnionAll: 1) Avoid fields with abstract type and 2) Avoid containers with abstract type parameters. Let’s say we have some type:

```julia
mutable struct MyType{field_type1<:Abstract_field_type, field_type2<:Abstract_field_type}
    a::field_type1
    b::field_type2
    ...
end

```

Where there are a few possible concrete types of Abstract\_field\_type. Now I want to contain a MyType object in a different object or in a dictionary of MyType objects:

```julia
mutable struct container_struct
    a::MyType
    b::Dict{Int64, MyType}
end

```

Now when I try to see if the values of the dict b are an abstract type, by setting `container = container_struct(...)` and printing: `println(valtype(container.b).abstract)`, I get `type UnionAll has no field abstract`. Is a UnionAll abstract? Might this cause a performance hit? Or is UnionAll covered under this blogpost: [Union-splitting: what it is, and why you should care](https://julialang.org/blog/2018/08/union-splitting/) when it says

> But often, `Union{Type1, Type2, ...}` is nothing to worry about, because it causes little or no performance hit of any kind.

If this is a potential performance problem, how can I design things better? Thank you!

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [October 19, 2020, 9:37pm UTC](https://discourse.julialang.org/t/is-unionall-abstract-is-it-bad-for-performance/48653/2 "2020-10-19T21:37:37Z")

</div>

Yes, `UnionAll` is abstract.

if your container struct and dict only hold one particular subtype of that UnionAll you can do

```julia
mutable struct container_struct{T<:MyType}
    a::T
    b::Dict{Int64, T}
end

```

Otherwise, you can just leave it as you have it if you need a hetrogenious container.  
It’s not the end of the world.  
We don’t have to scrape every last bit of peformance out.

You can put in function barriers where you need them
