From 785f33258128740b9fb43a014f0ad3d18cbb1b08 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 6 Dec 2023 00:48:40 +0100 Subject: [PATCH] Optimize HlslSourceRewriter.VisitIdentifierName --- .../SyntaxRewriters/HlslSourceRewriter.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs index 6c6bb244..e035e44a 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs @@ -394,7 +394,11 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter { IdentifierNameSyntax updatedNode = (IdentifierNameSyntax)base.VisitIdentifierName(node)!; - if (SemanticModel.For(node).GetOperation(node) is IFieldReferenceOperation operation && + // Only gather constants directly accessed by name. We can also pre-filter to exclude invocations + // and member access expressions, as those will be handled separately. Doing so avoids unnecessarily + // retrieving semantic information for every identifier, which would otherwise be fairly expensive. + if (node.Parent is not (InvocationExpressionSyntax or MemberAccessExpressionSyntax) && + SemanticModel.For(node).GetOperation(node) is IFieldReferenceOperation operation && operation.Field.IsConst && operation.Type!.TypeKind != TypeKind.Enum) {