Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Conversation

mjp41
Copy link
Member

@mjp41 mjp41 commented Sep 22, 2016

The existing implementation of Min/Max calls InOrderTreeWalk,
which perform an allocation of a stack for traversing the tree.
But when you are finding the smallest or largest element in a
tree there is no need to perform this allocation. This is an
optimised version that directly finds the smallest or largest
element.

This is an important use case for using a sorted set as a priority
queue. In microbenchmarking, with the following code I
found a 30-40% speed up:

        for(int j=0; j < 10; j++) {
            var s = new SortedSet<object>();
            for(int n = 0; n < 100000; n++) {
                s.Add(n);
            }

            int i = 0;
            while(s.Min != null) 
            {
                var o = s.Min;
                s.Remove(o);
                i++;
                if(i%2 == 0) {
                    s.Add((int)o * 2);
                }
            }
        }

The existing implementation of Min/Max calls InOrderWalkTree,
which perform an allocation of a stack for traversing the tree.
But when you are finding the smallest or largest element in a
tree there is no need to perform this allocation.  This is an
optimised version that directly finds the smallest or largest
element.
Copy link
Member

@stephentoub stephentoub left a comment

Choose a reason for hiding this comment

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

Other than the two nits, LGTM. Thanks for finding/fixing this, @mjp41.

return default(T);
}

var current = _root;
Copy link
Member

Choose a reason for hiding this comment

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

Nit: please replace var with Node. We try to only use var when the type is obvious from the right-hand side of the assignment.

return default(T);
}

var current = _root;
Copy link
Member

Choose a reason for hiding this comment

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

Ditto

@mjp41
Copy link
Member Author

mjp41 commented Sep 22, 2016

Thanks @stephentoub, I have fixed the code review feedback.

There is a similar problem with SortedDictionary, but it is much harder to fix as I think it requires an API change. I'll raise an issue for this, as the choices need discussing.

@stephentoub
Copy link
Member

@dotnet-bot Test Innerloop CentOS7.1 Release Build and Test please ("Failed to mkdirs")

@stephentoub stephentoub merged commit 9c416bc into dotnet:master Sep 23, 2016
@karelz karelz modified the milestone: 1.2.0 Dec 3, 2016
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
Fixed performance of Min/Max in SortedSet.

Commit migrated from dotnet/corefx@9c416bc
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants