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

Cache GetLowerBound in Array.Reverse and Array.Sort - #5019

Merged
jkotas merged 2 commits into
dotnet:masterfrom
hughbe:cache-lb
May 18, 2016
Merged

jkotas merged 2 commits into
dotnet:masterfrom
hughbe:cache-lb

Conversation

@hughbe

@hughbe hughbe commented May 17, 2016 •

Copy link
Copy Markdown
  • 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

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

@jamesqo

jamesqo commented May 17, 2016

Copy link
Copy Markdown

@hughbe Awesome, was just about to submit a PR with the Reverse part of those changes. (Luckily I've also been fiddling with the native code as well, so not all of my work was for naught.) LGTM.

Comment thread src/mscorlib/src/System/Array.cs Outdated
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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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...

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Oh crumbs yeah

- 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
Comment thread src/mscorlib/src/System/Array.cs Outdated
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))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Nice one, will do

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

done

@jamesqo

jamesqo commented May 18, 2016

Copy link
Copy Markdown

@hughbe

hughbe commented May 18, 2016

Copy link
Copy Markdown
Author

Thanks @jamesqo, will do

@jkotas
jkotas merged commit a2fb9c1 into dotnet:master May 18, 2016
@jkotas

jkotas commented May 18, 2016

Copy link
Copy Markdown
Member

Thank you!

@hughbe
hughbe deleted the cache-lb branch May 18, 2016 21:32
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
)

- 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
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants