From 91e8708b975478731eced144152ba9d10b356194 Mon Sep 17 00:00:00 2001 From: Bojun-Feng <102875484+Bojun-Feng@users.noreply.github.com> Date: Fri, 17 Feb 2023 10:23:33 -0600 Subject: [PATCH] optimize type check efficiency --- .../Common/tests/System/Collections/IListTest.cs | 2 +- .../Collections/Immutable/ImmutableExtensions.cs | 2 +- .../Generic/Comparers/Comparer.Generic.Tests.cs | 2 +- .../Comparers/EqualityComparer.Generic.Tests.cs | 2 +- .../CompilerServices/ReadOnlyCollectionBuilder.cs | 2 +- .../Enumerables/AggregationMinMaxHelpers.cs | 6 +++--- .../src/System/SpanHelpers.T.cs | 14 +++++++------- 7 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/libraries/Common/tests/System/Collections/IListTest.cs b/src/libraries/Common/tests/System/Collections/IListTest.cs index 24fbbf2212512..c1872d27407b5 100644 --- a/src/libraries/Common/tests/System/Collections/IListTest.cs +++ b/src/libraries/Common/tests/System/Collections/IListTest.cs @@ -1742,7 +1742,7 @@ protected override sealed bool ItemsMustBeUnique protected override sealed bool ItemsMustBeNonNull { - get { return default(T) != null; } + get { return typeof(T).IsValueType; } } protected override sealed object GenerateItem() diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableExtensions.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableExtensions.cs index 08f32a5a70c53..111fe4b010e71 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableExtensions.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableExtensions.cs @@ -19,7 +19,7 @@ internal static bool IsValueType() #if NETCOREAPP return typeof(T).IsValueType; #else - if (default(T) != null) + if (typeof(T).IsValueType) { return true; } diff --git a/src/libraries/System.Collections/tests/Generic/Comparers/Comparer.Generic.Tests.cs b/src/libraries/System.Collections/tests/Generic/Comparers/Comparer.Generic.Tests.cs index c49388dd67773..4c66b6571e77d 100644 --- a/src/libraries/System.Collections/tests/Generic/Comparers/Comparer.Generic.Tests.cs +++ b/src/libraries/System.Collections/tests/Generic/Comparers/Comparer.Generic.Tests.cs @@ -78,7 +78,7 @@ public void Comparer_IComparerCompareWithObjectsNotOfMatchingTypeShouldThrow() // throw if both inputs are non-null and one of them is not of type T IComparer comparer = Comparer.Default; StrongBox notOfTypeT = new StrongBox(default(T)); - if (default(T) != null) // if default(T) is null these asserts will fail as IComparer.Compare returns early if either side is null + if (typeof(T).IsValueType) // if default(T) is null these asserts will fail as IComparer.Compare returns early if either side is null { AssertExtensions.Throws(null, () => comparer.Compare(notOfTypeT, default(T))); // lhs is the problem AssertExtensions.Throws(null, () => comparer.Compare(default(T), notOfTypeT)); // rhs is the problem diff --git a/src/libraries/System.Collections/tests/Generic/Comparers/EqualityComparer.Generic.Tests.cs b/src/libraries/System.Collections/tests/Generic/Comparers/EqualityComparer.Generic.Tests.cs index 8432bf76f6d5c..d2d16f1fa8b9e 100644 --- a/src/libraries/System.Collections/tests/Generic/Comparers/EqualityComparer.Generic.Tests.cs +++ b/src/libraries/System.Collections/tests/Generic/Comparers/EqualityComparer.Generic.Tests.cs @@ -85,7 +85,7 @@ public void EqualityComparer_IEqualityComparerEqualsWithObjectsNotOfMatchingType Assert.Equal(default(T) == null, comparer.Equals(null, default(T))); Assert.True(comparer.Equals(null, null)); - if (default(T) != null) // if default(T) is null this assert will fail as IEqualityComparer.Equals returns early if either input is null + if (typeof(T).IsValueType) // if default(T) is null this assert will fail as IEqualityComparer.Equals returns early if either input is null { AssertExtensions.Throws(null, () => comparer.Equals(notOfTypeT, default(T))); // lhs is the problem AssertExtensions.Throws(null, () => comparer.Equals(default(T), notOfTypeT)); // rhs is the problem diff --git a/src/libraries/System.Linq.Expressions/src/System/Runtime/CompilerServices/ReadOnlyCollectionBuilder.cs b/src/libraries/System.Linq.Expressions/src/System/Runtime/CompilerServices/ReadOnlyCollectionBuilder.cs index cd398f5dca14d..8e4d9c3d2ef66 100644 --- a/src/libraries/System.Linq.Expressions/src/System/Runtime/CompilerServices/ReadOnlyCollectionBuilder.cs +++ b/src/libraries/System.Linq.Expressions/src/System/Runtime/CompilerServices/ReadOnlyCollectionBuilder.cs @@ -451,7 +451,7 @@ private static bool IsCompatibleObject(object? value) private static void ValidateNullValue(object? value, string argument) { - if (value == null && default(T) != null) + if (value == null && typeof(T).IsValueType) { throw Error.InvalidNullValue(typeof(T), argument); } diff --git a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/Enumerables/AggregationMinMaxHelpers.cs b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/Enumerables/AggregationMinMaxHelpers.cs index 746c1a5b7496a..c0b9b09f86ece 100644 --- a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/Enumerables/AggregationMinMaxHelpers.cs +++ b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/Enumerables/AggregationMinMaxHelpers.cs @@ -31,7 +31,7 @@ internal static class AggregationMinMaxHelpers AssociativeAggregationOperator, T> aggregation = new AssociativeAggregationOperator, T>(source, new Pair(false, default!), null, - true, intermediateReduce, finalReduce, resultSelector, default(T) != null, QueryAggregationOptions.AssociativeCommutative); + true, intermediateReduce, finalReduce, resultSelector, typeof(T).IsValueType, QueryAggregationOptions.AssociativeCommutative); return aggregation.Aggregate(); } @@ -71,7 +71,7 @@ private static Func, T, Pair> MakeIntermediateReduceFunct // the existing accumulated result is equal to the sign requested by the function factory, // we will return a new pair that contains the current element as the best item. We will // ignore null elements (for reference and nullable types) in the input stream. - if ((default(T) != null || element != null) && + if ((typeof(T).IsValueType || element != null) && (!accumulator.First || Util.Sign(comparer.Compare(element, accumulator.Second)) == sign)) { return new Pair(true, element); @@ -98,7 +98,7 @@ private static Func, Pair, Pair> MakeFinalReduce if (element.First && (!accumulator.First || Util.Sign(comparer.Compare(element.Second, accumulator.Second)) == sign)) { - Debug.Assert(default(T) != null || element.Second != null, "nulls unexpected in final reduce"); + Debug.Assert(typeof(T).IsValueType || element.Second != null, "nulls unexpected in final reduce"); return new Pair(true, element.Second); } diff --git a/src/libraries/System.Private.CoreLib/src/System/SpanHelpers.T.cs b/src/libraries/System.Private.CoreLib/src/System/SpanHelpers.T.cs index 11c79ef0b76af..33c897ddc6f18 100644 --- a/src/libraries/System.Private.CoreLib/src/System/SpanHelpers.T.cs +++ b/src/libraries/System.Private.CoreLib/src/System/SpanHelpers.T.cs @@ -232,7 +232,7 @@ public static unsafe bool Contains(ref T searchSpace, T value, int length) wh nint index = 0; // Use nint for arithmetic to avoid unnecessary 64->32->64 truncations - if (default(T) != null || (object?)value != null) + if (typeof(T).IsValueType || (object?)value != null) { Debug.Assert(value is not null); @@ -303,7 +303,7 @@ public static unsafe int IndexOf(ref T searchSpace, T value, int length) wher Debug.Assert(length >= 0); nint index = 0; // Use nint for arithmetic to avoid unnecessary 64->32->64 truncations - if (default(T) != null || (object?)value != null) + if (typeof(T).IsValueType || (object?)value != null) { Debug.Assert(value is not null); @@ -393,7 +393,7 @@ public static int IndexOfAny(ref T searchSpace, T value0, T value1, int lengt T lookUp; int index = 0; - if (default(T) != null || ((object?)value0 != null && (object?)value1 != null)) + if (typeof(T).IsValueType || ((object?)value0 != null && (object?)value1 != null)) { Debug.Assert(value0 is not null && value1 is not null); @@ -499,7 +499,7 @@ public static int IndexOfAny(ref T searchSpace, T value0, T value1, T value2, T lookUp; int index = 0; - if (default(T) != null || ((object?)value0 != null && (object?)value1 != null && (object?)value2 != null)) + if (typeof(T).IsValueType || ((object?)value0 != null && (object?)value1 != null && (object?)value2 != null)) { Debug.Assert(value0 is not null && value1 is not null && value2 is not null); @@ -713,7 +713,7 @@ public static int LastIndexOf(ref T searchSpace, T value, int length) where T { Debug.Assert(length >= 0); - if (default(T) != null || (object?)value != null) + if (typeof(T).IsValueType || (object?)value != null) { Debug.Assert(value is not null); @@ -797,7 +797,7 @@ public static int LastIndexOfAny(ref T searchSpace, T value0, T value1, int l Debug.Assert(length >= 0); T lookUp; - if (default(T) != null || ((object?)value0 != null && (object?)value1 != null)) + if (typeof(T).IsValueType || ((object?)value0 != null && (object?)value1 != null)) { Debug.Assert(value0 is not null && value1 is not null); @@ -902,7 +902,7 @@ public static int LastIndexOfAny(ref T searchSpace, T value0, T value1, T val Debug.Assert(length >= 0); T lookUp; - if (default(T) != null || ((object?)value0 != null && (object?)value1 != null && (object?)value2 != null)) + if (typeof(T).IsValueType || ((object?)value0 != null && (object?)value1 != null && (object?)value2 != null)) { Debug.Assert(value0 is not null && value1 is not null && value2 is not null);