From 3a058450784d6ceb2ca8fd4b7b1ca8d3d6d4e8a9 Mon Sep 17 00:00:00 2001 From: Levi Broderick Date: Thu, 4 Apr 2019 22:46:23 -0700 Subject: [PATCH] Improve performance of Memory.Span property getter (#23750) --- src/System.Private.CoreLib/shared/System/Memory.cs | 14 +++++++++++--- src/System.Private.CoreLib/shared/System/ReadOnlyMemory.cs | 14 +++++++++++--- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/System.Private.CoreLib/shared/System/Memory.cs b/src/System.Private.CoreLib/shared/System/Memory.cs index d468a8c..e61ada7 100644 --- a/src/System.Private.CoreLib/shared/System/Memory.cs +++ b/src/System.Private.CoreLib/shared/System/Memory.cs @@ -12,6 +12,14 @@ using EditorBrowsableState = System.ComponentModel.EditorBrowsableState; using Internal.Runtime.CompilerServices; +#if BIT64 +using nint = System.Int64; +using nuint = System.UInt64; +#else // BIT64 +using nint = System.Int32; +using nuint = System.UInt32; +#endif // BIT64 + namespace System { /// @@ -372,12 +380,12 @@ namespace System // least to be in-bounds when compared with the original Memory instance, so using the span won't // AV the process. - int desiredStartIndex = _index & ReadOnlyMemory.RemoveFlagsBitMask; + nuint desiredStartIndex = (uint)_index & (uint)ReadOnlyMemory.RemoveFlagsBitMask; int desiredLength = _length; #if BIT64 // See comment in Span.Slice for how this works. - if ((ulong)(uint)desiredStartIndex + (ulong)(uint)desiredLength > (ulong)(uint)lengthOfUnderlyingSpan) + if ((ulong)desiredStartIndex + (ulong)(uint)desiredLength > (ulong)(uint)lengthOfUnderlyingSpan) { ThrowHelper.ThrowArgumentOutOfRangeException(); } @@ -388,7 +396,7 @@ namespace System } #endif - refToReturn = ref Unsafe.Add(ref refToReturn, desiredStartIndex); + refToReturn = ref Unsafe.Add(ref refToReturn, (IntPtr)(void*)desiredStartIndex); lengthOfUnderlyingSpan = desiredLength; } diff --git a/src/System.Private.CoreLib/shared/System/ReadOnlyMemory.cs b/src/System.Private.CoreLib/shared/System/ReadOnlyMemory.cs index be664e6..b7814cf 100644 --- a/src/System.Private.CoreLib/shared/System/ReadOnlyMemory.cs +++ b/src/System.Private.CoreLib/shared/System/ReadOnlyMemory.cs @@ -12,6 +12,14 @@ using EditorBrowsableState = System.ComponentModel.EditorBrowsableState; using Internal.Runtime.CompilerServices; +#if BIT64 +using nint = System.Int64; +using nuint = System.UInt64; +#else // BIT64 +using nint = System.Int32; +using nuint = System.UInt32; +#endif // BIT64 + namespace System { /// @@ -294,12 +302,12 @@ namespace System // least to be in-bounds when compared with the original Memory instance, so using the span won't // AV the process. - int desiredStartIndex = _index & RemoveFlagsBitMask; + nuint desiredStartIndex = (uint)_index & (uint)RemoveFlagsBitMask; int desiredLength = _length; #if BIT64 // See comment in Span.Slice for how this works. - if ((ulong)(uint)desiredStartIndex + (ulong)(uint)desiredLength > (ulong)(uint)lengthOfUnderlyingSpan) + if ((ulong)desiredStartIndex + (ulong)(uint)desiredLength > (ulong)(uint)lengthOfUnderlyingSpan) { ThrowHelper.ThrowArgumentOutOfRangeException(); } @@ -310,7 +318,7 @@ namespace System } #endif - refToReturn = ref Unsafe.Add(ref refToReturn, desiredStartIndex); + refToReturn = ref Unsafe.Add(ref refToReturn, (IntPtr)(void*)desiredStartIndex); lengthOfUnderlyingSpan = desiredLength; } -- 2.7.4