-
Notifications
You must be signed in to change notification settings - Fork 49
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
sort and sortBy are not stack safe #192
Comments
I'm thinking something like this would work: sortBy :: forall a. (a -> a -> Ordering) -> List a -> List a
sortBy cmp = fromFoldable <<< Array.sortBy cmp <<< Array.fromFoldable But it's very slow. It should be much faster if we change fromFoldable :: forall f. Foldable f => f ~> Array
fromFoldable = fromFoldableImpl foldr Edit: Another option, but still slower than the original list version (up to 10k elements, then list version overflows). sortBy :: forall a. (a -> a -> Ordering) -> List a -> List a
sortBy cmp = fromFoldable <<< Array.sortBy cmp <<< toUnfoldable |
Have you benchmarked what effect it would have by swapping fromFoldable to use foldl rather than foldr? |
FYI the Haskell version depends more on the GHC runtime being smarter about stack usage, which is separate from laziness. In Haskell it’s very rare that you have to worry about stack overflows, because of how the GHC runtime works. |
I just ran into this while solving Advent of Code. Any recommended workarounds in the meantime? |
Should we swap out this current version (which I assume depends on Haskell's laziness) for something that just reuses Array's
sortBy
? I think that might end up being faster even in situations where we're not having stack issues.purescript-lists/src/Data/List.purs
Lines 447 to 482 in 6d8e30e
The text was updated successfully, but these errors were encountered: