I’m still struggling with (hard) typing and performance; this time consider the following code
it’s not so important what it does (while that also takes some allocations…but not the most for now)
abstract myAbsT
immutable myT <: myAbsT
v::Float64
end
function myFun{T <: myAbsT, S<: Number}(f::Array{T}, α::Array{S,1};
MaxIterations::Int64=500, myMask::Array{Bool} = Array(Bool,0)
)::Array{T}
iter::Int64 = 1
if (length(myMask) == 0)
myMask = falses(f)
end
x = f;
while ( iter < MaxIterations )
iter = iter+1
for i in eachindex(x)
if ~myMask[i]
x[i] = T(x[i].v+α[1])
end
end
end
return x
end
preF = ones(2^9,2^9)
@time myFun(myT.(preF), [0.2])
@code_warntype myFun(myT.(preF), [0.2])
@time myFun(myT.(preF), [0.2]; MaxIterations=1000)
@code_warntype myFun(myT.(preF), [0.2]; MaxIterations = 1000)
There’s something really interesting in the warntypes
(i happily learned here, they are very helpful).
When running the first @code_warntype
one gets
3.809560 seconds (130.57 M allocations: 1.948 GB, 2.27% gc time)
This is still a lot of allocations I don’t know completely where they come from but the body gets shorten tremendously in the profiles. Most interestingly for the second @code_warntype
8.382499 seconds (261.38 M allocations: 3.897 GB, 2.28% gc time)
The number doubles and the time gets quite bad. Looking at the variables, there’s #temp#@_12::Any
.
I even started from a larger example and coulnd’t get it smaller than this but still, i can’t find the reason this temporary variable of type any is used and why for this case even the body is not shortened and contains some red types for example the Array{Bool,N}
(of course myMask is a bool for every ntry in f
but how to fix that?). What’s the reason? I didn’t even touch the mask yet, just set the optional iterations parameter. Coulnd’t figure out what’s happening here. What am I missing here in the typing tricks of Julia?