From c8d3f3f88722eac4ab33a44cd71c7829b1bb0ee3 Mon Sep 17 00:00:00 2001 From: maumar Date: Fri, 13 Dec 2024 00:10:30 -0800 Subject: [PATCH] Fix to #35239 - EF9: SaveChanges() is significantly slower in .NET9 vs. .NET8 when using .ToJson() Mapping vs. PostgreSQL Legacy POCO mapping Problem was that as part of AOT refactoring we changed way that we build comparers. Specifically, comparers of collections - ListOfValueTypesComparer, ListOfNullableValueTypesComparer and ListOfReferenceTypesComparer. Before those list comparer Compare, Hashcode and Snapshot methods would take as argument element comparer, which was responsible for comparing elements. We need to be able to express these in code for AOT but we are not able to generate constant of type ValueComparer (or ValueComparer) that was needed. As a solution, each comparer now stores expression describing how it can be constructed, so we use that instead (as we are perfectly capable to expressing that in code form). Problem is that now every time compare, snapshot or hashcode method is called for array type, we construct new ValueComparer for the element type. As a result in the reported case we would generate 1000s of comparers which all have to be compiled and that causes huge overhead. Fix is to pass relevant func from the element comparer to the outer comparer. We only passed the element comparer object to the outer Compare/Hashcode/Snapshot function to call that relevant func. This way we avoid constructing redundant comparers. For ListOfReferenceTypesComparer it's a bit trickier - TElement of the outer comparer doesn't always match TElement of the element comparer. If we detect this case, we see if the lambdas (Equals, Hashcode, Snapshot) are compatible between what we expect (in the list comparer method) and what we get from the element comparer. If they are compatible we just pass the element lambda to the list method. If they are not compatible we rewrite the element lambda so that they match. Fixes #35239 --- .../Internal/StringDictionaryComparer.cs | 103 ++++++++++++++---- .../ListOfNullableValueTypesComparer.cs | 42 +++---- .../ListOfReferenceTypesComparer.cs | 100 ++++++++++++++--- .../ListOfValueTypesComparer.cs | 42 +++---- 4 files changed, 200 insertions(+), 87 deletions(-) diff --git a/src/EFCore.Cosmos/ChangeTracking/Internal/StringDictionaryComparer.cs b/src/EFCore.Cosmos/ChangeTracking/Internal/StringDictionaryComparer.cs index aa4589d54b8..f631a1f743c 100644 --- a/src/EFCore.Cosmos/ChangeTracking/Internal/StringDictionaryComparer.cs +++ b/src/EFCore.Cosmos/ChangeTracking/Internal/StringDictionaryComparer.cs @@ -15,13 +15,13 @@ namespace Microsoft.EntityFrameworkCore.Cosmos.ChangeTracking.Internal; public sealed class StringDictionaryComparer : ValueComparer, IInfrastructure { private static readonly MethodInfo CompareMethod = typeof(StringDictionaryComparer).GetMethod( - nameof(Compare), BindingFlags.Static | BindingFlags.NonPublic, [typeof(object), typeof(object), typeof(ValueComparer)])!; + nameof(Compare), BindingFlags.Static | BindingFlags.NonPublic, [typeof(object), typeof(object), typeof(Func)])!; private static readonly MethodInfo GetHashCodeMethod = typeof(StringDictionaryComparer).GetMethod( - nameof(GetHashCode), BindingFlags.Static | BindingFlags.NonPublic, [typeof(IEnumerable), typeof(ValueComparer)])!; + nameof(GetHashCode), BindingFlags.Static | BindingFlags.NonPublic, [typeof(IEnumerable), typeof(Func)])!; private static readonly MethodInfo SnapshotMethod = typeof(StringDictionaryComparer).GetMethod( - nameof(Snapshot), BindingFlags.Static | BindingFlags.NonPublic, [typeof(object), typeof(ValueComparer)])!; + nameof(Snapshot), BindingFlags.Static | BindingFlags.NonPublic, [typeof(object), typeof(Func)])!; /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -52,14 +52,40 @@ ValueComparer IInfrastructure.Instance var prm1 = Expression.Parameter(typeof(object), "a"); var prm2 = Expression.Parameter(typeof(object), "b"); + // we check the compatibility of expected equals lambda signature Func + // vs method we actually get from the element comparer, which would be typed to its generic argument + // if the expected is assignable from actual we can just do simple call... + var expectedSignature = typeof(Func); + var actualSignature = typeof(Func<,,>).MakeGenericType(elementComparer.Type, elementComparer.Type, typeof(bool)); + if (expectedSignature.IsAssignableFrom(actualSignature)) + { + return Expression.Lambda>( + Expression.Call( + CompareMethod, + prm1, + prm2, + elementComparer.EqualsExpression), + prm1, + prm2); + } + + // ...otherwise we need to rewrite the actual lambda (as we can't change the expected signature) + // in that case we are rewriting the inner lambda parameters to TElement and cast to the element comparer + // type argument in the body, so that semantics of the element comparison func don't change + var newInnerPrm1 = Expression.Parameter(typeof(TElement), "a"); + var newInnerPrm2 = Expression.Parameter(typeof(TElement), "b"); + return Expression.Lambda>( Expression.Call( CompareMethod, prm1, prm2, -#pragma warning disable EF9100 - elementComparer.ConstructorExpression), -#pragma warning restore EF9100 + Expression.Lambda( + elementComparer.ExtractEqualsBody( + Expression.Convert(newInnerPrm1, elementComparer.Type), + Expression.Convert(newInnerPrm2, elementComparer.Type)), + newInnerPrm1, + newInnerPrm2)), prm1, prm2); } @@ -68,15 +94,32 @@ private static Expression> GetHashCodeLambda(ValueComparer ele { var prm = Expression.Parameter(typeof(object), "o"); + var expectedSignature = typeof(Func); + var actualSignature = typeof(Func<,>).MakeGenericType(elementComparer.Type, typeof(int)); + if (expectedSignature.IsAssignableFrom(actualSignature)) + { + return Expression.Lambda>( + Expression.Call( + GetHashCodeMethod, + Expression.Convert( + prm, + typeof(IEnumerable)), + elementComparer.HashCodeExpression), + prm); + } + + var newInnerPrm = Expression.Parameter(typeof(TElement), "o"); + return Expression.Lambda>( Expression.Call( GetHashCodeMethod, Expression.Convert( prm, typeof(IEnumerable)), -#pragma warning disable EF9100 - elementComparer.ConstructorExpression), -#pragma warning restore EF9100 + Expression.Lambda( + elementComparer.ExtractHashCodeBody( + Expression.Convert(newInnerPrm, elementComparer.Type)), + newInnerPrm)), prm); } @@ -84,17 +127,35 @@ private static Expression> SnapshotLambda(ValueComparer ele { var prm = Expression.Parameter(typeof(object), "source"); + var expectedSignature = typeof(Func); + var actualSignature = typeof(Func<,>).MakeGenericType(elementComparer.Type, elementComparer.Type); + if (expectedSignature.IsAssignableFrom(actualSignature)) + { + return Expression.Lambda>( + Expression.Call( + SnapshotMethod, + prm, + elementComparer.SnapshotExpression), + prm); + } + + var newInnerPrm = Expression.Parameter(typeof(TElement), "source"); + + // note we need to also convert the result of inner lambda back to TElement return Expression.Lambda>( Expression.Call( SnapshotMethod, prm, -#pragma warning disable EF9100 - elementComparer.ConstructorExpression), -#pragma warning restore EF9100 + Expression.Lambda( + Expression.Convert( + elementComparer.ExtractSnapshotBody( + Expression.Convert(newInnerPrm, elementComparer.Type)), + typeof(TElement)), + newInnerPrm)), prm); } - private static bool Compare(object? a, object? b, ValueComparer elementComparer) + private static bool Compare(object? a, object? b, Func elementCompare) { if (ReferenceEquals(a, b)) { @@ -121,7 +182,7 @@ private static bool Compare(object? a, object? b, ValueComparer elementComparer) foreach (var pair in aDictionary) { if (!bDictionary.TryGetValue(pair.Key, out var bValue) - || !elementComparer.Equals(pair.Value, bValue)) + || !elementCompare(pair.Value, bValue)) { return false; } @@ -133,17 +194,17 @@ private static bool Compare(object? a, object? b, ValueComparer elementComparer) throw new InvalidOperationException( CosmosStrings.BadDictionaryType( (a is IDictionary ? b : a).GetType().ShortDisplayName(), - typeof(IDictionary<,>).MakeGenericType(typeof(string), elementComparer.Type).ShortDisplayName())); + typeof(IDictionary<,>).MakeGenericType(typeof(string), typeof(TElement)).ShortDisplayName())); } - private static int GetHashCode(IEnumerable source, ValueComparer elementComparer) + private static int GetHashCode(IEnumerable source, Func elementGetHashCode) { if (source is not IReadOnlyDictionary sourceDictionary) { throw new InvalidOperationException( CosmosStrings.BadDictionaryType( source.GetType().ShortDisplayName(), - typeof(IList<>).MakeGenericType(elementComparer.Type).ShortDisplayName())); + typeof(IList<>).MakeGenericType(typeof(TElement)).ShortDisplayName())); } var hash = new HashCode(); @@ -151,26 +212,26 @@ private static int GetHashCode(IEnumerable source, ValueComparer elementComparer foreach (var pair in sourceDictionary) { hash.Add(pair.Key); - hash.Add(pair.Value == null ? 0 : elementComparer.GetHashCode(pair.Value)); + hash.Add(pair.Value == null ? 0 : elementGetHashCode(pair.Value)); } return hash.ToHashCode(); } - private static IReadOnlyDictionary Snapshot(object source, ValueComparer elementComparer) + private static IReadOnlyDictionary Snapshot(object source, Func elementSnapshot) { if (source is not IReadOnlyDictionary sourceDictionary) { throw new InvalidOperationException( CosmosStrings.BadDictionaryType( source.GetType().ShortDisplayName(), - typeof(IDictionary<,>).MakeGenericType(typeof(string), elementComparer.Type).ShortDisplayName())); + typeof(IDictionary<,>).MakeGenericType(typeof(string), typeof(TElement)).ShortDisplayName())); } var snapshot = new Dictionary(); foreach (var pair in sourceDictionary) { - snapshot[pair.Key] = pair.Value == null ? default : (TElement?)elementComparer.Snapshot(pair.Value); + snapshot[pair.Key] = pair.Value == null ? default : (TElement?)elementSnapshot(pair.Value); } return snapshot; diff --git a/src/EFCore/ChangeTracking/ListOfNullableValueTypesComparer.cs b/src/EFCore/ChangeTracking/ListOfNullableValueTypesComparer.cs index d95a40877db..15f3e957fd1 100644 --- a/src/EFCore/ChangeTracking/ListOfNullableValueTypesComparer.cs +++ b/src/EFCore/ChangeTracking/ListOfNullableValueTypesComparer.cs @@ -33,15 +33,15 @@ public sealed class ListOfNullableValueTypesComparer : private static readonly MethodInfo CompareMethod = typeof(ListOfNullableValueTypesComparer).GetMethod( nameof(Compare), BindingFlags.Static | BindingFlags.NonPublic, - [typeof(IEnumerable), typeof(IEnumerable), typeof(ValueComparer)])!; + [typeof(IEnumerable), typeof(IEnumerable), typeof(Func)])!; private static readonly MethodInfo GetHashCodeMethod = typeof(ListOfNullableValueTypesComparer).GetMethod( nameof(GetHashCode), BindingFlags.Static | BindingFlags.NonPublic, - [typeof(IEnumerable), typeof(ValueComparer)])!; + [typeof(IEnumerable), typeof(Func)])!; private static readonly MethodInfo SnapshotMethod = typeof(ListOfNullableValueTypesComparer).GetMethod( nameof(Snapshot), BindingFlags.Static | BindingFlags.NonPublic, - [typeof(IEnumerable), typeof(ValueComparer)])!; + [typeof(IEnumerable), typeof(Func)])!; /// /// Creates a new instance of the list comparer. @@ -67,15 +67,13 @@ ValueComparer IInfrastructure.Instance var prm1 = Expression.Parameter(typeof(IEnumerable), "a"); var prm2 = Expression.Parameter(typeof(IEnumerable), "b"); - //(a, b) => Compare(a, b, (ValueComparer)elementComparer) + //(a, b) => Compare(a, b, elementComparer.Equals) return Expression.Lambda?, IEnumerable?, bool>>( Expression.Call( CompareMethod, prm1, prm2, - Expression.Convert( - elementComparer.ConstructorExpression, - typeof(ValueComparer))), + elementComparer.EqualsExpression), prm1, prm2); } @@ -84,14 +82,12 @@ ValueComparer IInfrastructure.Instance { var prm = Expression.Parameter(typeof(IEnumerable), "o"); - //o => GetHashCode(o, (ValueComparer)elementComparer) + //o => GetHashCode(o, elementComparer.GetHashCode) return Expression.Lambda, int>>( Expression.Call( GetHashCodeMethod, prm, - Expression.Convert( - elementComparer.ConstructorExpression, - typeof(ValueComparer))), + elementComparer.HashCodeExpression), prm); } @@ -99,18 +95,16 @@ ValueComparer IInfrastructure.Instance { var prm = Expression.Parameter(typeof(IEnumerable), "source"); - //source => Snapshot(source, (ValueComparer)elementComparer) + //source => Snapshot(source, elementComparer.Snapshot) return Expression.Lambda, IEnumerable>>( Expression.Call( SnapshotMethod, prm, - Expression.Convert( - elementComparer.ConstructorExpression, - typeof(ValueComparer))), + elementComparer.SnapshotExpression), prm); } - private static bool Compare(IEnumerable? a, IEnumerable? b, ValueComparer elementComparer) + private static bool Compare(IEnumerable? a, IEnumerable? b, Func elementCompare) { if (ReferenceEquals(a, b)) { @@ -152,7 +146,7 @@ private static bool Compare(IEnumerable? a, IEnumerable? b return false; } - if (!elementComparer.Equals(el1, el2)) + if (!elementCompare(el1, el2)) { return false; } @@ -164,29 +158,29 @@ private static bool Compare(IEnumerable? a, IEnumerable? b throw new InvalidOperationException( CoreStrings.BadListType( (a is IList ? b : a).GetType().ShortDisplayName(), - typeof(IList<>).MakeGenericType(elementComparer.Type.MakeNullable()).ShortDisplayName())); + typeof(IList<>).MakeGenericType(typeof(TElement).MakeNullable()).ShortDisplayName())); } - private static int GetHashCode(IEnumerable source, ValueComparer elementComparer) + private static int GetHashCode(IEnumerable source, Func elementGetHashCode) { var hash = new HashCode(); foreach (var el in source) { - hash.Add(el == null ? 0 : elementComparer.GetHashCode(el)); + hash.Add(el == null ? 0 : elementGetHashCode(el)); } return hash.ToHashCode(); } - private static IList Snapshot(IEnumerable source, ValueComparer elementComparer) + private static IList Snapshot(IEnumerable source, Func elementSnapshot) { if (source is not IList sourceList) { throw new InvalidOperationException( CoreStrings.BadListType( source.GetType().ShortDisplayName(), - typeof(IList<>).MakeGenericType(elementComparer.Type.MakeNullable()).ShortDisplayName())); + typeof(IList<>).MakeGenericType(typeof(TElement).MakeNullable()).ShortDisplayName())); } if (IsArray) @@ -195,7 +189,7 @@ private static int GetHashCode(IEnumerable source, ValueComparer source, ValueComparer() : (IList)Activator.CreateInstance()!; foreach (var e in sourceList) { - snapshot.Add(e == null ? null : elementComparer.Snapshot(e)); + snapshot.Add(e == null ? null : elementSnapshot(e)); } return IsReadOnly diff --git a/src/EFCore/ChangeTracking/ListOfReferenceTypesComparer.cs b/src/EFCore/ChangeTracking/ListOfReferenceTypesComparer.cs index e0e77528d87..207a7c6b732 100644 --- a/src/EFCore/ChangeTracking/ListOfReferenceTypesComparer.cs +++ b/src/EFCore/ChangeTracking/ListOfReferenceTypesComparer.cs @@ -30,13 +30,13 @@ public sealed class ListOfReferenceTypesComparer : Valu && typeof(TConcreteList).GetGenericTypeDefinition() == typeof(ReadOnlyCollection<>)); private static readonly MethodInfo CompareMethod = typeof(ListOfReferenceTypesComparer).GetMethod( - nameof(Compare), BindingFlags.Static | BindingFlags.NonPublic, [typeof(object), typeof(object), typeof(ValueComparer)])!; + nameof(Compare), BindingFlags.Static | BindingFlags.NonPublic, [typeof(object), typeof(object), typeof(Func)])!; private static readonly MethodInfo GetHashCodeMethod = typeof(ListOfReferenceTypesComparer).GetMethod( - nameof(GetHashCode), BindingFlags.Static | BindingFlags.NonPublic, [typeof(IEnumerable), typeof(ValueComparer)])!; + nameof(GetHashCode), BindingFlags.Static | BindingFlags.NonPublic, [typeof(IEnumerable), typeof(Func)])!; private static readonly MethodInfo SnapshotMethod = typeof(ListOfReferenceTypesComparer).GetMethod( - nameof(Snapshot), BindingFlags.Static | BindingFlags.NonPublic, [typeof(object), typeof(ValueComparer)])!; + nameof(Snapshot), BindingFlags.Static | BindingFlags.NonPublic, [typeof(object), typeof(Func)])!; /// /// Creates a new instance of the list comparer. @@ -62,13 +62,40 @@ ValueComparer IInfrastructure.Instance var prm1 = Expression.Parameter(typeof(object), "a"); var prm2 = Expression.Parameter(typeof(object), "b"); - // (a, b) => Compare(a, b, elementComparer) + // we check the compatibility of expected equals lambda signature Func + // vs method we actually get from the element comparer, which would be typed to its generic argument + // if the expected is assignable from actual we can just do simple call... + var expectedSignature = typeof(Func); + var actualSignature = typeof(Func<,,>).MakeGenericType(elementComparer.Type, elementComparer.Type, typeof(bool)); + if (expectedSignature.IsAssignableFrom(actualSignature)) + { + return Expression.Lambda>( + Expression.Call( + CompareMethod, + prm1, + prm2, + elementComparer.EqualsExpression), + prm1, + prm2); + } + + // ...otherwise we need to rewrite the actual lambda (as we can't change the expected signature) + // in that case we are rewriting the inner lambda parameters to TElement and cast to the element comparer + // type argument in the body, so that semantics of the element comparison func don't change + var newInnerPrm1 = Expression.Parameter(typeof(TElement), "a"); + var newInnerPrm2 = Expression.Parameter(typeof(TElement), "b"); + return Expression.Lambda>( Expression.Call( CompareMethod, prm1, prm2, - elementComparer.ConstructorExpression), + Expression.Lambda( + elementComparer.ExtractEqualsBody( + Expression.Convert(newInnerPrm1, elementComparer.Type), + Expression.Convert(newInnerPrm2, elementComparer.Type)), + newInnerPrm1, + newInnerPrm2)), prm1, prm2); } @@ -77,14 +104,32 @@ private static Expression> GetHashCodeLambda(ValueComparer ele { var prm = Expression.Parameter(typeof(object), "o"); - //o => GetHashCode((IEnumerable)o, elementComparer) + var expectedSignature = typeof(Func); + var actualSignature = typeof(Func<,>).MakeGenericType(elementComparer.Type, typeof(int)); + if (expectedSignature.IsAssignableFrom(actualSignature)) + { + return Expression.Lambda>( + Expression.Call( + GetHashCodeMethod, + Expression.Convert( + prm, + typeof(IEnumerable)), + elementComparer.HashCodeExpression), + prm); + } + + var newInnerPrm = Expression.Parameter(typeof(TElement), "o"); + return Expression.Lambda>( Expression.Call( GetHashCodeMethod, Expression.Convert( prm, typeof(IEnumerable)), - elementComparer.ConstructorExpression), + Expression.Lambda( + elementComparer.ExtractHashCodeBody( + Expression.Convert(newInnerPrm, elementComparer.Type)), + newInnerPrm)), prm); } @@ -92,16 +137,35 @@ private static Expression> SnapshotLambda(ValueComparer ele { var prm = Expression.Parameter(typeof(object), "source"); - //source => Snapshot(source, elementComparer) + var expectedSignature = typeof(Func); + var actualSignature = typeof(Func<,>).MakeGenericType(elementComparer.Type, elementComparer.Type); + if (expectedSignature.IsAssignableFrom(actualSignature)) + { + return Expression.Lambda>( + Expression.Call( + SnapshotMethod, + prm, + elementComparer.SnapshotExpression), + prm); + } + + var newInnerPrm = Expression.Parameter(typeof(TElement), "source"); + + // note we need to also convert the result of inner lambda back to TElement return Expression.Lambda>( Expression.Call( SnapshotMethod, prm, - elementComparer.ConstructorExpression), + Expression.Lambda( + Expression.Convert( + elementComparer.ExtractSnapshotBody( + Expression.Convert(newInnerPrm, elementComparer.Type)), + typeof(TElement)), + newInnerPrm)), prm); } - private static bool Compare(object? a, object? b, ValueComparer elementComparer) + private static bool Compare(object? a, object? b, Func elementCompare) { if (ReferenceEquals(a, b)) { @@ -143,7 +207,7 @@ private static bool Compare(object? a, object? b, ValueComparer elementComparer) return false; } - if (!elementComparer.Equals(el1, el2)) + if (!elementCompare(el1, el2)) { return false; } @@ -155,29 +219,29 @@ private static bool Compare(object? a, object? b, ValueComparer elementComparer) throw new InvalidOperationException( CoreStrings.BadListType( (a is IList ? b : a).GetType().ShortDisplayName(), - typeof(IList<>).MakeGenericType(elementComparer.Type).ShortDisplayName())); + typeof(IList<>).MakeGenericType(typeof(TElement)).ShortDisplayName())); } - private static int GetHashCode(IEnumerable source, ValueComparer elementComparer) + private static int GetHashCode(IEnumerable source, Func elementGetHashCode) { var hash = new HashCode(); foreach (var el in source) { - hash.Add(el == null ? 0 : elementComparer.GetHashCode(el)); + hash.Add(el == null ? 0 : elementGetHashCode((TElement?)el)); } return hash.ToHashCode(); } - private static IList Snapshot(object source, ValueComparer elementComparer) + private static IList Snapshot(object source, Func elementSnapshot) { if (source is not IList sourceList) { throw new InvalidOperationException( CoreStrings.BadListType( source.GetType().ShortDisplayName(), - typeof(IList<>).MakeGenericType(elementComparer.Type).ShortDisplayName())); + typeof(IList<>).MakeGenericType(typeof(TElement)).ShortDisplayName())); } if (IsArray) @@ -186,7 +250,7 @@ private static int GetHashCode(IEnumerable source, ValueComparer elementComparer for (var i = 0; i < sourceList.Count; i++) { var instance = sourceList[i]; - snapshot[i] = instance == null ? null : (TElement?)elementComparer.Snapshot(instance); + snapshot[i] = instance == null ? null : elementSnapshot(instance); } return snapshot; @@ -196,7 +260,7 @@ private static int GetHashCode(IEnumerable source, ValueComparer elementComparer var snapshot = IsReadOnly ? new List() : (IList)Activator.CreateInstance()!; foreach (var e in sourceList) { - snapshot.Add(e == null ? null : (TElement?)elementComparer.Snapshot(e)); + snapshot.Add(e == null ? null : elementSnapshot(e)); } return IsReadOnly diff --git a/src/EFCore/ChangeTracking/ListOfValueTypesComparer.cs b/src/EFCore/ChangeTracking/ListOfValueTypesComparer.cs index 19a3a8d4a2f..57e8e0f6793 100644 --- a/src/EFCore/ChangeTracking/ListOfValueTypesComparer.cs +++ b/src/EFCore/ChangeTracking/ListOfValueTypesComparer.cs @@ -31,14 +31,14 @@ public sealed class ListOfValueTypesComparer : ValueCom private static readonly MethodInfo CompareMethod = typeof(ListOfValueTypesComparer).GetMethod( nameof(Compare), BindingFlags.Static | BindingFlags.NonPublic, - [typeof(IEnumerable), typeof(IEnumerable), typeof(ValueComparer)])!; + [typeof(IEnumerable), typeof(IEnumerable), typeof(Func)])!; private static readonly MethodInfo GetHashCodeMethod = typeof(ListOfValueTypesComparer).GetMethod( nameof(GetHashCode), BindingFlags.Static | BindingFlags.NonPublic, - [typeof(IEnumerable), typeof(ValueComparer)])!; + [typeof(IEnumerable), typeof(Func)])!; private static readonly MethodInfo SnapshotMethod = typeof(ListOfValueTypesComparer).GetMethod( - nameof(Snapshot), BindingFlags.Static | BindingFlags.NonPublic, [typeof(IEnumerable), typeof(ValueComparer)])!; + nameof(Snapshot), BindingFlags.Static | BindingFlags.NonPublic, [typeof(IEnumerable), typeof(Func)])!; /// /// Creates a new instance of the list comparer. @@ -64,15 +64,13 @@ ValueComparer IInfrastructure.Instance var prm1 = Expression.Parameter(typeof(IEnumerable), "a"); var prm2 = Expression.Parameter(typeof(IEnumerable), "b"); - //(a, b) => Compare(a, b, (ValueComparer)elementComparer) + //(a, b) => Compare(a, b, elementComparer.Equals) return Expression.Lambda?, IEnumerable?, bool>>( Expression.Call( CompareMethod, prm1, prm2, - Expression.Convert( - elementComparer.ConstructorExpression, - typeof(ValueComparer))), + elementComparer.EqualsExpression), prm1, prm2); } @@ -81,14 +79,12 @@ private static Expression, int>> GetHashCodeLambda(Va { var prm = Expression.Parameter(typeof(IEnumerable), "o"); - //o => GetHashCode(o, (ValueComparer)elementComparer) + //o => GetHashCode(o, elementComparer.GetHashCode) return Expression.Lambda, int>>( Expression.Call( GetHashCodeMethod, prm, - Expression.Convert( - elementComparer.ConstructorExpression, - typeof(ValueComparer))), + elementComparer.HashCodeExpression), prm); } @@ -96,18 +92,16 @@ private static Expression, IEnumerable>> Sn { var prm = Expression.Parameter(typeof(IEnumerable), "source"); - //source => Snapshot(source, (ValueComparer)elementComparer) + //source => Snapshot(source, elementComparer.SnapShot) return Expression.Lambda, IEnumerable>>( Expression.Call( SnapshotMethod, prm, - Expression.Convert( - elementComparer.ConstructorExpression, - typeof(ValueComparer))), + elementComparer.SnapshotExpression), prm); } - private static bool Compare(IEnumerable? a, IEnumerable? b, ValueComparer elementComparer) + private static bool Compare(IEnumerable? a, IEnumerable? b, Func elementCompare) { if (ReferenceEquals(a, b)) { @@ -134,7 +128,7 @@ private static bool Compare(IEnumerable? a, IEnumerable? b, for (var i = 0; i < aList.Count; i++) { var (el1, el2) = (aList[i], bList[i]); - if (!elementComparer.Equals(el1, el2)) + if (!elementCompare(el1, el2)) { return false; } @@ -146,29 +140,29 @@ private static bool Compare(IEnumerable? a, IEnumerable? b, throw new InvalidOperationException( CoreStrings.BadListType( (a is IList ? b : a).GetType().ShortDisplayName(), - typeof(IList<>).MakeGenericType(elementComparer.Type).ShortDisplayName())); + typeof(IList<>).MakeGenericType(typeof(TElement)).ShortDisplayName())); } - private static int GetHashCode(IEnumerable source, ValueComparer elementComparer) + private static int GetHashCode(IEnumerable source, Func elementGetHashCode) { var hash = new HashCode(); foreach (var el in source) { - hash.Add(elementComparer.GetHashCode(el)); + hash.Add(elementGetHashCode(el)); } return hash.ToHashCode(); } - private static IList Snapshot(IEnumerable source, ValueComparer elementComparer) + private static IList Snapshot(IEnumerable source, Func elementSnapshot) { if (source is not IList sourceList) { throw new InvalidOperationException( CoreStrings.BadListType( source.GetType().ShortDisplayName(), - typeof(IList<>).MakeGenericType(elementComparer.Type.MakeNullable()).ShortDisplayName())); + typeof(IList<>).MakeGenericType(typeof(TElement).MakeNullable()).ShortDisplayName())); } if (IsArray) @@ -177,7 +171,7 @@ private static IList Snapshot(IEnumerable source, ValueCompa for (var i = 0; i < sourceList.Count; i++) { var instance = sourceList[i]; - snapshot[i] = elementComparer.Snapshot(instance); + snapshot[i] = elementSnapshot(instance); } return snapshot; @@ -187,7 +181,7 @@ private static IList Snapshot(IEnumerable source, ValueCompa var snapshot = IsReadOnly ? new List() : (IList)Activator.CreateInstance()!; foreach (var e in sourceList) { - snapshot.Add(elementComparer.Snapshot(e)); + snapshot.Add(elementSnapshot(e)); } return IsReadOnly