Skip to content

Allow runtime constants in precompiled queries - #38430

Merged
AndriySvyryd merged 21 commits into
dotnet:mainfrom
JoasE:feature/small-improvement-json-deserialize
Jun 17, 2026
Merged

AndriySvyryd merged 21 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
Contributor

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.

@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
Contributor

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.

@JoasE

JoasE commented Jun 16, 2026

Copy link
Copy Markdown
Contributor Author

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.
If my math is correct this can become pretty significant for larger entity types where N = property count for N * ((N + 1) / 2) array allocations, which would be 550 array allocations for 10 rows of a 10 property structural type, or 1200 array allocations for 10 rows of a 15 property structural type and, maybe less noticeable, UTF8 computations

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 2 comments.

Copilot AI review requested due to automatic review settings June 17, 2026 08:22

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 5 comments.

Comment on lines +52 to +56
var existing = _preexistingRuntimeConstants.Concat(_foundRuntimeConstants)
.FirstOrDefault(x => runtimeConstant.Value.Equals(x.Value) ||
ExpressionEqualityComparer.Instance.Equals(
x.InitializeExpression,
runtimeConstant.InitializeExpression));

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.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Up to you if you can think of a clean way of doing it.

Copilot AI review requested due to automatic review settings June 17, 2026 15:18

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 5 comments.

@JoasE
JoasE requested a review from AndriySvyryd June 17, 2026 16:06
Copilot AI review requested due to automatic review settings June 17, 2026 19:40

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 6 comments.

Comments suppressed due to low confidence (2)

src/EFCore/EFCore.baseline.json:1

  • The API baseline entries for RuntimeConstantProcessor don’t match the implementation in this PR: the baseline lists a Process(Expression, IEnumerable<RuntimeConstantExpression>?) overload and a RuntimeConstants property, but the code introduces Process(Expression) and LastProcessFoundRuntimeConstants. 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 RuntimeConstantProcessor don’t match the implementation in this PR: the baseline lists a Process(Expression, IEnumerable<RuntimeConstantExpression>?) overload and a RuntimeConstants property, but the code introduces Process(Expression) and LastProcessFoundRuntimeConstants. 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
JoasE requested a review from AndriySvyryd June 17, 2026 19:56
@AndriySvyryd

Copy link
Copy Markdown
Member

@JoasE You need to re-run the API baseline tests

Copilot AI review requested due to automatic review settings June 17, 2026 20:09

@AndriySvyryd AndriySvyryd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for your contribution!

@AndriySvyryd
AndriySvyryd enabled auto-merge (squash) June 17, 2026 20:19

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 2 comments.

@AndriySvyryd
AndriySvyryd merged commit 2b318b1 into dotnet:main Jun 17, 2026
13 checks passed
@github-actions github-actions Bot added the api-review This PR or issue is introducing public API changes that need to be reviewed label Jun 17, 2026
@github-actions

github-actions Bot commented Jun 17, 2026 •

Copy link
Copy Markdown
Contributor

API review baseline changes for src/EFCore/EFCore.baseline.json

Show diff

The diff below was generated by ApiChief between the base and the PR.

+ [Experimental] class Microsoft.EntityFrameworkCore.Query.RuntimeConstantExpression : System.Linq.Expressions.Expression,
+     Microsoft.EntityFrameworkCore.Query.IPrintableExpression
+ [Experimental] override bool CanReduce { get; }
+ [Experimental] virtual Expression InitializeExpression { get; }
+ [Experimental] virtual string Name { get; }
+ [Experimental] override ExpressionType NodeType { get; }
+ [Experimental] override Type Type { get; }
+ [Experimental] virtual object Value { get; }
+ [Experimental] void Print(ExpressionPrinter expressionPrinter);
+ [Experimental] override Expression Reduce();
+ [Experimental] RuntimeConstantExpression(string name, Expression initializeExpression);
+ [Experimental] virtual RuntimeConstantExpression Update(Expression initializeExpression);
+ [Experimental] override Expression VisitChildren(ExpressionVisitor visitor);
+ [Experimental] class Microsoft.EntityFrameworkCore.Query.RuntimeConstantProcessor : System.Linq.Expressions.ExpressionVisitor
+ [Experimental] virtual IReadOnlyList<RuntimeConstantExpression> LastProcessFoundRuntimeConstants { get; }
+ [Experimental] virtual Expression Process(Expression expression);
+ [Experimental] RuntimeConstantProcessor();
+ [Experimental] override Expression VisitExtension(Expression node);

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