Skip to content

Commit

Permalink
Fix to #35239 - EF9: SaveChanges() is significantly slower in .NET9 v…
Browse files Browse the repository at this point in the history
…s. .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.

In order to do that safely we need to make sure that type of the element comparer and the type on the list comparer are compatible (so that when func from element comparer is passed to the list comparer Equals/Hashcode/Snapshot method the resulting expression is valid. We do that by introducing a comparer that converts from one type to another, so that they are always aligned.

Fixes #35239
  • Loading branch information
maumar committed Dec 21, 2024
1 parent f163289 commit 14e0dc6
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ namespace Microsoft.EntityFrameworkCore.Cosmos.ChangeTracking.Internal;
public sealed class StringDictionaryComparer<TDictionary, TElement> : ValueComparer<object>, IInfrastructure<ValueComparer>
{
private static readonly MethodInfo CompareMethod = typeof(StringDictionaryComparer<TDictionary, TElement>).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<TElement, TElement, bool>)])!;

private static readonly MethodInfo GetHashCodeMethod = typeof(StringDictionaryComparer<TDictionary, TElement>).GetMethod(
nameof(GetHashCode), BindingFlags.Static | BindingFlags.NonPublic, [typeof(IEnumerable), typeof(ValueComparer)])!;
nameof(GetHashCode), BindingFlags.Static | BindingFlags.NonPublic, [typeof(IEnumerable), typeof(Func<TElement, int>)])!;

private static readonly MethodInfo SnapshotMethod = typeof(StringDictionaryComparer<TDictionary, TElement>).GetMethod(
nameof(Snapshot), BindingFlags.Static | BindingFlags.NonPublic, [typeof(object), typeof(ValueComparer)])!;
nameof(Snapshot), BindingFlags.Static | BindingFlags.NonPublic, [typeof(object), typeof(Func<TElement, TElement>)])!;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down Expand Up @@ -57,9 +57,7 @@ ValueComparer IInfrastructure<ValueComparer>.Instance
CompareMethod,
prm1,
prm2,
#pragma warning disable EF9100
elementComparer.ConstructorExpression),
#pragma warning restore EF9100
elementComparer.EqualsExpression),
prm1,
prm2);
}
Expand All @@ -74,9 +72,7 @@ private static Expression<Func<object, int>> GetHashCodeLambda(ValueComparer ele
Expression.Convert(
prm,
typeof(IEnumerable)),
#pragma warning disable EF9100
elementComparer.ConstructorExpression),
#pragma warning restore EF9100
elementComparer.HashCodeExpression),
prm);
}

Expand All @@ -88,13 +84,11 @@ private static Expression<Func<object, object>> SnapshotLambda(ValueComparer ele
Expression.Call(
SnapshotMethod,
prm,
#pragma warning disable EF9100
elementComparer.ConstructorExpression),
#pragma warning restore EF9100
elementComparer.SnapshotExpression),
prm);
}

