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

Improve performance of Array.GetLowerBound(0) - #4988

Closed
hughbe wants to merge 1 commit into
dotnet:masterfrom
hughbe:optimize-getlowerbound0
Closed

hughbe wants to merge 1 commit into
dotnet:masterfrom
hughbe:optimize-getlowerbound0

Conversation

@hughbe

@hughbe hughbe commented May 16, 2016 •

Copy link
Copy Markdown

Array.GetLowerBounds(0) is an extremely common operation on an Array: it is called pretty much everytime you call an Array method (Copy, Contains, IndexOf, BinarySearch, GetEnumerator, LastIndexOf, Reverse, Sort)

Currently, the implentation of GetLowerBound calls native code and validates parameters etc.
It can be found here

Instead, we can check immediately for the LowerBound of an array for dimension 0.

Currently, this PR is a slight WIP, as I don't know how to conduct perf tests for a locally built mscorlib. If someone could help me with this I'd really appreciate that. I expect perf to improve for GetLowerBound(0) but to worsen very slightly for GetLowerBound(dimension) where dimension > 0 as we have one additional branch and method invoke.

I think arrays are only created with custom lower bounds via CreateInstance(Type, int[], int[]) but let me know if I'm wrong here, and I will update the code to set _lowerBound0 where necessary.

/cc @jkotas @jamesqo

[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
public extern int GetUpperBound(int dimension);

private int _lowerBound0 = 0;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Huh? An new field added to the Array class?

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.

Yes, this is how we short circuit GetLowerBound(0). Is this a problem

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Yep, it's a problem. Fields cannot be added to the Array class as its layout (or to be precise, the layout of types derived from Array) is determined by the runtime. And even if it was possible you certainly don't want to increase the size of array objects by adding a field.

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.

Well shucks I had no idea! Ah well eh, closing this up

@hughbe hughbe closed this May 16, 2016
@hughbe
hughbe deleted the optimize-getlowerbound0 branch May 16, 2016 20:16
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.

3 participants