Combinatorics.jl
Combinatorics.jl copied to clipboard
Array allocation in iterating over `MultiExponents`
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.
Alternatively, does it even need to be a vector? Would an NTuple of Int work as well or even better?
The size isn't known at compile-time, so a Tuple may not be as performant if there's no constant propagation.
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.