JuliaMath / Combinatorics.jl

A combinatorics library for Julia
http://juliamath.github.io/Combinatorics.jl/dev/
Other
214 stars 58 forks source link

Array allocation in iterating over `MultiExponents` #127

Open jishnub opened 1 year ago

jishnub commented 1 year ago

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.

tbeason commented 1 year ago

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

jishnub commented 1 year ago

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

ararslan commented 1 year ago

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.