Combinatorics.jl icon indicating copy to clipboard operation
Combinatorics.jl copied to clipboard

Array allocation in iterating over `MultiExponents`

Open jishnub opened this issue 3 years ago • 3 comments

https://github.com/JuliaMath/Combinatorics.jl/blob/d1b633bffd1ed7ff3301e38f71cf37f415f19d1d/src/multinomials.jl#L18-L23

It appears that the size of the array is constant across iterations. Can the allocation of the array be moved to the constructor? Allocating an array every iteration is an unnecessary expense.

jishnub avatar Dec 08 '22 05:12 jishnub

Alternatively, does it even need to be a vector? Would an NTuple of Int work as well or even better?

tbeason avatar Jan 31 '23 19:01 tbeason

The size isn't known at compile-time, so a Tuple may not be as performant if there's no constant propagation.

jishnub avatar Feb 02 '23 04:02 jishnub

The benefit of allocating a new array is that it ensures there's no internal state that can become inconsistent, e.g. if iterating with @threads or if the user mutates the result.

ararslan avatar Feb 02 '23 17:02 ararslan