From fb559bab8bea4388ba06c5ccd510bb531039bd91 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 8 Dec 2023 13:34:31 +0100 Subject: [PATCH] Optimize HlslKnownTypes.IsNonLinearMatrixType lookups --- .../Mappings/HlslKnownTypes.cs | 72 +++++++++++++++---- 1 file changed, 60 insertions(+), 12 deletions(-) diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownTypes.cs index b108d3ca..97089fd5 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownTypes.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Mappings/HlslKnownTypes.cs @@ -4,7 +4,6 @@ using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Linq; using System.Numerics; -using System.Text.RegularExpressions; using ComputeSharp.SourceGeneration.Extensions; using Microsoft.CodeAnalysis; @@ -71,6 +70,11 @@ internal static partial class HlslKnownTypes /// private static readonly IReadOnlyDictionary KnownHlslTypeMetadataNames = BuildKnownHlslTypeMetadataNames(); + /// + /// The mapping of type info for all supported known matrix types to HLSL types. + /// + private static readonly IReadOnlyDictionary KnownNonLinearMatrixTypeInfo = BuildKnownNonLinearMatrixTypeInfo(); + /// /// Builds the mapping of known HLSL vector types. /// @@ -122,6 +126,48 @@ internal static partial class HlslKnownTypes elementSelector: static p => p.Value); } + /// + /// Builds the mapping of type info for all supported known matrix types to HLSL types. + /// + /// The mapping of type info for all supported known matrix types to HLSL types. + private static Dictionary BuildKnownNonLinearMatrixTypeInfo() + { + Type[] knownTypes = + [ + typeof(Bool2x1), typeof(Bool2x2), typeof(Bool2x3), typeof(Bool2x4), + typeof(Bool3x1), typeof(Bool3x2), typeof(Bool3x3), typeof(Bool3x4), + typeof(Bool4x1), typeof(Bool4x2), typeof(Bool4x3), typeof(Bool4x4), + typeof(Int2x1), typeof(Int2x2), typeof(Int2x3), typeof(Int2x4), + typeof(Int3x1), typeof(Int3x2), typeof(Int3x3), typeof(Int3x4), + typeof(Int4x1), typeof(Int4x2), typeof(Int4x3), typeof(Int4x4), + typeof(UInt2x1), typeof(UInt2x2), typeof(UInt2x3), typeof(UInt2x4), + typeof(UInt3x1), typeof(UInt3x2), typeof(UInt3x3), typeof(UInt3x4), + typeof(UInt4x1), typeof(UInt4x2), typeof(UInt4x3), typeof(UInt4x4), + typeof(Float2x1), typeof(Float2x2), typeof(Float2x3), typeof(Float2x4), + typeof(Float3x1), typeof(Float3x2), typeof(Float3x3), typeof(Float3x4), + typeof(Float4x1), typeof(Float4x2), typeof(Float4x3), typeof(Float4x4), + typeof(Double2x1), typeof(Double2x2), typeof(Double2x3), typeof(Double2x4), + typeof(Double3x1), typeof(Double3x2), typeof(Double3x3), typeof(Double3x4), + typeof(Double4x1), typeof(Double4x2), typeof(Double4x3), typeof(Double4x4) + ]; + + static NonLinearMatrixTypeInfo CreateTypeInfo(Type type) + { + // Extract the info for each non linear matrix type: + // - The name is just the prefix without the NxM part + // - The number of rows is the first suffix character + // - The number of columns is the last suffix character + return new( + ElementName: type.Name[..^3], + Rows: int.Parse(type.Name[^3].ToString(), CultureInfo.InvariantCulture), + Columns: int.Parse(type.Name[^1].ToString(), CultureInfo.InvariantCulture)); + } + + return knownTypes.ToDictionary( + keySelector: static type => type.FullName, + elementSelector: CreateTypeInfo); + } + /// /// Enumerates the known HLSL vector types. /// @@ -181,22 +227,16 @@ internal static partial class HlslKnownTypes /// Whether or not represents a non linear matrix type. public static bool IsNonLinearMatrixType(string typeName, out string? elementName, out int rows, out int columns) { - if (KnownMatrixTypes.Any(type => type.FullName == typeName)) + if (KnownNonLinearMatrixTypeInfo.TryGetValue(typeName, out NonLinearMatrixTypeInfo? value)) { - Match match = Regex.Match(typeName, @"^ComputeSharp\.(Bool|Int|UInt|Float|Double)([2-4])x([1-4])$"); + (elementName, rows, columns) = value; - if (match.Success) - { - elementName = match.Groups[1].Value; - rows = int.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture); - columns = int.Parse(match.Groups[3].Value, CultureInfo.InvariantCulture); - - return true; - } + return true; } elementName = null; - rows = columns = 0; + rows = 0; + columns = 0; return false; } @@ -383,4 +423,12 @@ internal static partial class HlslKnownTypes } } } + + /// + /// A simple model with info on a non linear matrix type. + /// + /// The element name of the matrix type. + /// The number of rows in the matrix type. + /// The number of columns in the matrix type. + private sealed record NonLinearMatrixTypeInfo(string ElementName, int Rows, int Columns); } \ No newline at end of file