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

Optimize ListSet.setify #5706

Merged
merged 1 commit into from
Sep 28, 2018
Merged

Optimize ListSet.setify #5706

merged 1 commit into from
Sep 28, 2018

Conversation

rojepp
Copy link
Contributor

@rojepp rojepp commented Sep 27, 2018

Avoid allocatey List.foldBack and one List.rev.

I noticed that there are no tests for this, what's a good place to add some?

@cartermp cartermp requested a review from dsyme September 27, 2018 17:28
@cartermp
Copy link
Contributor

cc @forki for review as well, since he spent some time looking at this

@forki
Copy link
Contributor

forki commented Sep 27, 2018

There are no tests since this is only compiler internal.

@rojepp
Copy link
Contributor Author

rojepp commented Sep 27, 2018

@forki Yeah I just tested it in a script file.

@forki
Copy link
Contributor

forki commented Sep 27, 2018

It looks good. I'm actually wondering why I did not see that. Can you run a quick property based test in some external test project to make sure they are actually the same? But since it's green I'm confident it's OK.

@rojepp
Copy link
Contributor Author

rojepp commented Sep 27, 2018

I’ll do that in a while. I also figured that the CI would break if it was broken.

@rojepp
Copy link
Contributor Author

rojepp commented Sep 27, 2018

#r "paket:
source https://api.nuget.org/v3/index.json
nuget FsCheck //"
open FsCheck

let inline contains f x l = List.exists (f x) l
let insert f x l = if contains f x l then l else x::l
let setify f l = List.foldBack (insert f) (List.rev l) [] |> List.rev
let setifynew f l = List.fold (fun acc x -> insert f x acc) [] l |> List.rev
let oldAndNewIdentical (xs:list<int>) = setify (=) xs = setifynew (=) xs

Check.Quick oldAndNewIdentical

FsCheck reports:

Ok, passed 100 tests.

@forki
Copy link
Contributor

forki commented Sep 27, 2018 via email

@KevinRansom KevinRansom merged commit 7f7ef16 into dotnet:master Sep 28, 2018
@@ -226,9 +226,8 @@ module ListSet =
| (h::t) -> if contains f h l1 then h::intersect f l1 t else intersect f l1 t
| [] -> []

(* NOTE: quadratic! *)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why was the comment removed? It is still quadratic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My mistake, sorry. While it is faster, it is clearly still quadratic. This has already been merged, do you want a PR to bring the comment back?

auduchinok pushed a commit to auduchinok/fsharp that referenced this pull request Mar 21, 2019
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.

5 participants