From 5de539c2196411ae4c7075f40e0702a61b056382 Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Mon, 8 Sep 2025 17:31:30 +0100 Subject: [PATCH 1/2] Avoid local for span length in`MemoryExtensions.Replace` --- .../src/System/MemoryExtensions.cs | 40 +++++++++---------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs b/src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs index 96fc6033bf583b..27fbf36a786969 100644 --- a/src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs +++ b/src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs @@ -4420,8 +4420,6 @@ public static void Sort(this Span keys, Span items, [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe void Replace(this Span span, T oldValue, T newValue) where T : IEquatable? { - nuint length = (uint)span.Length; - if (RuntimeHelpers.IsBitwiseEquatable()) { if (sizeof(T) == sizeof(byte)) @@ -4432,7 +4430,7 @@ public static unsafe void Replace(this Span span, T oldValue, T newValue) ref src, Unsafe.BitCast(oldValue), Unsafe.BitCast(newValue), - length); + (uint)span.Length); return; } else if (sizeof(T) == sizeof(ushort)) @@ -4444,7 +4442,7 @@ public static unsafe void Replace(this Span span, T oldValue, T newValue) ref src, Unsafe.BitCast(oldValue), Unsafe.BitCast(newValue), - length); + (uint)span.Length); return; } else if (sizeof(T) == sizeof(int)) @@ -4455,7 +4453,7 @@ public static unsafe void Replace(this Span span, T oldValue, T newValue) ref src, Unsafe.BitCast(oldValue), Unsafe.BitCast(newValue), - length); + (uint)span.Length); return; } else if (sizeof(T) == sizeof(long)) @@ -4466,13 +4464,13 @@ public static unsafe void Replace(this Span span, T oldValue, T newValue) ref src, Unsafe.BitCast(oldValue), Unsafe.BitCast(newValue), - length); + (uint)span.Length); return; } } ref T src2 = ref MemoryMarshal.GetReference(span); - SpanHelpers.Replace(ref src2, ref src2, oldValue, newValue, length); + SpanHelpers.Replace(ref src2, ref src2, oldValue, newValue, (uint)span.Length); } /// @@ -4579,13 +4577,12 @@ static void ReplaceComparer(Span span, T oldValue, T newValue, IEqualityCompa [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe void Replace(this ReadOnlySpan source, Span destination, T oldValue, T newValue) where T : IEquatable? { - nuint length = (uint)source.Length; - if (length == 0) + if (source.Length == 0) { return; } - if (length > (uint)destination.Length) + if (source.Length > destination.Length) { ThrowHelper.ThrowArgumentException_DestinationTooShort(); } @@ -4610,7 +4607,7 @@ ref Unsafe.As(ref src), ref Unsafe.As(ref dst), Unsafe.BitCast(oldValue), Unsafe.BitCast(newValue), - length); + (uint)source.Length); return; } else if (sizeof(T) == sizeof(ushort)) @@ -4621,7 +4618,7 @@ ref Unsafe.As(ref src), ref Unsafe.As(ref dst), Unsafe.BitCast(oldValue), Unsafe.BitCast(newValue), - length); + (uint)source.Length); return; } else if (sizeof(T) == sizeof(int)) @@ -4631,7 +4628,7 @@ ref Unsafe.As(ref src), ref Unsafe.As(ref dst), Unsafe.BitCast(oldValue), Unsafe.BitCast(newValue), - length); + (uint)source.Length); return; } else if (sizeof(T) == sizeof(long)) @@ -4641,12 +4638,12 @@ ref Unsafe.As(ref src), ref Unsafe.As(ref dst), Unsafe.BitCast(oldValue), Unsafe.BitCast(newValue), - length); + (uint)source.Length); return; } } - SpanHelpers.Replace(ref src, ref dst, oldValue, newValue, length); + SpanHelpers.Replace(ref src, ref dst, oldValue, newValue, (uint)source.Length); } /// @@ -4663,13 +4660,12 @@ ref Unsafe.As(ref dst), [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe void Replace(this ReadOnlySpan source, Span destination, T oldValue, T newValue, IEqualityComparer? comparer = null) { - nuint length = (uint)source.Length; - if (length == 0) + if (source.Length == 0) { return; } - if (length > (uint)destination.Length) + if (source.Length > destination.Length) { ThrowHelper.ThrowArgumentException_DestinationTooShort(); } @@ -4696,7 +4692,7 @@ ref Unsafe.As(ref src), ref Unsafe.As(ref dst), Unsafe.BitCast(oldValue), Unsafe.BitCast(newValue), - length); + (uint)source.Length); return; } else if (sizeof(T) == sizeof(ushort)) @@ -4707,7 +4703,7 @@ ref Unsafe.As(ref src), ref Unsafe.As(ref dst), Unsafe.BitCast(oldValue), Unsafe.BitCast(newValue), - length); + (uint)source.Length); return; } else if (sizeof(T) == sizeof(int)) @@ -4717,7 +4713,7 @@ ref Unsafe.As(ref src), ref Unsafe.As(ref dst), Unsafe.BitCast(oldValue), Unsafe.BitCast(newValue), - length); + (uint)source.Length); return; } else if (sizeof(T) == sizeof(long)) @@ -4727,7 +4723,7 @@ ref Unsafe.As(ref src), ref Unsafe.As(ref dst), Unsafe.BitCast(oldValue), Unsafe.BitCast(newValue), - length); + (uint)source.Length); return; } } From 580fac4a7bacbaee858e0bf0f24585b4ffe1cfd9 Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Tue, 9 Sep 2025 22:52:41 +0100 Subject: [PATCH 2/2] Keep local and avoid `nuint` cast --- .../src/System/MemoryExtensions.cs | 40 ++++++++++--------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs b/src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs index 27fbf36a786969..d72dd9995458ba 100644 --- a/src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs +++ b/src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs @@ -4420,6 +4420,8 @@ public static void Sort(this Span keys, Span items, [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe void Replace(this Span span, T oldValue, T newValue) where T : IEquatable? { + uint length = (uint)span.Length; + if (RuntimeHelpers.IsBitwiseEquatable()) { if (sizeof(T) == sizeof(byte)) @@ -4430,7 +4432,7 @@ public static unsafe void Replace(this Span span, T oldValue, T newValue) ref src, Unsafe.BitCast(oldValue), Unsafe.BitCast(newValue), - (uint)span.Length); + length); return; } else if (sizeof(T) == sizeof(ushort)) @@ -4442,7 +4444,7 @@ public static unsafe void Replace(this Span span, T oldValue, T newValue) ref src, Unsafe.BitCast(oldValue), Unsafe.BitCast(newValue), - (uint)span.Length); + length); return; } else if (sizeof(T) == sizeof(int)) @@ -4453,7 +4455,7 @@ public static unsafe void Replace(this Span span, T oldValue, T newValue) ref src, Unsafe.BitCast(oldValue), Unsafe.BitCast(newValue), - (uint)span.Length); + length); return; } else if (sizeof(T) == sizeof(long)) @@ -4464,13 +4466,13 @@ public static unsafe void Replace(this Span span, T oldValue, T newValue) ref src, Unsafe.BitCast(oldValue), Unsafe.BitCast(newValue), - (uint)span.Length); + length); return; } } ref T src2 = ref MemoryMarshal.GetReference(span); - SpanHelpers.Replace(ref src2, ref src2, oldValue, newValue, (uint)span.Length); + SpanHelpers.Replace(ref src2, ref src2, oldValue, newValue, length); } /// @@ -4577,12 +4579,13 @@ static void ReplaceComparer(Span span, T oldValue, T newValue, IEqualityCompa [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe void Replace(this ReadOnlySpan source, Span destination, T oldValue, T newValue) where T : IEquatable? { - if (source.Length == 0) + uint length = (uint)source.Length; + if (length == 0) { return; } - if (source.Length > destination.Length) + if (length > (uint)destination.Length) { ThrowHelper.ThrowArgumentException_DestinationTooShort(); } @@ -4607,7 +4610,7 @@ ref Unsafe.As(ref src), ref Unsafe.As(ref dst), Unsafe.BitCast(oldValue), Unsafe.BitCast(newValue), - (uint)source.Length); + length); return; } else if (sizeof(T) == sizeof(ushort)) @@ -4618,7 +4621,7 @@ ref Unsafe.As(ref src), ref Unsafe.As(ref dst), Unsafe.BitCast(oldValue), Unsafe.BitCast(newValue), - (uint)source.Length); + length); return; } else if (sizeof(T) == sizeof(int)) @@ -4628,7 +4631,7 @@ ref Unsafe.As(ref src), ref Unsafe.As(ref dst), Unsafe.BitCast(oldValue), Unsafe.BitCast(newValue), - (uint)source.Length); + length); return; } else if (sizeof(T) == sizeof(long)) @@ -4638,12 +4641,12 @@ ref Unsafe.As(ref src), ref Unsafe.As(ref dst), Unsafe.BitCast(oldValue), Unsafe.BitCast(newValue), - (uint)source.Length); + length); return; } } - SpanHelpers.Replace(ref src, ref dst, oldValue, newValue, (uint)source.Length); + SpanHelpers.Replace(ref src, ref dst, oldValue, newValue, length); } /// @@ -4660,12 +4663,13 @@ ref Unsafe.As(ref dst), [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe void Replace(this ReadOnlySpan source, Span destination, T oldValue, T newValue, IEqualityComparer? comparer = null) { - if (source.Length == 0) + uint length = (uint)source.Length; + if (length == 0) { return; } - if (source.Length > destination.Length) + if (length > (uint)destination.Length) { ThrowHelper.ThrowArgumentException_DestinationTooShort(); } @@ -4692,7 +4696,7 @@ ref Unsafe.As(ref src), ref Unsafe.As(ref dst), Unsafe.BitCast(oldValue), Unsafe.BitCast(newValue), - (uint)source.Length); + length); return; } else if (sizeof(T) == sizeof(ushort)) @@ -4703,7 +4707,7 @@ ref Unsafe.As(ref src), ref Unsafe.As(ref dst), Unsafe.BitCast(oldValue), Unsafe.BitCast(newValue), - (uint)source.Length); + length); return; } else if (sizeof(T) == sizeof(int)) @@ -4713,7 +4717,7 @@ ref Unsafe.As(ref src), ref Unsafe.As(ref dst), Unsafe.BitCast(oldValue), Unsafe.BitCast(newValue), - (uint)source.Length); + length); return; } else if (sizeof(T) == sizeof(long)) @@ -4723,7 +4727,7 @@ ref Unsafe.As(ref src), ref Unsafe.As(ref dst), Unsafe.BitCast(oldValue), Unsafe.BitCast(newValue), - (uint)source.Length); + length); return; } }