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

Commit dd52e03

Browse files
committed
Add Span and ReadOnlySpan from CoreCLR
Signed-off-by: dotnet-bot <[email protected]>
1 parent 40c33f6 commit dd52e03

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

src/mscorlib/shared/System/ReadOnlySpan.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ public struct ReadOnlySpan<T>
2020
/// <summary>A byref or a native ptr.</summary>
2121
private readonly ByReference<T> _pointer;
2222
/// <summary>The number of elements this ReadOnlySpan contains.</summary>
23+
#if PROJECTN
24+
[Bound]
25+
#endif
2326
private readonly int _length;
2427

2528
/// <summary>
@@ -164,14 +167,21 @@ public ref T DangerousGetPinnableReference()
164167
/// </exception>
165168
public T this[int index]
166169
{
170+
#if PROJECTN
171+
[BoundsChecking]
172+
get
173+
{
174+
return Unsafe.Add(ref _pointer.Value, index);
175+
}
176+
#else
167177
[MethodImpl(MethodImplOptions.AggressiveInlining)]
168178
get
169179
{
170180
if ((uint)index >= (uint)_length)
171181
ThrowHelper.ThrowIndexOutOfRangeException();
172-
173182
return Unsafe.Add(ref _pointer.Value, index);
174183
}
184+
#endif
175185
}
176186

177187
/// <summary>

src/mscorlib/shared/System/Span.NonGeneric.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,12 @@ internal static unsafe void ClearWithoutReferences(ref byte b, nuint byteLength)
165165
if (byteLength == 0)
166166
return;
167167

168-
#if AMD64
168+
#if AMD64 && CORECLR
169169
if (byteLength > 4096) goto PInvoke;
170170
Unsafe.InitBlockUnaligned(ref b, 0, (uint)byteLength);
171171
return;
172-
#else // AMD64
173-
// TODO: Optimize this method on X86 machine
172+
#else
173+
// TODO: Optimize other platforms to be on par with AMD64 CoreCLR
174174
// Note: It's important that this switch handles lengths at least up to 22.
175175
// See notes below near the main loop for why.
176176

@@ -472,7 +472,7 @@ internal static unsafe void ClearWithoutReferences(ref byte b, nuint byteLength)
472472
}
473473

474474
return;
475-
#endif // AMD64
475+
#endif
476476

477477
PInvoke:
478478
RuntimeImports.RhZeroMemory(ref b, byteLength);

src/mscorlib/shared/System/Span.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ public struct Span<T>
2727
/// <summary>A byref or a native ptr.</summary>
2828
private readonly ByReference<T> _pointer;
2929
/// <summary>The number of elements this Span contains.</summary>
30+
#if PROJECTN
31+
[Bound]
32+
#endif
3033
private readonly int _length;
3134

3235
/// <summary>
@@ -179,14 +182,21 @@ public ref T DangerousGetPinnableReference()
179182
/// </exception>
180183
public ref T this[int index]
181184
{
185+
#if PROJECTN
186+
[BoundsChecking]
187+
get
188+
{
189+
return ref Unsafe.Add(ref _pointer.Value, index);
190+
}
191+
#else
182192
[MethodImpl(MethodImplOptions.AggressiveInlining)]
183193
get
184194
{
185195
if ((uint)index >= (uint)_length)
186196
ThrowHelper.ThrowIndexOutOfRangeException();
187-
188197
return ref Unsafe.Add(ref _pointer.Value, index);
189198
}
199+
#endif
190200
}
191201

192202
/// <summary>

0 commit comments

Comments
 (0)