Skip to content

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:

@toyboot4e toyboot4e force-pushed the unbox-next-permutation branch from b39874a to ee88c23 Compare July 21, 2024 03:24
@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