diff --git a/src/System.Private.CoreLib/shared/System/ReadOnlySpan.Fast.cs b/src/System.Private.CoreLib/shared/System/ReadOnlySpan.Fast.cs index 4fb039a0fc85..8ba8fd66b66f 100644 --- a/src/System.Private.CoreLib/shared/System/ReadOnlySpan.Fast.cs +++ b/src/System.Private.CoreLib/shared/System/ReadOnlySpan.Fast.cs @@ -153,7 +153,13 @@ public ref readonly T this[int index] /// It can be used for pinning and is required to support the use of span within a fixed statement. /// [EditorBrowsable(EditorBrowsableState.Never)] - public unsafe ref readonly T GetPinnableReference() => ref (_length != 0) ? ref _pointer.Value : ref Unsafe.AsRef(null); + public unsafe ref readonly T GetPinnableReference() + { + // Ensure that the native code has just one forward branch that is predicted-not-taken. + ref T ret = ref Unsafe.AsRef(null); + if (_length != 0) ret = ref _pointer.Value; + return ref ret; + } /// /// Copies the contents of this read-only span into destination span. If the source diff --git a/src/System.Private.CoreLib/shared/System/Span.Fast.cs b/src/System.Private.CoreLib/shared/System/Span.Fast.cs index b3cfc8daff7b..3073592cd6ea 100644 --- a/src/System.Private.CoreLib/shared/System/Span.Fast.cs +++ b/src/System.Private.CoreLib/shared/System/Span.Fast.cs @@ -158,7 +158,13 @@ public ref T this[int index] /// It can be used for pinning and is required to support the use of span within a fixed statement. /// [EditorBrowsable(EditorBrowsableState.Never)] - public unsafe ref T GetPinnableReference() => ref (_length != 0) ? ref _pointer.Value : ref Unsafe.AsRef(null); + public unsafe ref T GetPinnableReference() + { + // Ensure that the native code has just one forward branch that is predicted-not-taken. + ref T ret = ref Unsafe.AsRef(null); + if (_length != 0) ret = ref _pointer.Value; + return ref ret; + } /// /// Clears the contents of this span.