Allow runtime constants in precompiled queries#38430
Open
JoasE wants to merge 10 commits into
Open
Conversation
Improves performance by moving GetBytes call to query compile time instead of shaper runtime. Quick benchmark: https://gist.github.com/JoasE/47ed25296fe8651825e358c57454b44c
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds support for “runtime constants” during precompiled query code generation so that certain computed constants (e.g., UTF8 byte arrays for JSON property names) are emitted as static fields initialized at runtime instead of being embedded directly into generated code.
Changes:
- Introduce
RuntimeConstantExpressionandRuntimeConstantProcessorto capture and rewrite runtime-constant nodes. - Update relational shaper processing to wrap
Encoding.UTF8.GetBytes(...)for JSON property-name comparisons in runtime constants. - Extend precompiled query code generation and translator name-uniquification, and add coverage in design/relational/SQL Server tests.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| test/EFCore.SqlServer.FunctionalTests/Query/PrecompiledQuerySqlServerTest.cs | Adds provider-specific SQL assertion for the new runtime-constant JSON shaper scenario. |
| test/EFCore.Relational.Specification.Tests/Query/PrecompiledQueryRelationalTestBase.cs | Adds a relational test validating runtime constant emission for UTF8 byte arrays in generated code. |
| test/EFCore.Design.Tests/Query/LinqToCSharpSyntaxTranslatorTest.cs | Adds a regression test for variable renaming when a constant replacement would conflict. |
| src/EFCore/Query/RuntimeConstantProcessor.cs | New processor that collects runtime constants and rewrites them to constants during visiting. |
| src/EFCore/Query/RuntimeConstantExpression.cs | New expression node representing a runtime-initialized constant. |
| src/EFCore.Relational/Query/RelationalShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.cs | Wraps JSON property-name UTF8 byte generation in runtime constants. |
| src/EFCore.Design/Query/Internal/PrecompiledQueryCodeGenerator.cs | Collects runtime constants, emits them as static fields, and passes constant replacements into translation. |
| src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs | Avoids variable-name collisions with constant replacement identifiers. |
Comment on lines
+2755
to
2760
| if (parameterNames.Contains(name) || _liftedState.VariableNames.Contains(name) || _constantReplacements?.Values.Contains(name) == true) | ||
| { | ||
| var baseName = name; | ||
| for (var j = isUnnamed ? _unnamedParameterCounter++ : 0; | ||
| parameterNames.Contains(name) || _liftedState.VariableNames.Contains(name); | ||
| parameterNames.Contains(name) || _liftedState.VariableNames.Contains(name) || _constantReplacements?.Values.Contains(name) == true; | ||
| j++) |
Contributor
Author
There was a problem hiding this comment.
I was thinking this is ok for code generation, since it's not a hot path?
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds support for storing values which don't change across executions of the query in precompiled queries as a private static readonly field. Also allowing non-precompiled queries to use a ConstantExpression directly for these values.
Improves performance by moving GetBytes call to query compile time instead of shaper runtime.