Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array allocation in iterating over MultiExponents #127

Open
jishnub opened this issue Dec 8, 2022 · 3 comments
Open

Array allocation in iterating over MultiExponents #127

jishnub opened this issue Dec 8, 2022 · 3 comments

Comments

@jishnub
Copy link
Member

jishnub commented Dec 8, 2022

# stars minus their consecutive
# position becomes their index
result = zeros(Int, m.nterms)
for (i, s) in enumerate(stars)
result[s-i+1] += 1
end

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
Copy link

tbeason commented Jan 31, 2023

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

@jishnub
Copy link
Member Author

jishnub commented Feb 2, 2023

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

@ararslan
Copy link
Member

ararslan commented Feb 2, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants