summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Adams <thundercat@illyriad.co.uk>2017-02-09 15:11:09 +0000
committerJan Kotas <jkotas@microsoft.com>2017-02-09 07:11:09 -0800
commit7d81c60da7846d73df31d657a5c8c27aaed15d83 (patch)
tree64a3becb28c0460a22244889884722e43c4eb1e5
parentb2e2c4434a1796d9d245ae3be727a675f4d63e36 (diff)
downloadcoreclr-7d81c60da7846d73df31d657a5c8c27aaed15d83.tar.gz
coreclr-7d81c60da7846d73df31d657a5c8c27aaed15d83.tar.bz2
coreclr-7d81c60da7846d73df31d657a5c8c27aaed15d83.zip
Inlinable ArraySegment ctor (#9433)
-rw-r--r--src/mscorlib/src/System/ArraySegment.cs14
-rw-r--r--src/mscorlib/src/System/ThrowHelper.cs23
2 files changed, 25 insertions, 12 deletions
diff --git a/src/mscorlib/src/System/ArraySegment.cs b/src/mscorlib/src/System/ArraySegment.cs
index b767e7bd77..03556e492a 100644
--- a/src/mscorlib/src/System/ArraySegment.cs
+++ b/src/mscorlib/src/System/ArraySegment.cs
@@ -15,8 +15,6 @@
using System.Collections;
using System.Collections.Generic;
-using System.Runtime.InteropServices;
-using System.Diagnostics;
using System.Diagnostics.Contracts;
namespace System
@@ -46,14 +44,10 @@ namespace System
public ArraySegment(T[] array, int offset, int count)
{
- if (array == null)
- ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
- if (offset < 0)
- ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.offset, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
- if (count < 0)
- ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
- if (array.Length - offset < count)
- ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
+ // Validate arguments, check is minimal instructions with reduced branching for inlinable fast-path
+ // Failure should be rare and location determination and message is delegated to failure functions
+ if (array == null || (offset | count) < 0 || (array.Length - offset < count))
+ ThrowHelper.ThrowArraySegmentCtorValidationFailedExceptions(array, offset, count);
Contract.EndContractBlock();
_array = array;
diff --git a/src/mscorlib/src/System/ThrowHelper.cs b/src/mscorlib/src/System/ThrowHelper.cs
index 5ba7a305a9..a48bb60a56 100644
--- a/src/mscorlib/src/System/ThrowHelper.cs
+++ b/src/mscorlib/src/System/ThrowHelper.cs
@@ -39,7 +39,6 @@ namespace System {
using Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
- using System.Diagnostics;
using System.Diagnostics.Contracts;
[Pure]
@@ -125,8 +124,12 @@ namespace System {
throw GetArgumentException(resource, argument);
}
+ private static ArgumentNullException GetArgumentNullException(ExceptionArgument argument) {
+ return new ArgumentNullException(GetArgumentName(argument));
+ }
+
internal static void ThrowArgumentNullException(ExceptionArgument argument) {
- throw new ArgumentNullException(GetArgumentName(argument));
+ throw GetArgumentNullException(argument);
}
internal static void ThrowArgumentNullException(ExceptionResource resource) {
@@ -209,6 +212,22 @@ namespace System {
throw GetInvalidOperationException(ExceptionResource.InvalidOperation_EnumOpCantHappen);
}
+ internal static void ThrowArraySegmentCtorValidationFailedExceptions(Array array, int offset, int count) {
+ throw GetArraySegmentCtorValidationFailedException(array, offset, count);
+ }
+
+ private static Exception GetArraySegmentCtorValidationFailedException(Array array, int offset, int count) {
+ if (array == null)
+ return GetArgumentNullException(ExceptionArgument.array);
+ if (offset < 0)
+ return GetArgumentOutOfRangeException(ExceptionArgument.offset, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
+ if (count < 0)
+ return GetArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
+
+ Debug.Assert(array.Length - offset < count);
+ return GetArgumentException(ExceptionResource.Argument_InvalidOffLen);
+ }
+
private static ArgumentException GetArgumentException(ExceptionResource resource) {
return new ArgumentException(GetResourceString(resource));
}