`maximum!` allocates

I get allocations, but I don’t understand why since it is writing the results in a preallocated array.

X = rand(10,3,4); x= rand(1,3,4);
@btime maximum!($x,$X)
# 452.492 ns (6 allocations: 240 bytes)

Deep down, following lots of function calls, maximum! calls the function mapfirst! and that allocates memory

You can prevent that by calling maximum!(x, X, init=false) but I think you have to ensure manually that x does not contain a value bigger than any value in X (but I am not completely sure about that to be honest)

3 Likes

I raised an issue, https://github.com/JuliaLang/julia/issues/28928

I am not really sure this is an issue. maximum! never claims to not allocate memory. And those are all just temporary allocations that are freed as soon as mapfirst! has finished …

1 Like

In principle the allocation could affect performance.
Those allocations don’t have to wait for GC?

2 Likes