ClimaLand.jl icon indicating copy to clipboard operation
ClimaLand.jl copied to clipboard

remove symbol usage for type stability

Open juliasloan25 opened this issue 11 months ago • 0 comments

Is your feature request related to a problem? Please describe. Throughout our code, we use the pattern (; :sym => val) in NamedTuples. This can be type unstable because of the use of symbols. We should update this pattern to avoid type instability in our code.

This pattern relies on the compiler propagating the value of each symbol throughout the code. If this constant propagation ever stops, the compiler will only know the symbol is of type Symbol, but it won't actually know what the symbol is. This would cause an expression of the format (; :sym => val) to have an uninferable type, so things will take longer to compile and allocate more.

Describe the solution you'd like replace the pattern (; :sym => val) with (; sym => val)

ClimaCore.MatrixFields has a macro @name that can be used to loop over nested names of a NamedTuple, which is one of the reasons we've been using this pattern. (see docs here)

Examples of failed type inference

get_pairs() = (:a => 1, :b => 2) make_named_tuple() = (; get_pairs()...) @code_warntype make_named_tuple()

get_symbols() = (:a, :b) make_named_tuple(value) = (; map(symbol -> symbol => value, get_symbols())...) @code_warntype make_named_tuple(1)

juliasloan25 avatar Mar 15 '24 22:03 juliasloan25