No, of you look at the typed IR I posted, it says
259::Union{Nothing, Some{Int64}}
and (259 = Base.getproperty(o, :o))
So it's inferred properly to be a small union which julia handles efficiently at runtime: julia> @btime type_stable()
18.988 ns (1 allocation: 32 bytes)
1
> Furthermore, TypeStable itself has to be on the heap, which I think means this causes two pointer lookups rather than just one.It's on the heap because you wrote
mutable struct
in your example and I was just mimicking you as close as possible. If you don't actually want it to be mutable, then you can just remove the keyword mutable and it'll be even faster and have no heap allocations: julia> @btime type_stable()
18.426 ns (0 allocations: 0 bytes)
1