Skip to content
This repository was archived by the owner on Oct 4, 2020. It is now read-only.

More efficient toUnfoldable without the Ord constraint #76

Merged
merged 2 commits into from
Dec 8, 2016

Conversation

sammthomson
Copy link
Contributor

Keep a List of Maps as the unfold state instead of a single Map.
Appending Maps together was linear time (making the entire function quadratic?).
Prepending to a List is constant time and doesn't require an Ord k instance.

Keep a List of Maps as the unfold state, instead of a single Map.
Unioning Maps was linear time (making the entire function quadratic?).
Prepending to a List is constant time and doesn't require an `Ord k` instance.
Copy link
Contributor

@hdgarrood hdgarrood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Have you done any testing to measure the performance improvement?

Two left k v right ->
Just $ Tuple (Tuple k v) (left : right : tl)
Three left k1 v1 mid k2 v2 right ->
Just $ Tuple (Tuple k1 v1) ((singleton k2 v2) : left : mid : right : tl)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nit: I'd remove the brackets around singleton k2 v2.

@sammthomson
Copy link
Contributor Author

Here's benchmark results for smaller maps, showing the quadratic behavior:

image
(json)
Here's the code. It's using {1: 1, ..., n: n} as test cases.

It gets really slow for big maps. The stack blows with maps sized around 10,000 because List's append is not stack-safe. The new version doesn't have this issue because it's not using <>.

@hdgarrood
Copy link
Contributor

Wow, that's pretty significant! Ok then, LGTM 👍

@garyb garyb merged commit c673935 into purescript-deprecated:master Dec 8, 2016
@garyb
Copy link
Member

garyb commented Dec 8, 2016

Awesome, thanks!

@joshuahhh joshuahhh mentioned this pull request Jan 19, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants