Krastanov:
In particular, should I think of it as “the julia compiler is not smart enough YET, so unnatural/ugly workarounds like Fix2 are required” or is it a more fundamental limitation of the language that requires fundamental rethinking of my attitude to closures.
opened 05:49PM - 07 Sep 17 UTC
closed 05:07AM - 12 Oct 24 UTC
```julia
function foo(a::AbstractVector)
T = eltype(a)
b = Vector{T}(… )
c = [Set{T}() for x in a]
return length(c)
end
a = rand(1:10, 200);
```
`@code_warntype` shows an inference failure in `b` and `c`.
Changing to `c = Set{T}[Set{T}() for x in a]` fixes `c`'s inference issue.
opened 03:19PM - 28 Feb 16 UTC
performance
compiler:lowering
``` jl
using Images: realtype
function ifi{T<:Real,K,N}(img::AbstractArray{T,N}… , kern::AbstractArray{K,N}, border::AbstractString, value)
if border == "circular" && size(img) == size(kern)
out = real(ifftshift(ifft(fft(img).*fft(kern))))
elseif border != "inner"
prepad = [div(size(kern,i)-1, 2) for i = 1:N]
postpad = [div(size(kern,i), 2) for i = 1:N]
fullpad = [nextprod([2,3], size(img,i) + prepad[i] + postpad[i]) - size(img, i) - prepad[i] for i = 1:N]
A = padarray(img, prepad, fullpad, border, convert(T, value))
krn = zeros(typeof(one(T)*one(K)), size(A))
indexesK = ntuple(d->[size(krn,d)-prepad[d]+1:size(krn,d);1:size(kern,d)-prepad[d]], N)::NTuple{N,Vector{Int}}
AF = ifft(fft(A).*fft(krn))
out = Array(realtype(eltype(AF)), size(img))
end
out
end
```
Test:
``` jl
julia> @code_warntype ifi(rand(3,3), rand(3,3), "replicate", 0)
Variables:
#self#::#ifi
img::Array{Float64,2}
kern::Array{Float64,2}
border::ASCIIString
value::Int64
prepad::Box
...
```
Now comment out the `indexesK = ...` line (the output of which is not used at all). Suddenly `prepad` is inferred as `Array{Int, 1}`.
It’s two of the oldest Julia issues. They are not easy to solve, and they will likely be around for awhile. Your compile times and even in some cases runtime will be happy. Generally it’s a good style to avoid things that you know might hit things that go wrong, which is why usually we try to keep closures to a minimum. That doesn’t mean don’t use them, but you should probably double check when you do, or put a let block around it.
1 Like