Improve RuntimeHelpers.GetSubArray (#23829)
authorStephen Toub <stoub@microsoft.com>
Tue, 9 Apr 2019 11:21:40 +0000 (07:21 -0400)
committerGitHub <noreply@github.com>
Tue, 9 Apr 2019 11:21:40 +0000 (07:21 -0400)
commit5fd24c69f75a1222a4d9b7abaed1c5712d47bd06
treed4f6eebf741808593f9b19f6069b5b1966747332
parent13421bf4d01816fb24b974695acb9a9698193575
Improve RuntimeHelpers.GetSubArray (#23829)

* Improve RuntimeHelpers.GetSubArray

This change does three things.

First, it fixes `GetSubArray` to work when the supplied array is actually a `U[]` where `U : T`.  Currently this case ends up throwing an exception inside of span, which doesn't like working with arrays covariantly.

Second, it fixes argument validation so that we throw an ArgumentNullException if the input array is null rather than NullReferenceException.

Third, it improves the performance of `GetSubArray` for the 95% common case where either `T` is a value type or the type of the array matches the `T` type specified.

* Only use `Array.Empty<T>` when `typeof(T[]) == array.GetType()`
src/System.Private.CoreLib/shared/System/Runtime/CompilerServices/RuntimeHelpers.cs