From 2f70a1156d83d021fe681134345d56c41794322a Mon Sep 17 00:00:00 2001 From: Huo Yaoyuan Date: Sat, 29 Aug 2020 22:10:12 +0800 Subject: [PATCH 1/4] Optimize Half.Equals. --- src/libraries/System.Private.CoreLib/src/System/Half.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Half.cs b/src/libraries/System.Private.CoreLib/src/System/Half.cs index 1840f3c61107e6..2fd5bcfba6dedb 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Half.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Half.cs @@ -422,11 +422,13 @@ public override bool Equals(object? obj) /// public bool Equals(Half other) { - if (this == other) + if (_value == other._value) { return true; } - return IsNaN(this) && IsNaN(other); + + return AreZero(this, other) + || (IsNaN(this) && IsNaN(other)); } /// From 8c17b9cf080f06190f61ad8b94b3c07f7f2bc161 Mon Sep 17 00:00:00 2001 From: Huo Yaoyuan Date: Sat, 29 Aug 2020 23:02:46 +0800 Subject: [PATCH 2/4] Add AggressiveInlining. --- src/libraries/System.Private.CoreLib/src/System/Half.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libraries/System.Private.CoreLib/src/System/Half.cs b/src/libraries/System.Private.CoreLib/src/System/Half.cs index 2fd5bcfba6dedb..0114ad9a9eb934 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Half.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Half.cs @@ -5,6 +5,7 @@ using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Numerics; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace System @@ -420,6 +421,7 @@ public override bool Equals(object? obj) /// /// Returns a value that indicates whether this instance is equal to a specified value. /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool Equals(Half other) { if (_value == other._value) From 3cdc0fba97c02516ae81f05ba3efa2d7e7a84350 Mon Sep 17 00:00:00 2001 From: Huo Yaoyuan Date: Sat, 29 Aug 2020 23:52:26 +0800 Subject: [PATCH 3/4] Change implementation to chained expression base on benchmark result. --- src/libraries/System.Private.CoreLib/src/System/Half.cs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Half.cs b/src/libraries/System.Private.CoreLib/src/System/Half.cs index 0114ad9a9eb934..03f0421dddb6e1 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Half.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Half.cs @@ -424,12 +424,8 @@ public override bool Equals(object? obj) [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool Equals(Half other) { - if (_value == other._value) - { - return true; - } - - return AreZero(this, other) + return _value == other._value + || AreZero(this, other) || (IsNaN(this) && IsNaN(other)); } From bf15c374ec12dd815113d82f2074b8fc06f8dedb Mon Sep 17 00:00:00 2001 From: Huo Yaoyuan Date: Sun, 30 Aug 2020 00:55:17 +0800 Subject: [PATCH 4/4] Don't add AggressiveInlining without real-world benchmarks. --- src/libraries/System.Private.CoreLib/src/System/Half.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Half.cs b/src/libraries/System.Private.CoreLib/src/System/Half.cs index 03f0421dddb6e1..98852c424c6a83 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Half.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Half.cs @@ -5,7 +5,6 @@ using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Numerics; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace System @@ -421,7 +420,6 @@ public override bool Equals(object? obj) /// /// Returns a value that indicates whether this instance is equal to a specified value. /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool Equals(Half other) { return _value == other._value