diff --git a/src/libraries/System.Private.CoreLib/src/System/Memory.cs b/src/libraries/System.Private.CoreLib/src/System/Memory.cs index 989cac29c57c1f..5ec18c4cb842ec 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Memory.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Memory.cs @@ -263,7 +263,7 @@ public Memory Slice(int start, int length) /// /// Returns a span from the memory. /// - public unsafe Span Span + public Span Span { [MethodImpl(MethodImplOptions.AggressiveInlining)] get @@ -327,7 +327,9 @@ public unsafe Span Span // least to be in-bounds when compared with the original Memory instance, so using the span won't // AV the process. + // We use 'nuint' because it gives us a free early zero-extension to 64 bits when running on a 64-bit platform. nuint desiredStartIndex = (uint)_index & (uint)ReadOnlyMemory.RemoveFlagsBitMask; + int desiredLength = _length; #if TARGET_64BIT @@ -343,7 +345,7 @@ public unsafe Span Span } #endif - refToReturn = ref Unsafe.Add(ref refToReturn, (IntPtr)(void*)desiredStartIndex); + refToReturn = ref Unsafe.Add(ref refToReturn, desiredStartIndex); lengthOfUnderlyingSpan = desiredLength; } diff --git a/src/libraries/System.Private.CoreLib/src/System/ReadOnlyMemory.cs b/src/libraries/System.Private.CoreLib/src/System/ReadOnlyMemory.cs index 6b59ac75e57663..ee2059ef0170aa 100644 --- a/src/libraries/System.Private.CoreLib/src/System/ReadOnlyMemory.cs +++ b/src/libraries/System.Private.CoreLib/src/System/ReadOnlyMemory.cs @@ -192,7 +192,7 @@ public ReadOnlyMemory Slice(int start, int length) /// /// Returns a span from the memory. /// - public unsafe ReadOnlySpan Span + public ReadOnlySpan Span { [MethodImpl(MethodImplOptions.AggressiveInlining)] get @@ -249,7 +249,9 @@ public unsafe ReadOnlySpan Span // least to be in-bounds when compared with the original Memory instance, so using the span won't // AV the process. + // We use 'nuint' because it gives us a free early zero-extension to 64 bits when running on a 64-bit platform. nuint desiredStartIndex = (uint)_index & (uint)RemoveFlagsBitMask; + int desiredLength = _length; #if TARGET_64BIT @@ -265,7 +267,7 @@ public unsafe ReadOnlySpan Span } #endif - refToReturn = ref Unsafe.Add(ref refToReturn, (IntPtr)(void*)desiredStartIndex); + refToReturn = ref Unsafe.Add(ref refToReturn, desiredStartIndex); lengthOfUnderlyingSpan = desiredLength; }