Skip to content

Conversation

@roji
Copy link
Member

@roji roji commented Jan 5, 2026

Especially around NativeAOT-friendly patterns.

Especially around NativeAOT-friendly patterns.
@roji roji force-pushed the TranslationCleanup branch from 39cfa7a to f7dda44 Compare January 5, 2026 21:54
// https://learn.microsoft.com/dotnet/api/system.string.startswith#system-string-startswith(system-char)
// https://learn.microsoft.com/dotnet/api/system.string.endswith#system-string-endswith(system-string)
// https://learn.microsoft.com/dotnet/api/system.string.endswith#system-string-endswith(system-char)
if (method.Name is nameof(string.StartsWith) or nameof(string.EndsWith)
Copy link
Member Author

Choose a reason for hiding this comment

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

FYI this would be the new, NativeAOT-friendly pattern for matching methods/members in translation. We're currently using reflection to look up MethodInfos, which causes the (trimming) linker the preserve all these methods regardless of whether users actually use them or not (increasing size). In many cases we're even generating closed generic methods via MakeGenericMethod, which isn't allowed at all.

Instead, we pattern match based on the method name, declaring type, and precise parameter list. This allows for accurate pattern matching, is fully NativeAOT-compatible without needlessly preserving any method that isn't used by users, and IMHO even easier-to-read, with the full, idiomatic match in one place.

@roji roji marked this pull request as ready for review January 5, 2026 22:30
@roji roji requested a review from a team as a code owner January 5, 2026 22:30
Copilot AI review requested due to automatic review settings January 5, 2026 22:30
@roji roji enabled auto-merge (squash) January 5, 2026 22:30
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR modernizes three expression visitor classes to use NativeAOT-friendly patterns, primarily converting traditional constructors to primary constructors and refactoring method implementations for better code clarity.

  • Converts constructors to C# 12 primary constructor syntax across all three visitor classes
  • Removes static MethodInfo field caching in favor of runtime pattern matching on method names and signatures
  • Refactors control flow in VisitBinary, VisitUnary, and VisitMethodCall to be more consistent and maintainable

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/EFCore/Query/QueryableMethodTranslatingExpressionVisitor.cs Converts to primary constructor with inline field initialization; moves XML documentation to constructor parameters
src/EFCore.Sqlite.Core/Query/Internal/SqliteSqlTranslatingExpressionVisitor.cs Converts to primary constructor; removes static MethodInfo fields; refactors VisitUnary to handle translations before custom logic; refactors VisitBinary to use switch expressions; simplifies VisitMethodCall local function signature
src/EFCore.SqlServer/Query/Internal/SqlServerSqlTranslatingExpressionVisitor.cs Converts to primary constructor; removes six static MethodInfo fields and DateTimeDataTypes HashSet; refactors all three Visit methods to call base first then handle special cases; converts VisitMethodCall to use switch statement on method names; removes unused System.Diagnostics.CodeAnalysis using

@roji roji merged commit e84e7fe into dotnet:main Jan 6, 2026
12 of 13 checks passed
@roji roji deleted the TranslationCleanup branch January 6, 2026 01:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants