Optimize InternalUsageDiagnosticAnalyzer: replace ToDisplayString with symbol equality - #38726
Merged
Merged
Conversation
…ocations Instead of calling ToDisplayString() on every attribute class to compare against the EntityFrameworkInternalAttribute name (which allocates a new string each time), look up the INamedTypeSymbol for that attribute once per compilation in CompilationStart and use SymbolEqualityComparer.Default.Equals() for comparisons. Fixes #38723 Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Optimize ToDisplayString check in InternalUsageDiagnosticAnalyzer
Optimize InternalUsageDiagnosticAnalyzer: replace ToDisplayString with symbol equality
Jul 30, 2026
There was a problem hiding this comment.
Pull request overview
This PR optimizes InternalUsageDiagnosticAnalyzer by removing repeated ToDisplayString() allocations during attribute checks and replacing them with cached symbol equality comparisons, addressing perf findings from #38723.
Changes:
- Resolves
EntityFrameworkInternalAttributeonce per compilation viaRegisterCompilationStartAction+GetTypeByMetadataName. - Replaces per-attribute string formatting/comparison with
SymbolEqualityComparer.Default.Equals(...)against the cached symbol. - Threads the cached
INamedTypeSymbol?through the analyzer’s private call chain so the attribute check becomes a fast no-op when the symbol can’t be resolved.
RikkiGibson
approved these changes
Jul 31, 2026
AndriySvyryd
marked this pull request as ready for review
July 31, 2026 00:48
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.
HasInternalAttributewas callingToDisplayString()on every attribute class for every analyzed symbol to match againstEntityFrameworkInternalAttributeby name — allocating a new string each time. This was measured at ~36% of inclusive time in a perf trace, contributing ~2 MB of extra string allocations per build.Changes
RegisterCompilationStartActionand resolve theINamedTypeSymbolforEntityFrameworkInternalAttributeonce per compilation usingGetTypeByMetadataName.HasInternalAttributenow usesSymbolEqualityComparer.Default.Equals(a.AttributeClass, internalAttributeSymbol)— no allocation.HasInternalAttribute()now accept and forwardINamedTypeSymbol? internalAttributeSymbol. If EFCore isn't referenced (null), the attribute check is skipped entirely.