summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan Kotas <jkotas@microsoft.com>2018-10-15 21:39:18 -0700
committerGitHub <noreply@github.com>2018-10-15 21:39:18 -0700
commitd3ed7cb8da830657c376d4adb2862bc3c09cfef7 (patch)
tree4760e9a64feb639c74bea3125dc9c5b815b63528
parent85ed652fef5f1dec7c532bd4963dd3cde0199211 (diff)
downloadcoreclr-d3ed7cb8da830657c376d4adb2862bc3c09cfef7.tar.gz
coreclr-d3ed7cb8da830657c376d4adb2862bc3c09cfef7.tar.bz2
coreclr-d3ed7cb8da830657c376d4adb2862bc3c09cfef7.zip
Optimize Span.GetPinnableReference (#20428)
* Optimize Span.GetPinnableReference * CR feedback
-rw-r--r--src/System.Private.CoreLib/shared/System/ReadOnlySpan.Fast.cs8
-rw-r--r--src/System.Private.CoreLib/shared/System/Span.Fast.cs8
2 files changed, 14 insertions, 2 deletions
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.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
- public unsafe ref readonly T GetPinnableReference() => ref (_length != 0) ? ref _pointer.Value : ref Unsafe.AsRef<T>(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<T>(null);
+ if (_length != 0) ret = ref _pointer.Value;
+ return ref ret;
+ }
/// <summary>
/// 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 b3cfc8daff..3073592cd6 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 @@ namespace System
/// It can be used for pinning and is required to support the use of span within a fixed statement.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
- public unsafe ref T GetPinnableReference() => ref (_length != 0) ? ref _pointer.Value : ref Unsafe.AsRef<T>(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<T>(null);
+ if (_length != 0) ret = ref _pointer.Value;
+ return ref ret;
+ }
/// <summary>
/// Clears the contents of this span.