Optimize HlslDefinitionsSyntaxProcessor.GetDeclaredTypes

このコミットが含まれているのは:
Sergio Pedri committed 2023-12-02 19:43:40 +01:00
1 親 b685f09d06
コミット 1660c8ca1b
1ファイル変更
+26 -9
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
using ComputeSharp.SourceGeneration.Mappings;
@@ -100,17 +99,35 @@ internal static class HlslDefinitionsSyntaxProcessor
VariableDeclarator(Identifier(mappedName!)))));
}
// Declare the methods of the current type
foreach (KeyValuePair<IMethodSymbol, MethodDeclarationSyntax> method in instanceMethods.Where(pair => SymbolEqualityComparer.Default.Equals(pair.Key.ContainingType, type)))
// Enumerate all members in a single pass, so we can avoid materializing the collection.
// Additionally, this lets us add all members to the struct declaration in a single go.
static IEnumerable<MethodDeclarationSyntax> GatherInstanceMethods(
INamedTypeSymbol type,
IReadOnlyDictionary<IMethodSymbol, MethodDeclarationSyntax> instanceMethods,
IReadOnlyDictionary<IMethodSymbol, (MethodDeclarationSyntax, MethodDeclarationSyntax)> constructors)
{
structDeclaration = structDeclaration.AddMembers(method.Value);
// Normal instance methods
foreach (KeyValuePair<IMethodSymbol, MethodDeclarationSyntax> method in instanceMethods)
{
if (SymbolEqualityComparer.Default.Equals(method.Key.ContainingType, type))
{
yield return method.Value;
}
}
// Constructors and stubs
foreach (KeyValuePair<IMethodSymbol, (MethodDeclarationSyntax Stub, MethodDeclarationSyntax Ctor)> methods in constructors)
{
if (SymbolEqualityComparer.Default.Equals(methods.Key.ContainingType, type))
{
yield return methods.Value.Stub;
yield return methods.Value.Ctor;
}
}
}
// Also do the same for all constructors
foreach (KeyValuePair<IMethodSymbol, (MethodDeclarationSyntax Stub, MethodDeclarationSyntax Ctor)> methods in constructors.Where(pair => SymbolEqualityComparer.Default.Equals(pair.Key.ContainingType, type)))
{
structDeclaration = structDeclaration.AddMembers(methods.Value.Stub, methods.Value.Ctor);
}
// Declare the methods of the current type
structDeclaration = structDeclaration.WithMembers(structDeclaration.Members.AddRange(GatherInstanceMethods(type, instanceMethods, constructors)));
// Insert the trailing ; right after the closing bracket (after normalization)
builder.Add((