It is well know that Julia struggles with type inference in higher order
functions.  This usually leads to slow code and memory allocations.
There are a few hacks to work around this.  Anyway, the question I have
is: Why can't Julia do better with in-place functions?

In short, a higher-order function like this:

function f(fn!,ar)
    for i=1:n
        fn!(ar, i) # fn! updates ar[i] somehow, returns nothing
        nothing    # to make sure output of f is discarded
    end
end

has almost as bad a performance (runtime and allocation-wise) as

function g(fn,ar)
    for i=1:n
        ar[i] = fn(ar[i])
    end
end

A in-depth, ready to run example is here:
https://gist.github.com/mauro3/f17da10247b0bad96f1a
Including output of @code_warntype.

So, why is Julia allocating memory when running f?  Nothing of f gets
assigned to anything.

Would this be something which is fixable more easily than the whole of
the higher-order performance issues?  If so, is there an issue for this?

Having good in-place higher order functions would go a long way with
numerical computations.  Thanks!

Reply via email to