private static bool Compare(object? a, object? b, ValueComparer elementComparer)
private static bool Compare(object? a, object? b, Func<TElement?, TElement?, bool> elementCompare)
{
if (ReferenceEquals(a, b))
{
Expand All @@ -121,7 +115,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;
}
Expand All @@ -133,44 +127,44 @@ private static bool Compare(object? a, object? b, ValueComparer elementComparer)
throw new InvalidOperationException(
CosmosStrings.BadDictionaryType(
(a is IDictionary<string, TElement?> ? 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<TElement?, int> elementGetHashCode)
{
if (source is not IReadOnlyDictionary<string, TElement?> 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();

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<string, TElement?> Snapshot(object source, ValueComparer elementComparer)
private static IReadOnlyDictionary<string, TElement?> Snapshot(object source, Func<TElement?, TElement?> elementSnapshot)
{
if (source is not IReadOnlyDictionary<string, TElement?> 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<string, TElement?>();
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;
Expand Down
85 changes: 85 additions & 0 deletions src/EFCore/ChangeTracking/Internal/ConvertingValueComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.EntityFrameworkCore.ChangeTracking.Internal;

using static Expression;

/// <summary>
/// A composable value comparer that accepts a value comparer, and exposes it as a value comparer for a base type.
/// Used when a collection comparer over e.g. object[] is needed over a specific element type (e.g. int)
/// </summary>
/// <remarks>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </remarks>
public class ConvertingValueComparer<TTo, TFrom> : ValueComparer<TTo>, IInfrastructure<ValueComparer>
{
private readonly ValueComparer<TFrom> _valueComparer;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public ConvertingValueComparer(ValueComparer<TFrom> valueComparer)
: base(
CreateEquals(valueComparer),
CreateHashCode(valueComparer),
CreateSnapshot(valueComparer))
=> _valueComparer = valueComparer;

private static Expression<Func<TTo?, TTo?, bool>> CreateEquals(ValueComparer<TFrom> valueComparer)
{
var p1 = Parameter(typeof(TTo), "v1");
var p2 = Parameter(typeof(TTo), "v2");

var body = typeof(TTo).IsAssignableFrom(typeof(TFrom))
? valueComparer.EqualsExpression.Body
: valueComparer.ExtractEqualsBody(
Convert(p1, typeof(TFrom)),
Convert(p2, typeof(TFrom)));

return Lambda<Func<TTo?, TTo?, bool>>(
body,
p1,
p2);
}

private static Expression<Func<TTo, int>> CreateHashCode(ValueComparer<TFrom> valueComparer)
{
var p = Parameter(typeof(TTo), "v");

var body = typeof(TTo).IsAssignableFrom(typeof(TFrom))
? valueComparer.HashCodeExpression.Body
: valueComparer.ExtractHashCodeBody(
Convert(p, typeof(TFrom)));

return Lambda<Func<TTo, int>>(
body,
p);
}

private static Expression<Func<TTo, TTo>> CreateSnapshot(ValueComparer<TFrom> valueComparer)
{
var p = Parameter(typeof(TTo), "v");

// types must match exactly as we have both covariance and contravariance case here
var body = typeof(TTo) == typeof(TFrom)
? valueComparer.SnapshotExpression.Body
: Convert(
valueComparer.ExtractSnapshotBody(
Convert(p, typeof(TFrom))),
typeof(TTo));

return Lambda<Func<TTo, TTo>>(
body,
p);
}

ValueComparer IInfrastructure<ValueComparer>.Instance
=> _valueComparer;
}
26 changes: 26 additions & 0 deletions src/EFCore/ChangeTracking/Internal/ValueComparerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,30 @@ public static class ValueComparerExtensions
: (ValueComparer)Activator.CreateInstance(
typeof(NullableValueComparer<>).MakeGenericType(valueComparer.Type),
valueComparer)!;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static ValueComparer? ComposeConversion(this ValueComparer? valueComparer, Type targetClrType)
{
if (valueComparer is null || valueComparer.Type == targetClrType)
{
return valueComparer;
}

if (targetClrType.IsNullableValueType() && !valueComparer.Type.IsNullableValueType()
&& targetClrType.UnwrapNullableType() == valueComparer.Type)
{
return (ValueComparer)Activator.CreateInstance(
typeof(NullableValueComparer<>).MakeGenericType(valueComparer.Type),
valueComparer)!;
}

return (ValueComparer)Activator.CreateInstance(
typeof(ConvertingValueComparer<,>).MakeGenericType(targetClrType, valueComparer.Type),
valueComparer)!;
}
}
42 changes: 18 additions & 24 deletions src/EFCore/ChangeTracking/ListOfNullableValueTypesComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ public sealed class ListOfNullableValueTypesComparer<TConcreteList, TElement> :

private static readonly MethodInfo CompareMethod = typeof(ListOfNullableValueTypesComparer<TConcreteList, TElement>).GetMethod(
nameof(Compare), BindingFlags.Static | BindingFlags.NonPublic,
[typeof(IEnumerable<TElement?>), typeof(IEnumerable<TElement?>), typeof(ValueComparer<TElement?>)])!;
[typeof(IEnumerable<TElement?>), typeof(IEnumerable<TElement?>), typeof(Func<TElement?, TElement?, bool>)])!;

private static readonly MethodInfo GetHashCodeMethod = typeof(ListOfNullableValueTypesComparer<TConcreteList, TElement>).GetMethod(
nameof(GetHashCode), BindingFlags.Static | BindingFlags.NonPublic,
[typeof(IEnumerable<TElement?>), typeof(ValueComparer<TElement?>)])!;
[typeof(IEnumerable<TElement?>), typeof(Func<TElement?, int>)])!;

private static readonly MethodInfo SnapshotMethod = typeof(ListOfNullableValueTypesComparer<TConcreteList, TElement>).GetMethod(
nameof(Snapshot), BindingFlags.Static | BindingFlags.NonPublic,
[typeof(IEnumerable<TElement?>), typeof(ValueComparer<TElement?>)])!;
[typeof(IEnumerable<TElement?>), typeof(Func<TElement?, TElement?>)])!;

/// <summary>
/// Creates a new instance of the list comparer.
Expand All @@ -67,15 +67,13 @@ ValueComparer IInfrastructure<ValueComparer>.Instance
var prm1 = Expression.Parameter(typeof(IEnumerable<TElement?>), "a");
var prm2 = Expression.Parameter(typeof(IEnumerable<TElement?>), "b");

//(a, b) => Compare(a, b, (ValueComparer<TElement?>)elementComparer)
//(a, b) => Compare(a, b, elementComparer.Equals)
return Expression.Lambda<Func<IEnumerable<TElement?>?, IEnumerable<TElement?>?, bool>>(
Expression.Call(
CompareMethod,
prm1,
prm2,
Expression.Convert(
elementComparer.ConstructorExpression,
typeof(ValueComparer<TElement?>))),
elementComparer.EqualsExpression),
prm1,
prm2);
}
Expand All @@ -84,33 +82,29 @@ ValueComparer IInfrastructure<ValueComparer>.Instance
{
var prm = Expression.Parameter(typeof(IEnumerable<TElement?>), "o");

//o => GetHashCode(o, (ValueComparer<TElement?>)elementComparer)
//o => GetHashCode(o, elementComparer.GetHashCode)
return Expression.Lambda<Func<IEnumerable<TElement?>, int>>(
Expression.Call(
GetHashCodeMethod,
prm,
Expression.Convert(
elementComparer.ConstructorExpression,
typeof(ValueComparer<TElement?>))),
elementComparer.HashCodeExpression),
prm);
}

