Repository navigation
Allow runtime constants in precompiled queries - #38430
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. |
|
I didn't create an issue for this, but ran into this while working on the shaper for cosmos and comparing with the relational code. |
| var existing = _preexistingRuntimeConstants.Concat(_foundRuntimeConstants) | ||
| .FirstOrDefault(x => runtimeConstant.Value.Equals(x.Value) || | ||
| ExpressionEqualityComparer.Instance.Equals( | ||
| x.InitializeExpression, | ||
| runtimeConstant.InitializeExpression)); |
There was a problem hiding this comment.
This feels a bit overkill to me, also because with how this class works right now we'd have to clear and rebuild the lookup structure every Process call. LMK if I should rethink this to reduce complexity
There was a problem hiding this comment.
Up to you if you can think of a clean way of doing it.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 11 out of 11 changed files in this pull request and generated 6 comments.
Comments suppressed due to low confidence (2)
src/EFCore/EFCore.baseline.json:1
- The API baseline entries for
RuntimeConstantProcessordon’t match the implementation in this PR: the baseline lists aProcess(Expression, IEnumerable<RuntimeConstantExpression>?)overload and aRuntimeConstantsproperty, but the code introducesProcess(Expression)andLastProcessFoundRuntimeConstants. This will likely fail API verification. Either update the baseline to match the actual public API, or adjust the implementation to match the baseline (including the method signature and property name).
src/EFCore/EFCore.baseline.json:1 - The API baseline entries for
RuntimeConstantProcessordon’t match the implementation in this PR: the baseline lists aProcess(Expression, IEnumerable<RuntimeConstantExpression>?)overload and aRuntimeConstantsproperty, but the code introducesProcess(Expression)andLastProcessFoundRuntimeConstants. This will likely fail API verification. Either update the baseline to match the actual public API, or adjust the implementation to match the baseline (including the method signature and property name).
|
@JoasE You need to re-run the API baseline tests |
AndriySvyryd
left a comment
There was a problem hiding this comment.
Thanks for your contribution!
API review baseline changes for
|
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.