Skip to content

Commit

Permalink
Improve generator perf (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
viceroypenguin authored Jan 18, 2025
1 parent 61bc84b commit 74fe1ab
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/Immediate.Handlers.Generators/ImmediateHandlersGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
var hasMsDi = context
.MetadataReferencesProvider
.Collect()
.Select((refs, _) => refs
.Any(r => (r.Display ?? "")
.Contains("Microsoft.Extensions.DependencyInjection.Abstractions")));
.Select(
(refs, _) => refs
.Any(
r => (r.Display ?? "")
.Contains("Microsoft.Extensions.DependencyInjection.Abstractions")
)
);

var assemblyName = context.CompilationProvider
.Select((cp, _) => cp.AssemblyName!
Expand All @@ -27,9 +31,11 @@ public void Initialize(IncrementalGeneratorInitializationContext context)

var @namespace = context
.AnalyzerConfigOptionsProvider
.Select((c, _) => c.GlobalOptions
.TryGetValue("build_property.rootnamespace", out var ns)
? ns : null);
.Select(
(c, _) => c.GlobalOptions
.TryGetValue("build_property.rootnamespace", out var ns)
? ns : null
);

var behaviors = context.SyntaxProvider
.ForAttributeWithMetadataName(
Expand All @@ -43,8 +49,9 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
var handlers = context.SyntaxProvider
.ForAttributeWithMetadataName(
"Immediate.Handlers.Shared.HandlerAttribute",
predicate: (_, _) => true,
TransformHandler);
predicate: (node, _) => node is TypeDeclarationSyntax,
TransformHandler
);

var handlerNodes = handlers
.Combine(behaviors)
Expand All @@ -58,7 +65,8 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
handler: node.Left.Left,
behaviors: node.Left.Right,
hasMsDi: node.Right,
template)
template
)
);

var registrationNodes = handlers
Expand All @@ -67,7 +75,8 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
.Combine(behaviors)
.Combine(@namespace
.Combine(hasMsDi)
.Combine(assemblyName));
.Combine(assemblyName)
);

context.RegisterSourceOutput(
registrationNodes,
Expand Down

0 comments on commit 74fe1ab

Please sign in to comment.