diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.cs index 0b0da448e4eba9..6f3dd4c01b73c8 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.cs @@ -113,7 +113,7 @@ public static Memory AsMemory(ReadOnlyMemory memory) => /// Thrown when or contains pointers. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Span Cast(Span span) + public static unsafe Span Cast(Span span) where TFrom : struct where TTo : struct { @@ -122,6 +122,9 @@ public static Span Cast(Span span) if (RuntimeHelpers.IsReferenceOrContainsReferences()) ThrowHelper.ThrowInvalidTypeWithPointersNotSupported(typeof(TTo)); + if (typeof(TFrom) == typeof(TTo)) + return *(Span*)&span; + // Use unsigned integers - unsigned division by constant (especially by power of 2) // and checked casts are faster and smaller. uint fromSize = (uint)Unsafe.SizeOf(); @@ -168,7 +171,7 @@ ref Unsafe.As(ref span._reference), /// Thrown when or contains pointers. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static ReadOnlySpan Cast(ReadOnlySpan span) + public static unsafe ReadOnlySpan Cast(ReadOnlySpan span) where TFrom : struct where TTo : struct { @@ -177,6 +180,9 @@ public static ReadOnlySpan Cast(ReadOnlySpan span) if (RuntimeHelpers.IsReferenceOrContainsReferences()) ThrowHelper.ThrowInvalidTypeWithPointersNotSupported(typeof(TTo)); + if (typeof(TFrom) == typeof(TTo)) + return *(ReadOnlySpan*)&span; + // Use unsigned integers - unsigned division by constant (especially by power of 2) // and checked casts are faster and smaller. uint fromSize = (uint)Unsafe.SizeOf();