Add Span and ReadOnlySpan from CoreCLR
authorJan Kotas <jkotas@microsoft.com>
Tue, 14 Mar 2017 16:06:56 +0000 (09:06 -0700)
committerJan Kotas <jkotas@microsoft.com>
Mon, 17 Apr 2017 14:05:34 +0000 (07:05 -0700)
Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
src/mscorlib/shared/System/ReadOnlySpan.cs
src/mscorlib/shared/System/Span.NonGeneric.cs
src/mscorlib/shared/System/Span.cs

index 35d4c4f..8ec431f 100644 (file)
@@ -20,6 +20,9 @@ namespace System
         /// <summary>A byref or a native ptr.</summary>
         private readonly ByReference<T> _pointer;
         /// <summary>The number of elements this ReadOnlySpan contains.</summary>
+#if PROJECTN
+        [Bound]
+#endif
         private readonly int _length;
 
         /// <summary>
@@ -164,14 +167,21 @@ namespace System
         /// </exception>
         public T this[int index]
         {
+#if PROJECTN
+            [BoundsChecking]
+            get
+            {
+                return Unsafe.Add(ref _pointer.Value, index);
+            }
+#else
             [MethodImpl(MethodImplOptions.AggressiveInlining)]
             get
             {
                 if ((uint)index >= (uint)_length)
                     ThrowHelper.ThrowIndexOutOfRangeException();
-
                 return Unsafe.Add(ref _pointer.Value, index);
             }
+#endif
         }
 
         /// <summary>
index acae5e4..4cdba21 100644 (file)
@@ -165,12 +165,12 @@ namespace System
             if (byteLength == 0)
                 return;
             
-#if AMD64
+#if AMD64 && CORECLR
             if (byteLength > 4096) goto PInvoke;
             Unsafe.InitBlockUnaligned(ref b, 0, (uint)byteLength);
             return;
-#else // AMD64
-            // TODO: Optimize this method on X86 machine
+#else
+            // TODO: Optimize other platforms to be on par with AMD64 CoreCLR
             // Note: It's important that this switch handles lengths at least up to 22.
             // See notes below near the main loop for why.
 
@@ -472,7 +472,7 @@ namespace System
             }
 
             return;
-#endif // AMD64
+#endif
             
             PInvoke:
             RuntimeImports.RhZeroMemory(ref b, byteLength);
index 913c4a2..82ea35f 100644 (file)
@@ -27,6 +27,9 @@ namespace System
         /// <summary>A byref or a native ptr.</summary>
         private readonly ByReference<T> _pointer;
         /// <summary>The number of elements this Span contains.</summary>
+#if PROJECTN
+        [Bound]
+#endif
         private readonly int _length;
 
         /// <summary>
@@ -179,14 +182,21 @@ namespace System
         /// </exception>
         public ref T this[int index]
         {
+#if PROJECTN
+            [BoundsChecking]
+            get
+            {
+                return ref Unsafe.Add(ref _pointer.Value, index);
+            }
+#else
             [MethodImpl(MethodImplOptions.AggressiveInlining)]
             get
             {
                 if ((uint)index >= (uint)_length)
                     ThrowHelper.ThrowIndexOutOfRangeException();
-
                 return ref Unsafe.Add(ref _pointer.Value, index);
             }
+#endif
         }
 
         /// <summary>