Skip to content

Commit a30cd07

Browse files
committed
Switch to interpreted delegates for one-off invocations
Closes dotnet#29814
1 parent f732875 commit a30cd07

File tree

5 files changed

+21
-18
lines changed

5 files changed

+21
-18
lines changed

src/EFCore.Cosmos/Query/Internal/CosmosSqlTranslatingExpressionVisitor.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,9 @@ private static SqlConstantExpression GetConstantOrNull(Expression expression)
995995
=> CanEvaluate(expression)
996996
? new SqlConstantExpression(
997997
Expression.Constant(
998-
Expression.Lambda<Func<object>>(Expression.Convert(expression, typeof(object))).Compile().Invoke(),
998+
Expression.Lambda<Func<object>>(Expression.Convert(expression, typeof(object)))
999+
.Compile(preferInterpretation: true)
1000+
.Invoke(),
9991001
expression.Type),
10001002
null)
10011003
: null;

src/EFCore.InMemory/Query/Internal/InMemoryExpressionTranslatingExpressionVisitor.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1505,7 +1505,9 @@ when CanEvaluate(memberInitExpression):
15051505

15061506
private static ConstantExpression GetValue(Expression expression)
15071507
=> Expression.Constant(
1508-
Expression.Lambda<Func<object>>(Expression.Convert(expression, typeof(object))).Compile().Invoke(),
1508+
Expression.Lambda<Func<object>>(Expression.Convert(expression, typeof(object)))
1509+
.Compile(preferInterpretation: true)
1510+
.Invoke(),
15091511
expression.Type);
15101512

15111513
private static bool CanEvaluate(Expression expression)
@@ -1518,7 +1520,7 @@ private static bool CanEvaluate(Expression expression)
15181520
return true;
15191521

15201522
case NewExpression newExpression:
1521-
return newExpression.Arguments.All(e => CanEvaluate(e));
1523+
return newExpression.Arguments.All(CanEvaluate);
15221524

15231525
case MemberInitExpression memberInitExpression:
15241526
return CanEvaluate(memberInitExpression.NewExpression)

src/EFCore.Relational/Metadata/Conventions/RelationalQueryFilterRewritingConvention.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ protected override Expression VisitMethodCall(MethodCallExpression methodCallExp
8484
else
8585
{
8686
var formattableString = Expression.Lambda<Func<FormattableString>>(
87-
Expression.Convert(methodCallExpression.Arguments[1], typeof(FormattableString))).Compile().Invoke();
87+
Expression.Convert(methodCallExpression.Arguments[1], typeof(FormattableString)))
88+
.Compile(preferInterpretation: true)
89+
.Invoke();
8890

8991
sql = formattableString.Format;
9092
argument = Expression.Constant(formattableString.GetArguments());

src/EFCore.Relational/Query/RelationalSqlTranslatingExpressionVisitor.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1540,7 +1540,9 @@ private static Expression GetConstantOrNotTranslated(Expression expression)
15401540
=> CanEvaluate(expression)
15411541
? new SqlConstantExpression(
15421542
Expression.Constant(
1543-
Expression.Lambda<Func<object>>(Expression.Convert(expression, typeof(object))).Compile().Invoke(),
1543+
Expression.Lambda<Func<object>>(Expression.Convert(expression, typeof(object)))
1544+
.Compile(preferInterpretation: true)
1545+
.Invoke(),
15441546
expression.Type),
15451547
null)
15461548
: QueryCompilationContext.NotTranslatedExpression;

src/EFCore/Query/Internal/ParameterExtractingExpressionVisitor.cs

+8-13
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,7 @@ protected override Expression VisitConditional(ConditionalExpression conditional
151151
{
152152
var newTestExpression = TryGetConstantValue(conditionalExpression.Test) ?? Visit(conditionalExpression.Test);
153153

154-
if (newTestExpression is ConstantExpression constantTestExpression
155-
&& constantTestExpression.Value is bool constantTestValue)
154+
if (newTestExpression is ConstantExpression { Value: bool constantTestValue })
156155
{
157156
return constantTestValue
158157
? Visit(conditionalExpression.IfTrue)
@@ -227,8 +226,7 @@ protected override Expression VisitBinary(BinaryExpression binaryExpression)
227226
}
228227

229228
private static bool ShortCircuitLogicalExpression(Expression expression, ExpressionType nodeType)
230-
=> expression is ConstantExpression constantExpression
231-
&& constantExpression.Value is bool constantValue
229+
=> expression is ConstantExpression { Value: bool constantValue }
232230
&& ((constantValue && nodeType == ExpressionType.OrElse)
233231
|| (!constantValue && nodeType == ExpressionType.AndAlso));
234232

@@ -265,7 +263,7 @@ private static Expression GenerateConstantExpression(object? value, Type returnT
265263

266264
return constantExpression.Type != returnType
267265
? Expression.Convert(constantExpression, returnType)
268-
: (Expression)constantExpression;
266+
: constantExpression;
269267
}
270268

271269
private Expression Evaluate(Expression expression, bool generateParameter)
@@ -365,8 +363,7 @@ public ContextParameterReplacingExpressionVisitor(Type contextType)
365363
private static Expression RemoveConvert(Expression expression)
366364
{
367365
if (expression is UnaryExpression unaryExpression
368-
&& (expression.NodeType == ExpressionType.Convert
369-
|| expression.NodeType == ExpressionType.ConvertChecked))
366+
&& expression.NodeType is ExpressionType.Convert or ExpressionType.ConvertChecked)
370367
{
371368
return RemoveConvert(unaryExpression.Operand);
372369
}
@@ -436,18 +433,16 @@ private static Expression RemoveConvert(Expression expression)
436433
parameterName = methodCallExpression.Method.Name;
437434
break;
438435

439-
case UnaryExpression unaryExpression
440-
when (unaryExpression.NodeType == ExpressionType.Convert
441-
|| unaryExpression.NodeType == ExpressionType.ConvertChecked)
442-
&& (unaryExpression.Type.UnwrapNullableType() == unaryExpression.Operand.Type):
436+
case UnaryExpression { NodeType: ExpressionType.Convert or ExpressionType.ConvertChecked } unaryExpression
437+
when (unaryExpression.Type.UnwrapNullableType() == unaryExpression.Operand.Type):
443438
return GetValue(unaryExpression.Operand, out parameterName);
444439
}
445440

446441
try
447442
{
448443
return Expression.Lambda<Func<object>>(
449444
Expression.Convert(expression, typeof(object)))
450-
.Compile()
445+
.Compile(preferInterpretation: true)
451446
.Invoke();
452447
}
453448
catch (Exception exception)
@@ -611,7 +606,7 @@ protected override Expression VisitMethodCall(MethodCallExpression methodCallExp
611606
protected override Expression VisitMember(MemberExpression memberExpression)
612607
{
613608
_containsClosure = memberExpression.Expression != null
614-
|| !(memberExpression.Member is FieldInfo fieldInfo && fieldInfo.IsInitOnly);
609+
|| !(memberExpression.Member is FieldInfo { IsInitOnly: true });
615610
return base.VisitMember(memberExpression);
616611
}
617612

0 commit comments

Comments
 (0)