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

make nextPermutation faster #497

Closed
wants to merge 1 commit into from

Conversation

toyboot4e
Copy link
Contributor

@toyboot4e toyboot4e commented Jul 20, 2024

This PR makes nextPermutation like 10 times faster, resolving the TODO from 2014:

-                  -- TODO: make tuple unboxed
-                  let (kval',k') = if prev < cur then (prev,i-1) else (kval,k)
+                  let (!kval',!k') = if prev < cur then (prev,i-1) else (kval,k)

Let's do it!

Edit: stToPrim (suggested by @gksato) makes it even faster. With stToPrim, the bangs are not needed for the speed, but I preferred explicit strict evaluation.


If anyone is interested, here's my AtCoder submissions for a simple speed comparsion:

@Shimuuar
Copy link
Contributor

LGTM. Extra strictness won't cause any problems since kval' and k' will be passed to strict function loop immediately

I make simple benchmark and this change reduced execution time 5× and allocations 9×. Most gains seems to come from stToPrim. Probably with simpler program GHC is better able pick up strictness.

@toyboot4e do you plan any more optimizations?

@gksato
Copy link
Contributor

gksato commented Jul 21, 2024

In our private conversation on Twitter, oh, X (in Japanese), I have suggested some optimizations to him/her, and (s)he suggested that I take this over. I'm submitting superceding PR.

@toyboot4e
Copy link
Contributor Author

Thank you! Closing in favor of #498.

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

Successfully merging this pull request may close these issues.

3 participants