Defining types of Structs attributes can sometimes be negatively impacting performance?

Of note is that the declared field type has no bearing on the dispatch behavior of a value retrieved from that field. For example:

julia> struct Foo
           a::Real
       end

julia> bar(::Real) = "Real fallback"
bar (generic function with 1 method)

julia> bar(::Int) = "Int specialization"
bar (generic function with 2 methods)

julia> bar(Foo(1).a)
"Int specialization"

julia> bar(Foo(1.0).a)
"Real fallback"

When you say a struct field has type Real, all you’re saying is “this field can hold objects of any subtype of Real”. Type inference will use that information to narrow down the possibilities for dispatching, but it won’t otherwise influence dispatch behavior.

One consequence of declaring a field with an abstract type is that the object actually stored there is no longer stored in-line, because the original type needs to be preserved for dynamic dispatch to work. It’s this indirection & dynamic type checks that cause a slowdown in hot loops.

2 Likes