Julia @code_warntype warning when two different return types are expected

Viewed 98

I made a function called jacobian that creates 4 sparse matrices with Float64 entries, make the calculations, and depending on the keyword argument stacked being true or false, returns all four separately, return A1, A2, A3, A4, or stacked as a single matrix, return vcat(hcat(A1,A2), hcat(A3,A4)). Running the function with the @code_warntype macro gives me the warning:

Body::Union{NTuple{4,SparseMatrixCSC{Float64,Int64}}, SparseMatrixCSC{Float64,Int64}}
1 ─ %1 = Main.:(var"#jacobian#29")(false, #self#, Y, V)::Union{NTuple{4,SparseMatrixCSC{Float64,Int64}}, SparseMatrixCSC{Float64,Int64}}
└──      return %1

with the ::Union... parts marked in red, which I don't think is a good sign. Is this considered type instablility? If yes, how can I solve this?

Thanks!

1 Answers

This does indeed mark a type instability, though it will usually be less bad than Any being inferred as return type. E.g. the eigen function of LinearAlgebra.jl will return either a complex or a real matrix based on if the input was Symmetric/Hermitian.

Since the result type in your case only depends on an argument being true or false, you could wrap it in a Val to dispatch between the two variants.

Related