Repository navigation
Cache GetLowerBound in Array.Reverse and Array.Sort - #5019
Conversation
|
@hughbe Awesome, was just about to submit a PR with the |
| throw new RankException(Environment.GetResourceString("Rank_MultiDimNotSupported")); | ||
| if (items != null && keys.GetLowerBound(0) != items.GetLowerBound(0)) | ||
| int keysLowerBound = keys.GetLowerBound(0); | ||
| int itemsLowerBound = items.GetLowerBound(0); |
There was a problem hiding this comment.
The original code was special-casing items == null - I think you still need to do that. E.g.:
int lowerBound = keys.GetLowerBound(0);
if (items != null && lowerBound != items.GetLowerBound(0))
...
you can use lowerBound for both keys and items since you know they are equal...
- Avoids calling internal native method 4 times per sort: 2 times is enough - Avoids calling internal native method 2 times per reverse: 1 time is enough
| if (index < keysLowerBound || length < 0) | ||
| throw new ArgumentOutOfRangeException((length<0 ? "length" : "index"), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); | ||
| if (keys.Length - (index - keys.GetLowerBound(0)) < length || (items != null && (index - items.GetLowerBound(0)) > items.Length - length)) | ||
| if (keys.Length - (index - keysLowerBound) < length || (items != null && (index - items.GetLowerBound(0)) > items.Length - length)) |
There was a problem hiding this comment.
You could replace items.GetLowerBound(0) with keysLowerBound as well because of we have checked that the keys and items lower bound is the same above.
|
Note: After you're done with this PR, don't forget to port to the corert version: https://github.com/dotnet/corert/blob/master/src/System.Private.CoreLib/src/System/Array.cs |
|
Thanks @jamesqo, will do |
|
Thank you! |
) - Avoids calling internal native method 4 times per sort: 2 times is enough - Avoids calling internal native method 2 times per reverse: 1 time is enough Commit migrated from dotnet/coreclr@a2fb9c1
enough
enough
Thanks to @jamesqo's great
GetLowerBound(0)improvement, the method is superfast.However, each time we call it we still need to call a native method.
This means that across methods in Array, we cache the result of
GetLowerBound(0). However, Sort and Reverse don't do this, so we can boost their perf slightly and be consistent./cc @jkotas @mikedn @jamesqo