Special-Case Ascending Value Types for System.Linq.OrderBy #336
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
2nd of n optimizations lifted from my exploration of an alternative
System.LinqimplementationCistern.Linq. There are lots more goodies there, but only so many that will survive the transfer toSystem.Linq!(Previous corefx optimization)
This one (to a first order approximation) adds O(N) operations to the sort (huh?) and also forces dual arrays to be sorted (via
Array.Sort(keys, elements, ...)) so that doesn't sound very good now does it? (Of course it remains a stable sort)But...
Anyway, here are some results from a slightly modified performance test that just uses a variety of sorting sizes gives the following results:
So there is some degraded performance for non-value types for very small collections, but these are minor, and only for < 10 elements - due to additional overhead of checking type, etc., but the performance of a known value type (
DateTime) is significant - ~35% of the time for 256 elements.A value type which has a custom comparer also benefits (although by a much more modest amount). (i.e. not just limited to the case of Array Sort optimizing the primitive types)
But!! If the comparer was significantly complex then this could slow it down (due to the additional O(N) operations) but as N increases, the ~O(N log N) will drown this out.
... and this really isn't just for value types as N grows it overcomes the additional O(N) and has faster results on reference types (well I tried it with the name strings as per
System.Linq.Tests.Perf_OrderBy.OrderByStringand around the few hundred items it was faster). But the current state of this PR is just for value types.