Skip to content

Commit

Permalink
Improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
mayuki committed Oct 25, 2023
1 parent c29bbda commit 6689b39
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using MagicOnion.Client.SourceGenerator.CodeAnalysis;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace MagicOnion.Client.SourceGenerator;
Expand All @@ -22,7 +23,7 @@ public void Execute(GeneratorExecutionContext context)

foreach (var (initializerClassDecl, semanticModel) in syntaxReceiver.Candidates)
{
var classSymbol = semanticModel.GetDeclaredSymbol(initializerClassDecl) as INamedTypeSymbol;
var classSymbol = semanticModel.GetDeclaredSymbol(initializerClassDecl);
if (classSymbol is null) continue;

var attrs = classSymbol.GetAttributes();
Expand All @@ -48,21 +49,21 @@ class SyntaxContextReceiver : ISyntaxContextReceiver

public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
{
if (context.Node is ClassDeclarationSyntax classDeclSyntax)
if (context.Node is AttributeSyntax attrSyntax &&
attrSyntax.Parent is AttributeListSyntax &&
attrSyntax.Parent.Parent is ClassDeclarationSyntax classDeclSyntax)
{
foreach (var attrList in classDeclSyntax.AttributeLists)
var attrName = attrSyntax.Name is QualifiedNameSyntax qualifiedName
? qualifiedName.Right.Identifier.ValueText
: ((IdentifierNameSyntax)attrSyntax.Name).Identifier.ValueText;

if ((attrName.EndsWith("Attribute") && attrName == MagicOnionClientSourceGenerator.MagicOnionClientGenerationAttributeShortName) ||
(attrName == MagicOnionClientSourceGenerator.MagicOnionClientGenerationAttributeName))
{
foreach (var attr in attrList.Attributes)
{
var attrSymbol = context.SemanticModel.GetSymbolInfo(attr).Symbol as IMethodSymbol;
if (attrSymbol is null) continue;

var attrContainingType = attrSymbol.ContainingType;
if (attrContainingType.ToDisplayString() == MagicOnionClientSourceGenerator.MagicOnionClientGenerationAttributeFullName)
{
Candidates.Add((classDeclSyntax,context.SemanticModel));
}
}
var attrSymbol = context.SemanticModel.GetSymbolInfo(attrSyntax).Symbol as IMethodSymbol;
if (attrSymbol is null) return;

Candidates.Add((classDeclSyntax, context.SemanticModel));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ namespace MagicOnion.Client.SourceGenerator;
public partial class MagicOnionClientSourceGenerator
{
public const string SourceGeneratorAttributesHintName = "MagicOnionClientSourceGeneratorAttributes.g.cs";
public const string MagicOnionClientGenerationAttributeName = "MagicOnionClientGenerationAttribute";
public const string MagicOnionClientGenerationAttributeShortName = "MagicOnionClientGeneration";
public const string MagicOnionClientGenerationAttributeName = $"{MagicOnionClientGenerationAttributeShortName}Attribute";
public const string MagicOnionClientGenerationAttributeFullName = $"MagicOnion.Client.{MagicOnionClientGenerationAttributeName}";

static class Emitter
Expand Down Expand Up @@ -122,12 +123,14 @@ public static void Emit(GenerationContext context, ImmutableArray<INamedTypeSymb

foreach (var service in serviceCollection.Services)
{
cancellationToken.ThrowIfCancellationRequested();
var x = StaticMagicOnionClientGenerator.Build(context, service);
outputs.Add((GeneratePathFromNamespaceAndTypeName(service.ServiceType.Namespace, service.GetClientName()), x));
}

foreach (var hub in serviceCollection.Hubs)
{
cancellationToken.ThrowIfCancellationRequested();
var x = StaticStreamingHubClientGenerator.Build(context, hub);
outputs.Add((GeneratePathFromNamespaceAndTypeName(hub.ServiceType.Namespace, hub.GetClientName()), x));
}
Expand Down

0 comments on commit 6689b39

Please sign in to comment.