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

Could SortedSet.InOrderTreeWalk use recursion instead of allocating? #18769

Closed
jamesqo opened this issue Sep 29, 2016 · 6 comments
Closed

Could SortedSet.InOrderTreeWalk use recursion instead of allocating? #18769

jamesqo opened this issue Sep 29, 2016 · 6 comments
Labels
area-System.Collections enhancement Product code improvement that does NOT require public API changes/additions help wanted [up-for-grabs] Good issue for external contributors tenet-performance Performance related issue
Milestone

Comments

@jamesqo
Copy link
Contributor

jamesqo commented Sep 29, 2016

SortedSet.InOrderTreeWalk currently uses a stack on which the nodes to walk are pushed/popped: https://github.com/dotnet/corefx/blob/master/src/System.Collections/src/System/Collections/Generic/SortedSet.cs#L234

It looks like it would be possible to implement this recursively instead, i.e. InOrderTreeWalk(left); action(current); InOrderTreeWalk(right) and eliminate the Stack allocation.

@stephentoub
Copy link
Member

Given that there's little risk of stack overflow due to the balancing of the tree, seems reasonable to explore and measure. At the same time it may also be useful to separate the reverse checks out so that there's only one check/branch for reverse to go down a different path that does right;current;left instead of left;current;right. One could also look at having the function take not only a delegate but also a T that would be passed into the delegate, allowing the call site to avoid the closure/delegate allocation that most of them have.

@ianhays
Copy link
Contributor

ianhays commented Oct 3, 2016

I'd be interested to see how much space you save by removing the stack allocation in favor of recursion. My gut says it would be slower than the approach we're using because of the added overhead in method calls, but I agree it's worth investigation.

@jamesqo
Copy link
Contributor Author

jamesqo commented Oct 3, 2016

because of the added overhead in method calls,

@ianhays Neither Stack.Pop or Stack.Push inline currently.

@ianhays
Copy link
Contributor

ianhays commented Feb 13, 2017

@jamesqo are you still interested in looking into this and getting some perf results to see if it's a good enhancement?

@jamesqo
Copy link
Contributor Author

jamesqo commented Feb 13, 2017

@ianhays, I haven't forgotten about this issue. I am thinking it would be good to cleanup the SortedSet code a bit more first (I have a PR for part 2 of the cleanup open at dotnet/corefx#16061) before making optimizations. I also think that maybe we could just remove InOrderTreeWalk entirely and do the recursion ourselves at every call site, avoiding the delegate invocations-- it shouldn't be too hard, especially after we get C# 7 local functions in corefx.

@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the Future milestone Jan 31, 2020
@maryamariyan maryamariyan added the untriaged New issue has not been triaged by the area owner label Feb 23, 2020
@layomia layomia removed the untriaged New issue has not been triaged by the area owner label Jun 24, 2020
@eiriktsarpalis
Copy link
Member

This was investigated by @johnthcall in #56561. While there are substantial perf improvements in a recursion-based approach, we decided not make that change out of abundance of caution.

@ghost ghost locked as resolved and limited conversation to collaborators Nov 10, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Collections enhancement Product code improvement that does NOT require public API changes/additions help wanted [up-for-grabs] Good issue for external contributors tenet-performance Performance related issue
Projects
None yet
Development

No branches or pull requests

7 participants