private static Expression<Func<IEnumerable<TElement?>, IEnumerable<TElement?>>> SnapshotLambda(ValueComparer elementComparer)
{
var prm = Expression.Parameter(typeof(IEnumerable<TElement?>), "source");

//source => Snapshot(source, (ValueComparer<TElement?>)elementComparer)
//source => Snapshot(source, elementComparer.Snapshot)
return Expression.Lambda<Func<IEnumerable<TElement?>, IEnumerable<TElement?>>>(
Expression.Call(
SnapshotMethod,
prm,
Expression.Convert(
elementComparer.ConstructorExpression,
typeof(ValueComparer<TElement?>))),
elementComparer.SnapshotExpression),
prm);
}

private static bool Compare(IEnumerable<TElement?>? a, IEnumerable<TElement?>? b, ValueComparer<TElement?> elementComparer)
private static bool Compare(IEnumerable<TElement?>? a, IEnumerable<TElement?>? b, Func<TElement?, TElement?, bool> elementCompare)
{
if (ReferenceEquals(a, b))
{
Expand Down Expand Up @@ -152,7 +146,7 @@ private static bool Compare(IEnumerable<TElement?>? a, IEnumerable<TElement?>? b
return false;
}

if (!elementComparer.Equals(el1, el2))
if (!elementCompare(el1, el2))
{
return false;
}
Expand All @@ -164,29 +158,29 @@ private static bool Compare(IEnumerable<TElement?>? a, IEnumerable<TElement?>? b
throw new InvalidOperationException(
CoreStrings.BadListType(
(a is IList<TElement?> ? b : a).GetType().ShortDisplayName(),
typeof(IList<>).MakeGenericType(elementComparer.Type.MakeNullable()).ShortDisplayName()));
typeof(IList<>).MakeGenericType(typeof(TElement).MakeNullable()).ShortDisplayName()));
}

private static int GetHashCode(IEnumerable<TElement?> source, ValueComparer<TElement?> elementComparer)
private static int GetHashCode(IEnumerable<TElement?> source, Func<TElement?, int> 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<TElement?> Snapshot(IEnumerable<TElement?> source, ValueComparer<TElement?> elementComparer)
private static IList<TElement?> Snapshot(IEnumerable<TElement?> source, Func<TElement?, TElement?> elementSnapshot)
{
if (source is not IList<TElement?> 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)
Expand All @@ -195,7 +189,7 @@ private static int GetHashCode(IEnumerable<TElement?> source, ValueComparer<TEle
for (var i = 0; i < sourceList.Count; i++)
{
var instance = sourceList[i];
snapshot[i] = instance == null ? null : elementComparer.Snapshot(instance);
snapshot[i] = instance == null ? null : elementSnapshot(instance);
}

return snapshot;
Expand All @@ -205,7 +199,7 @@ private static int GetHashCode(IEnumerable<TElement?> source, ValueComparer<TEle
var snapshot = IsReadOnly ? new List<TElement?>() : (IList<TElement?>)Activator.CreateInstance<TConcreteList>()!;
foreach (var e in sourceList)
{
snapshot.Add(e == null ? null : elementComparer.Snapshot(e));
snapshot.Add(e == null ? null : elementSnapshot(e));
}

return IsReadOnly
Expand Down
Loading

0 comments on commit 14e0dc6

Please sign in to comment.