From d3ed7cb8da830657c376d4adb2862bc3c09cfef7 Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Mon, 15 Oct 2018 21:39:18 -0700 Subject: Optimize Span.GetPinnableReference (#20428) * Optimize Span.GetPinnableReference * CR feedback --- src/System.Private.CoreLib/shared/System/ReadOnlySpan.Fast.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'src/System.Private.CoreLib/shared/System/ReadOnlySpan.Fast.cs') diff --git a/src/System.Private.CoreLib/shared/System/ReadOnlySpan.Fast.cs b/src/System.Private.CoreLib/shared/System/ReadOnlySpan.Fast.cs index 4fb039a0fc..8ba8fd66b6 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 @@ namespace System /// 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 -- cgit v1.2.3