Improve RuntimeHelpers.GetSubArray (dotnet/coreclr#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)
commitd6075bee2ad5778f8f10ca0c7c16b5a6566f437f
treedf9dccd8e923d5e63ff85a75dab3903f85edc4a2
parent52556e23670e498e78b050d6814071c03d965030
Improve RuntimeHelpers.GetSubArray (dotnet/coreclr#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()`

Commit migrated from https://github.com/dotnet/coreclr/commit/5fd24c69f75a1222a4d9b7abaed1c5712d47bd06
src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.cs