-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimized source generator by using ForAttributeWithMetadataName meth…
…od and equality comparers for caching.
- Loading branch information
1 parent
ffbcc8c
commit 6a31597
Showing
3 changed files
with
173 additions
and
136 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
...eworkCore.Projectables.Generator/MemberDeclarationSyntaxAndCompilationEqualityComparer.cs
This file contains 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,22 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace EntityFrameworkCore.Projectables.Generator; | ||
|
||
public class MemberDeclarationSyntaxAndCompilationEqualityComparer : IEqualityComparer<(MemberDeclarationSyntax, Compilation)> | ||
{ | ||
public bool Equals((MemberDeclarationSyntax, Compilation) x, (MemberDeclarationSyntax, Compilation) y) | ||
{ | ||
return GetMemberDeclarationSyntaxAndCompilationName(x.Item1, x.Item2) == GetMemberDeclarationSyntaxAndCompilationName(y.Item1, y.Item2); | ||
} | ||
|
||
public int GetHashCode((MemberDeclarationSyntax, Compilation) obj) | ||
{ | ||
return GetMemberDeclarationSyntaxAndCompilationName(obj.Item1, obj.Item2).GetHashCode(); | ||
} | ||
|
||
public static string GetMemberDeclarationSyntaxAndCompilationName(MemberDeclarationSyntax memberDeclarationSyntax, Compilation compilation) | ||
{ | ||
return $"{compilation.AssemblyName}:{MemberDeclarationSyntaxEqualityComparer.GetMemberDeclarationSyntaxName(memberDeclarationSyntax)}"; | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/EntityFrameworkCore.Projectables.Generator/MemberDeclarationSyntaxEqualityComparer.cs
This file contains 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,63 @@ | ||
using System.Text; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace EntityFrameworkCore.Projectables.Generator; | ||
|
||
public class MemberDeclarationSyntaxEqualityComparer : IEqualityComparer<MemberDeclarationSyntax> | ||
{ | ||
public bool Equals(MemberDeclarationSyntax x, MemberDeclarationSyntax y) | ||
{ | ||
return GetMemberDeclarationSyntaxName(x) == GetMemberDeclarationSyntaxName(y); | ||
} | ||
|
||
public int GetHashCode(MemberDeclarationSyntax obj) | ||
{ | ||
return GetMemberDeclarationSyntaxName(obj).GetHashCode(); | ||
} | ||
|
||
public static string GetMemberDeclarationSyntaxName(MemberDeclarationSyntax memberDeclaration) | ||
{ | ||
var sb = new StringBuilder(); | ||
|
||
// Get the member name | ||
if (memberDeclaration is MethodDeclarationSyntax methodDeclaration) | ||
{ | ||
sb.Append(methodDeclaration.Identifier.Text); | ||
} | ||
else if (memberDeclaration is PropertyDeclarationSyntax propertyDeclaration) | ||
{ | ||
sb.Append(propertyDeclaration.Identifier.Text); | ||
} | ||
else if (memberDeclaration is FieldDeclarationSyntax fieldDeclaration) | ||
{ | ||
sb.Append(string.Join(", ", fieldDeclaration.Declaration.Variables.Select(v => v.Identifier.Text))); | ||
} | ||
|
||
// Traverse up the tree to get containing type names | ||
var parent = memberDeclaration.Parent; | ||
while (parent != null) | ||
{ | ||
switch (parent) | ||
{ | ||
case NamespaceDeclarationSyntax namespaceDeclaration: | ||
sb.Insert(0, namespaceDeclaration.Name + "."); | ||
break; | ||
case ClassDeclarationSyntax classDeclaration: | ||
sb.Insert(0, classDeclaration.Identifier.Text + "."); | ||
break; | ||
case StructDeclarationSyntax structDeclaration: | ||
sb.Insert(0, structDeclaration.Identifier.Text + "."); | ||
break; | ||
case InterfaceDeclarationSyntax interfaceDeclaration: | ||
sb.Insert(0, interfaceDeclaration.Identifier.Text + "."); | ||
break; | ||
case EnumDeclarationSyntax enumDeclaration: | ||
sb.Insert(0, enumDeclaration.Identifier.Text + "."); | ||
break; | ||
} | ||
parent = parent.Parent; | ||
} | ||
|
||
return sb.ToString(); | ||
} | ||
} |
This file contains 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