I’m confused by why the following allocates (this is a reduced example that arose from trying to implement a function from #32739 via higher order functions instead of explicit loops):
g(v) = mapreduce(i->v*i, +, eachindex(v)) v=rand(10) @btime g($v)
... (1 allocation: 16 bytes) on v1.1. How can I get rid of that allocation?
A similar thing happens with e.g.
sum(f, itr) as soon as the passed function
f contains reference to any element of a captured array:
_h(i,v) = i*v[i] h(v) = sum(i->_h(i,v), eachindex(v))
... (1 allocation: 16 bytes).
I guess that suggests that the allocations are not really due to
sum, or similar, but to the anonymous function itself? Still, I don’t really get why this happens.
[This was tested on julia-v1.1.1, btw.]