Skip to content

Commit f686ae5

Browse files
authored
Fix performance degradation in converters because the instance was not reused. (#36897)
1 parent de693b4 commit f686ae5

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/EFCore/Storage/Json/JsonConvertedValueReaderWriter.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ public class JsonConvertedValueReaderWriter<TModel, TProvider> :
1616
JsonValueReaderWriter<TModel>,
1717
IJsonConvertedValueReaderWriter
1818
{
19+
private static readonly bool UseOldBehavior36856 =
20+
AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue36856", out var enabled) && enabled;
21+
1922
private readonly JsonValueReaderWriter<TProvider> _providerReaderWriter;
2023
private readonly ValueConverter _converter;
2124

@@ -52,8 +55,17 @@ ValueConverter IJsonConvertedValueReaderWriter.Converter
5255

5356
/// <inheritdoc />
5457
public override Expression ConstructorExpression
55-
=> Expression.New(
56-
_constructorInfo,
57-
((ICompositeJsonValueReaderWriter)this).InnerReaderWriter.ConstructorExpression,
58-
((IJsonConvertedValueReaderWriter)this).Converter.ConstructorExpression);
58+
=> UseOldBehavior36856
59+
? Expression.New(
60+
_constructorInfo,
61+
((ICompositeJsonValueReaderWriter)this).InnerReaderWriter.ConstructorExpression,
62+
((IJsonConvertedValueReaderWriter)this).Converter.ConstructorExpression)
63+
: Expression.New(
64+
_constructorInfo,
65+
((ICompositeJsonValueReaderWriter)this).InnerReaderWriter.ConstructorExpression,
66+
// We shouldn't quote converters, because it will create a new instance every time and
67+
// it will have to compile the expression again and
68+
// it will have a negative performance impact. See #36856 for more info.
69+
// This means this is currently unsupported scenario for precompilation.
70+
Expression.Constant(((IJsonConvertedValueReaderWriter)this).Converter));
5971
}

0 commit comments

Comments
 (0)