I am having some trouble getting a deeper understanding of inference / type stability. I have a struct with an array of matrices and a function. I want to extract something from a matrix in the array, say the size of the first matrix:
type MyType
A::Array{<:AbstractArray,1}
end
function firstsize(b::MyType); return size(b.A[1]); end
a=MyType([randn(3,3),randn(3,3)]);
firstsize(a)
# gives (3,3)
If I run code_warntype on this I get:
@code_warntype(firstsize(a))
Variables:
#self# <optimized out>
b::MyType
Body:
begin
return (Main.size)((Base.arrayref)((Core.getfield)(b::MyType, :A)::Array{#s231,1} where #s231<:AbstractArray, 1)::AbstractArray)::Any
end::Any
This function will be called many times in an inner loop. Should I worry about the ::Any
as a return type? Why do I get ::Any
and not Tuple{Int64,Int64}
? Is there a way to get around it? I would preferable have the work for both sparse and dense matrices.