Open
Description
Hi,
With Julia 1.10.1 I noticed that mapreduce
with two arrays allocates:
julia> x = randn((512,512));
julia> y = randn((512, 512));
julia> g(x) = x^2
g (generic function with 1 method)
julia> @time mapreduce(g,+,x)
0.031165 seconds (16.85 k allocations: 1.149 MiB, 99.57% compilation time: 100% of which was recompilation)
261797.64934712922
# no signficant allocation, perfect
julia> @time mapreduce(g,+,x)
0.000168 seconds (1 allocation: 16 bytes)
261797.64934712922
julia> f(x,y) = x * y
f (generic function with 1 method)
julia> mapreduce(f, +, x, y);
# bad allocations
julia> @time mapreduce(f, +, x, y);
0.001142 seconds (6 allocations: 2.000 MiB)
Looking at the implementation there is no specialization.
Maybe we should adapt the over promising docstring then?
mapreduce is functionally equivalent to calling reduce(op, map(f, itr); init=init), but will in general execute faster since no intermediate collection
needs to be created. See documentation for reduce and map.
Related to #38558 ?
Best,
Felix