-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix to #35239 - EF9: SaveChanges() is significantly slower in .NET9 v…
…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. 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 refactored into ConvertingValueComparer
- Loading branch information
Showing
7 changed files
with
178 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/EFCore/ChangeTracking/Internal/ConvertingValueComparer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.