I am not sure, that I understand correctly the performance tips regards type declarations and containers with abstract types, so I tested my code with Traceur.
But I don’t really understand the output, could you please help me with it?
I have this code in mwe.jl
:
using Dates
abstract type AbstractWorkoutType end
struct RunningWorkout <: AbstractWorkoutType
distance::Float64
end
struct GymWorkout <: AbstractWorkoutType
type::String
end
struct Workout{T<:AbstractWorkoutType}
date::DateTime
workout::T
effort::Union{Int64, Nothing}
end
function traceme()
dt1 = DateTime(2019,1,1)
dt2 = DateTime(2019,1,2)
r = RunningWorkout(1.5)
g = GymWorkout("push ups")
w1 = Workout(dt1, r, nothing)
w2 = Workout(dt2, g, 5)
return [w1, w2]
end
If I ran @trace
, I get the following:
julia> include("mwe.jl")
traceme (generic function with 1 method)
julia> traceme()
2-element Array{Workout,1}:
Workout{RunningWorkout}(2019-01-01T00:00:00, RunningWorkout(1.5), nothing)
Workout{GymWorkout}(2019-01-02T00:00:00, GymWorkout("push ups"), 5)
julia> using Traceur
julia> @trace traceme()
┌ Warning: err is assigned as Union{Nothing, ArgumentError}
└ @ C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.1\Dates\src\types.jl:182
┌ Warning: unsafe_pointer_to_objref returns Any
└ @ pointer.jl:128
┌ Warning: getindex returns Any
└ @ essentials.jl:549
┌ Warning: getindex returns Any
└ @ array.jl:729
┌ Warning: aprimary is assigned as Type{Workout}
└ @ promotion.jl:95
┌ Warning: aprimary is assigned as Type{Workout{T<:AbstractWorkoutType}}
└ @ promotion.jl:108
┌ Warning: aprimary is assigned as Any
└ @ promotion.jl:112
┌ Warning: dynamic dispatch to (Base.UnionAll)(φ (%97 => %93, %119 => %114), φ (%97 => %82, %119 => %101))
└ @ promotion.jl:112
┌ Warning: typejoin returns Any
└ @ promotion.jl:16
┌ Warning: getindex returns Union{Workout{GymWorkout}, Workout{RunningWorkout}}
└ @ tuple.jl:24
┌ Warning: x is assigned as Union{Workout{GymWorkout}, Workout{RunningWorkout}}
└ @ abstractarray.jl:668
┌ Warning: y is assigned as Union{Nothing, Tuple{Int64,Int64}}
└ @ abstractarray.jl:667
┌ Warning: y is assigned as Union{Nothing, Tuple{Int64,Int64}}
└ @ abstractarray.jl:672
2-element Array{Workout,1}:
Workout{RunningWorkout}(2019-01-01T00:00:00, RunningWorkout(1.5), nothing)
Workout{GymWorkout}(2019-01-02T00:00:00, GymWorkout("push ups"), 5)
I don’t understand where these warnings come from, and whether I can ignore them.
Later I wan to operate on arrays of Workout
, and while this current example is not performance critical, I have similar types at another project where speed does matter, so I would like to do it right.
Thank you for your help in advance!