From eb34817073cbf9ce04b6ed73ec2604939c6fd56a Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 7 Oct 2022 14:00:33 +0200 Subject: [PATCH] Remove allocations when checking duplicate resource indices --- ...rator.CreateLoadInputDescriptionsMethod.cs | 16 +++++++++++----- ...teLoadResourceTextureDescriptionsMethod.cs | 19 ++++++++++++++----- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadInputDescriptionsMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadInputDescriptionsMethod.cs index ca9a12e4..c1b9182c 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadInputDescriptionsMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadInputDescriptionsMethod.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; using ComputeSharp.D2D1.SourceGenerators.Models; @@ -71,14 +70,21 @@ partial class ID2D1ShaderGenerator return; } - HashSet inputDescriptionIndices = new(inputDescriptionsBuilder.Select(description => description.Index)); + Span selectedInputDescriptionIndices = stackalloc bool[8]; // All input description indices must be unique - if (inputDescriptionIndices.Count != inputDescriptionsBuilder.Count) + foreach (InputDescription inputDescription in inputDescriptionsBuilder) { - diagnostics.Add(RepeatedD2DInputDescriptionIndices, structDeclarationSymbol, structDeclarationSymbol); + ref bool isInputDescriptionIndexUsed = ref selectedInputDescriptionIndices[(int)inputDescription.Index]; - return; + if (isInputDescriptionIndexUsed) + { + diagnostics.Add(RepeatedD2DInputDescriptionIndices, structDeclarationSymbol, structDeclarationSymbol); + + return; + } + + isInputDescriptionIndexUsed = true; } inputDescriptions = inputDescriptionsBuilder.ToImmutable(); diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadResourceTextureDescriptionsMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadResourceTextureDescriptionsMethod.cs index 3a128e56..c9fd1576 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadResourceTextureDescriptionsMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadResourceTextureDescriptionsMethod.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System; using System.Collections.Immutable; using System.Linq; using ComputeSharp.D2D1.SourceGenerators.Models; @@ -99,12 +99,21 @@ partial class ID2D1ShaderGenerator return; } - HashSet resourceTextureIndices = new(resourceTextureInfos.Where(info => info.Index.HasValue).Select(info => info.Index!.Value)); + Span selectedResourceTextureIndices = stackalloc bool[16]; - // All input description indices must be unique - if (resourceTextureIndices.Count != resourceTextureInfos.Count(info => info.Index.HasValue)) + // All input description indices must be unique (also take this path for invalid indices) + foreach ((int? index, _) in resourceTextureInfos) { - diagnostics.Add(RepeatedD2DResourceTextureIndices, structDeclarationSymbol, structDeclarationSymbol); + ref bool isResourceTextureIndexUsed = ref selectedResourceTextureIndices[index ?? 0]; + + if (isResourceTextureIndexUsed) + { + diagnostics.Add(RepeatedD2DResourceTextureIndices, structDeclarationSymbol, structDeclarationSymbol); + + return; + } + + isResourceTextureIndexUsed = true; } } }