From bb404fbcc90b26e986f1df3a04d8a757b8695dd9 Mon Sep 17 00:00:00 2001 From: maumar Date: Mon, 25 Nov 2024 22:32:24 -0800 Subject: [PATCH] Fix to #35208 - Query/Perf: don't compile liftable constant resolvers in interpretation mode when the resolver itself contains a lambda In EF9 we changed the way we generate shapers in preparation for AOT scenarios. We no longer can embed arbitrary objects into the shaper, instead we need to provide a way to construct that object in code (using LiftableConstant mechanism), or simulate the functionality it used to provide. At the end of our processing, we find all liftable constants and for the non-AOT case we compile their resolver lambdas and invoke the result with liftable context object to produce the resulting constant object we initially wanted. (in AOT case we generate code from the resolver lambda). Problem is that we are compiling the resolver lambda in the interpretation mode - if the final product is itself a delegate, that delegate will itself be in the interpreter mode and therefore less efficient. Fix is to use regular compilation rather than interpretation. Fixes #35208 This is part of a fix for a larger perf issue: #35053 --- .../Query/RelationalLiftableConstantProcessor.cs | 3 ++- src/EFCore/Query/LiftableConstantProcessor.cs | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/EFCore.Relational/Query/RelationalLiftableConstantProcessor.cs b/src/EFCore.Relational/Query/RelationalLiftableConstantProcessor.cs index 2d0b9fa8c0c..c02be144073 100644 --- a/src/EFCore.Relational/Query/RelationalLiftableConstantProcessor.cs +++ b/src/EFCore.Relational/Query/RelationalLiftableConstantProcessor.cs @@ -36,7 +36,8 @@ protected override ConstantExpression InlineConstant(LiftableConstantExpression if (liftableConstant.ResolverExpression is Expression> resolverExpression) { - var resolver = resolverExpression.Compile(preferInterpretation: true); + // TODO: deep dive into this - see issue #35210 + var resolver = resolverExpression.Compile(preferInterpretation: false); var value = resolver(_relationalMaterializerLiftableConstantContext); return Expression.Constant(value, liftableConstant.Type); } diff --git a/src/EFCore/Query/LiftableConstantProcessor.cs b/src/EFCore/Query/LiftableConstantProcessor.cs index 5e384638cd8..8c3ec528c55 100644 --- a/src/EFCore/Query/LiftableConstantProcessor.cs +++ b/src/EFCore/Query/LiftableConstantProcessor.cs @@ -198,7 +198,8 @@ protected virtual ConstantExpression InlineConstant(LiftableConstantExpression l // Make sure there aren't any problematic un-lifted constants within the resolver expression. _unsupportedConstantChecker.Check(resolverExpression); - var resolver = resolverExpression.Compile(preferInterpretation: true); + // TODO: deep dive into this - see issue #35210 + var resolver = resolverExpression.Compile(preferInterpretation: false); var value = resolver(_materializerLiftableConstantContext); return Expression.Constant(value, liftableConstant.Type);