-
Notifications
You must be signed in to change notification settings - Fork 31
Improve source generator and analyzer responsiveness #171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b8ca7bf
Improve source generator and analyzer responsiveness, while limiting …
cc398fa
Merge branch 'refs/heads/master' into bugfix/inconsistent-analyzer
82481ad
Improve comparers without ToFullString calls and don't use Linq in Pr…
daec909
Initial plan
Copilot 4506f4a
Add incremental generator tests and fix source-tree equality check in…
Copilot ff58a34
Merge pull request #172 from koenbeuk/copilot/sub-pr-171
PhenX 88987e7
Merge remote-tracking branch 'origin/bugfix/inconsistent-analyzer' in…
38016f3
We cannot track changes in other files as it's too resource consuming
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
74 changes: 64 additions & 10 deletions
74
...eworkCore.Projectables.Generator/MemberDeclarationSyntaxAndCompilationEqualityComparer.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,76 @@ | ||
| using Microsoft.CodeAnalysis; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Runtime.CompilerServices; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
|
||
| namespace EntityFrameworkCore.Projectables.Generator; | ||
|
|
||
| public class MemberDeclarationSyntaxAndCompilationEqualityComparer : IEqualityComparer<(MemberDeclarationSyntax, Compilation)> | ||
| public class MemberDeclarationSyntaxAndCompilationEqualityComparer | ||
| : IEqualityComparer<((MemberDeclarationSyntax Member, ProjectableAttributeData Attribute), Compilation)> | ||
| { | ||
| public bool Equals((MemberDeclarationSyntax, Compilation) x, (MemberDeclarationSyntax, Compilation) y) | ||
| { | ||
| return GetMemberDeclarationSyntaxAndCompilationName(x.Item1, x.Item2) == GetMemberDeclarationSyntaxAndCompilationName(y.Item1, y.Item2); | ||
| } | ||
| private readonly static MemberDeclarationSyntaxEqualityComparer _memberComparer = new(); | ||
|
|
||
| public int GetHashCode((MemberDeclarationSyntax, Compilation) obj) | ||
| public bool Equals( | ||
| ((MemberDeclarationSyntax Member, ProjectableAttributeData Attribute), Compilation) x, | ||
| ((MemberDeclarationSyntax Member, ProjectableAttributeData Attribute), Compilation) y) | ||
| { | ||
| return GetMemberDeclarationSyntaxAndCompilationName(obj.Item1, obj.Item2).GetHashCode(); | ||
| var (xLeft, xCompilation) = x; | ||
| var (yLeft, yCompilation) = y; | ||
|
|
||
| // 1. Fast reference equality short-circuit | ||
| if (ReferenceEquals(xLeft.Member, yLeft.Member) && | ||
| ReferenceEquals(xCompilation, yCompilation)) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| // 2. The syntax tree of the member's own file must be the same object | ||
| // (Roslyn reuses SyntaxTree instances for unchanged files, even when | ||
| // the Compilation object itself is new due to edits elsewhere) | ||
| // Single pointer comparison — very cheap. | ||
| if (!ReferenceEquals(xLeft.Member.SyntaxTree, yLeft.Member.SyntaxTree)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| // 3. Attribute arguments (primitive record struct) — cheap value comparison | ||
| if (xLeft.Attribute != yLeft.Attribute) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| // 4. Member text — string allocation, only reached when the SyntaxTree is shared | ||
| if (!_memberComparer.Equals(xLeft.Member, yLeft.Member)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| // 5. Assembly-level references — most expensive (ImmutableArray enumeration) | ||
| return xCompilation.ExternalReferences.SequenceEqual(yCompilation.ExternalReferences); | ||
| } | ||
|
|
||
| public static string GetMemberDeclarationSyntaxAndCompilationName(MemberDeclarationSyntax memberDeclarationSyntax, Compilation compilation) | ||
| public int GetHashCode(((MemberDeclarationSyntax Member, ProjectableAttributeData Attribute), Compilation) obj) | ||
| { | ||
| return $"{compilation.AssemblyName}:{MemberDeclarationSyntaxEqualityComparer.GetMemberDeclarationSyntaxName(memberDeclarationSyntax)}"; | ||
| var (left, compilation) = obj; | ||
| unchecked | ||
| { | ||
| var hash = 17; | ||
| hash = hash * 31 + _memberComparer.GetHashCode(left.Member); | ||
| hash = hash * 31 + RuntimeHelpers.GetHashCode(left.Member.SyntaxTree); | ||
| hash = hash * 31 + left.Attribute.GetHashCode(); | ||
|
|
||
| // Incorporate compilation external references to align with Equals | ||
| var references = compilation.ExternalReferences; | ||
| var referencesHash = 17; | ||
| referencesHash = referencesHash * 31 + references.Length; | ||
| foreach (var reference in references) | ||
| { | ||
| referencesHash = referencesHash * 31 + RuntimeHelpers.GetHashCode(reference); | ||
| } | ||
| hash = hash * 31 + referencesHash; | ||
|
|
||
| return hash; | ||
| } | ||
| } | ||
| } | ||
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
62 changes: 62 additions & 0 deletions
62
src/EntityFrameworkCore.Projectables.Generator/ProjectableAttributeData.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| using Microsoft.CodeAnalysis; | ||
|
|
||
| namespace EntityFrameworkCore.Projectables.Generator; | ||
|
|
||
| /// <summary> | ||
| /// Plain-data snapshot of the [Projectable] attribute arguments. | ||
| /// </summary> | ||
| public readonly record struct ProjectableAttributeData | ||
| { | ||
| public NullConditionalRewriteSupport NullConditionalRewriteSupport { get; } | ||
| public string? UseMemberBody { get; } | ||
| public bool ExpandEnumMethods { get; } | ||
| public bool AllowBlockBody { get; } | ||
|
|
||
| public ProjectableAttributeData(AttributeData attribute) | ||
| { | ||
| var nullConditionalRewriteSupport = default(NullConditionalRewriteSupport); | ||
| string? useMemberBody = null; | ||
| var expandEnumMethods = false; | ||
| var allowBlockBody = false; | ||
|
|
||
| foreach (var namedArgument in attribute.NamedArguments) | ||
| { | ||
| var key = namedArgument.Key; | ||
| var value = namedArgument.Value; | ||
| switch (key) | ||
| { | ||
| case "NullConditionalRewriteSupport": | ||
| if (value.Kind == TypedConstantKind.Enum && | ||
| value.Value is not null && | ||
| Enum.IsDefined(typeof(NullConditionalRewriteSupport), value.Value)) | ||
| { | ||
| nullConditionalRewriteSupport = (NullConditionalRewriteSupport)value.Value; | ||
| } | ||
| break; | ||
| case "UseMemberBody": | ||
| if (value.Value is string s) | ||
| { | ||
| useMemberBody = s; | ||
| } | ||
| break; | ||
| case "ExpandEnumMethods": | ||
| if (value.Value is bool expand && expand) | ||
| { | ||
| expandEnumMethods = true; | ||
| } | ||
| break; | ||
| case "AllowBlockBody": | ||
| if (value.Value is bool allow && allow) | ||
| { | ||
| allowBlockBody = true; | ||
| } | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| NullConditionalRewriteSupport = nullConditionalRewriteSupport; | ||
| UseMemberBody = useMemberBody; | ||
| ExpandEnumMethods = expandEnumMethods; | ||
| AllowBlockBody = allowBlockBody; | ||
| } | ||
| } |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.