Reputation: 559
I think I am getting a type instability in my code, but I'm not sure why. Consider the following code.
module test
mutable struct Data
arr::Array{Int64, 1}
Data(n) = new(zeros(n))
end
function main()
data = Data(5)
data.arr
end
println(main())
@code_warntype main()
end
The output of @code_warntype main()
starts with the following three lines
Variables:
#self#::test.#main
data::Any
^^^
Maybe I am not interpreting the output of @code_warntype
correctly, but it seems that I have a type-instability for the data
variable. Can someone shed some light on what is going on here? I'm using 0.6.2
if that helps.
Upvotes: 1
Views: 182
Reputation: 372
This is not really an answer, but it may shed some light on the problem. If I rewrite your code as a 2-dimensional array, all the type instabilities seem to disappear and the native code is minimised:
module test
mutable struct Data
arr::Array{Int64,2}
Data(n::Int64, m::Int64=1) = new(zeros(Int64, n, m))
end
function main()
data = Data(5)
data.arr
end
println(main())
@code_warntype main()
end
The complete output in this case is as follows:
[0; 0; 0; 0; 0]
Variables:
#self# <optimized out>
data::test.Data
Body:
begin
data::test.Data = $(Expr(:invoke, MethodInstance for test.Data(::Int64, ::Int64), :(test.Data), 5, 1)) # line 37:
return (Core.getfield)(data::test.Data, :arr)::Array{Int64,2}
end::Array{Int64,2}
All references to Any
now seem to have miraculously disappeared from the output. I have no idea why!
Upvotes: 1