Skip to content

Allow runtime constants in precompiled queries#38430

Open
JoasE wants to merge 10 commits into
dotnet:mainfrom
JoasE:feature/small-improvement-json-deserialize
Open

Allow runtime constants in precompiled queries#38430
JoasE wants to merge 10 commits into
dotnet:mainfrom
JoasE:feature/small-improvement-json-deserialize

Conversation

@JoasE

@JoasE JoasE commented Jun 14, 2026

Copy link
Copy Markdown
Contributor

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.

Improves performance by moving GetBytes call to query compile time instead of shaper runtime.
Quick benchmark:
https://gist.github.com/JoasE/47ed25296fe8651825e358c57454b44c
@JoasE JoasE changed the title Small optimization json shaper Allow runtime constants in precompiled queries Jun 15, 2026
@JoasE JoasE marked this pull request as ready for review June 15, 2026 13:25
@JoasE JoasE requested a review from a team as a code owner June 15, 2026 13:25
Copilot AI review requested due to automatic review settings June 15, 2026 13:25

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 RuntimeConstantExpression and RuntimeConstantProcessor to 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 thread src/EFCore.Design/Query/Internal/PrecompiledQueryCodeGenerator.cs Outdated
Comment thread src/EFCore/Query/RuntimeConstantProcessor.cs Outdated
Comment thread src/EFCore/Query/RuntimeConstantExpression.cs Outdated
@JoasE JoasE marked this pull request as draft June 15, 2026 14:39
@AndriySvyryd AndriySvyryd self-assigned this Jun 15, 2026
@JoasE JoasE marked this pull request as ready for review June 16, 2026 11:48
Copilot AI review requested due to automatic review settings June 16, 2026 11:49

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated 9 comments.

Comment thread src/EFCore/Query/RuntimeConstantExpression.cs
Comment thread src/EFCore/Query/RuntimeConstantProcessor.cs
Comment thread src/EFCore.Design/Query/Internal/PrecompiledQueryCodeGenerator.cs
Comment thread src/EFCore/Query/RuntimeConstantExpression.cs
Comment thread src/EFCore/Query/RuntimeConstantProcessor.cs
Comment thread src/EFCore/Query/RuntimeConstantExpression.cs
Comment thread src/EFCore.Design/Query/Internal/PrecompiledQueryCodeGenerator.cs Outdated
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++)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking this is ok for code generation, since it's not a hot path?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants