sisl / GaussianFilters.jl

Julia Package for discrete-time linear Gaussian parametric filtering systems, namely KF, EKF, UKF, GM-PHD
MIT License
44 stars 11 forks source link

All struct fields should be concrete types #22

Open lassepe opened 5 years ago

lassepe commented 5 years ago

If you want to maximize performance, having structs immutable is a good first step. However, to get even better performance, it should be avoided to have non-concrete fields. These things are sometimes quite subtle, e.g. this

https://github.com/sisl/GaussianFilters.jl/blob/bc24efe32d6c2c493d8458318150aef0ce40eca6/src/kf_classes.jl#L27

is a problem because Symmetric has two template parameters, e.g.:

julia> Symmetric(A)
3×3 Symmetric{Int64,Array{Int64,2}}:
 1  2  3
 2  5  6
 3  6  8

julia> typeof(Symmetric(A))
Symmetric{Int64,Array{Int64,2}}

Therefore Symmetric{a} where {a} is not a concrete type and makes uses of the corresponding struct unnecessarily slow. A better thing to do here is to have a bounded type parameter, e.g.:

struct LinearDynamicsModel{TA<:AbstracArray, TA<:AbstractArray,
                TW<:Symmetric} <: DynamicsModel
    A::TA
    B::TB
    W::TW
end

Another instance of this problem is the use of Function as a field type, as Function is also an abstract type:

https://github.com/sisl/GaussianFilters.jl/blob/bc24efe32d6c2c493d8458318150aef0ce40eca6/src/kf_classes.jl#L87

lassepe commented 5 years ago

@jamgochiana By the way: Don't feel obligated to do all of this. These are just some hints as I really like the idea of this package. If it seriously bothers me when using this package, I'll just open a PR one day.

jamgochiana commented 5 years ago

No, by all means, keep it coming this is great!

MaximeBouton commented 5 years ago

Regarding the use of Function as a field type. For the nonlinear models, what is the best way to define them such that the filtering algorithm are type stable? Because right now calling model.f or model.h is not type stable since it depends on what model is. Any suggestions? Are there other packages that use that kind of interface?

lassepe commented 5 years ago

POMDPs uses function fields quite a lot (e.g. for FunctionPolicy). Things should be type stable if you do:

struct Foo{F<:Function}
    f::F
end

or am I getting your question wrong?

MaximeBouton commented 5 years ago

Fantastic, I forgot this worked for functions :)