From c40bdf0a267b20e3cb4ffe25e1ccc6ae80d5696a Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Jul 2021 22:12:37 +0200 Subject: [PATCH] Remove unnecessary ArrayPoolStringBuilder public methods --- ...erGenerator.CreateBuildHlslStringMethod.cs | 58 +++++++++---------- .../__Internals/ArrayPoolStringBuilder.cs | 49 +++------------- 2 files changed, 37 insertions(+), 70 deletions(-) diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslStringMethod.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslStringMethod.cs index 7d74cc11..f59ea44d 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslStringMethod.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslStringMethod.cs @@ -486,31 +486,6 @@ namespace ComputeSharp.SourceGenerators List statements = new(); StringBuilder textBuilder = new(); int capturedDelegates = 0; - - foreach (var (fieldSymbol, fieldName, fieldType) in instanceFields) - { - if (fieldSymbol.Type.TypeKind == TypeKind.Delegate) - { - // ShaderMethodSourceAttribute __Attribute = ShaderMethodSourceAttribute.GetForDelegate(, ""); - statements.Add( - LocalDeclarationStatement(VariableDeclaration(IdentifierName(nameof(ShaderMethodSourceAttribute))) - .AddVariables( - VariableDeclarator(Identifier($"__{fieldName}Attribute")) - .WithInitializer( - EqualsValueClause( - InvocationExpression( - MemberAccessExpression( - SyntaxKind.SimpleMemberAccessExpression, - IdentifierName(nameof(ShaderMethodSourceAttribute)), - IdentifierName(nameof(ShaderMethodSourceAttribute.GetForDelegate)))) - .AddArgumentListArguments( - Argument(IdentifierName(fieldName)), - Argument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(fieldName))))))))); - - capturedDelegates++; - } - } - int sizeHint = 64; void AppendLF() @@ -558,6 +533,31 @@ namespace ComputeSharp.SourceGenerators } } + // Go through all existing delegate fields, if any + foreach (var (fieldSymbol, fieldName, fieldType) in instanceFields) + { + if (fieldSymbol.Type.TypeKind == TypeKind.Delegate) + { + // ShaderMethodSourceAttribute __Attribute = ShaderMethodSourceAttribute.GetForDelegate(, ""); + statements.Add( + LocalDeclarationStatement(VariableDeclaration(IdentifierName(nameof(ShaderMethodSourceAttribute))) + .AddVariables( + VariableDeclarator(Identifier($"__{fieldName}Attribute")) + .WithInitializer( + EqualsValueClause( + InvocationExpression( + MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + IdentifierName(nameof(ShaderMethodSourceAttribute)), + IdentifierName(nameof(ShaderMethodSourceAttribute.GetForDelegate)))) + .AddArgumentListArguments( + Argument(IdentifierName(fieldName)), + Argument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(fieldName))))))))); + + capturedDelegates++; + } + } + // Header AppendLineAndLF("// ================================================"); AppendLineAndLF("// AUTO GENERATED"); @@ -568,11 +568,11 @@ namespace ComputeSharp.SourceGenerators // Group size constants AppendLF(); AppendLine("#define __GroupSize__get_X "); - AppendStatement("builder.AppendLine(threadsX.ToString());"); + AppendStatement("builder.Append(threadsX);"); AppendLine("#define __GroupSize__get_Y "); - AppendStatement("builder.AppendLine(threadsY.ToString());"); + AppendStatement("builder.Append(threadsY);"); AppendLine("#define __GroupSize__get_Z "); - AppendStatement("builder.AppendLine(threadsZ.ToString());"); + AppendStatement("builder.Append(threadsZ);"); // Define declarations foreach (var (name, value) in definedConstants) @@ -707,7 +707,7 @@ namespace ComputeSharp.SourceGenerators if (fieldSymbol.Type.TypeKind == TypeKind.Delegate) { AppendLF(); - AppendStatement($"builder.AppendLine(__{fieldName}Attribute.{nameof(ShaderMethodSourceAttribute.GetMappedInvokeMethod)}(\"{fieldName}\"));"); + AppendStatement($"builder.Append(__{fieldName}Attribute.{nameof(ShaderMethodSourceAttribute.GetMappedInvokeMethod)}(\"{fieldName}\"));"); } } diff --git a/src/ComputeSharp/Shaders/Renderer/__Internals/ArrayPoolStringBuilder.cs b/src/ComputeSharp/Shaders/Renderer/__Internals/ArrayPoolStringBuilder.cs index cda9f57a..b5f1069e 100644 --- a/src/ComputeSharp/Shaders/Renderer/__Internals/ArrayPoolStringBuilder.cs +++ b/src/ComputeSharp/Shaders/Renderer/__Internals/ArrayPoolStringBuilder.cs @@ -1,5 +1,6 @@ using System; using System.Buffers; +using System.ComponentModel; using System.Diagnostics; using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; @@ -9,6 +10,8 @@ namespace ComputeSharp.__Internals /// /// A helper type that implements a pooled buffer writer for values. /// + [EditorBrowsable(EditorBrowsableState.Never)] + [Obsolete("This type is not intended to be used directly by user code")] [DebuggerTypeProxy(typeof(DebugView))] public ref struct ArrayPoolStringBuilder { @@ -59,49 +62,13 @@ namespace ComputeSharp.__Internals } /// - /// Appends a character to the current buffer. + /// Appends the text representation of the input value to the current buffer. /// - public void Append(char character) + /// The input value to write. + public void Append(int value) { - EnsureCapacity(1); - - this.array[this.index++] = character; - } - - /// - /// Appends the input sequence of characters to the current buffer, with a trailing new line. - /// - /// The input characters to write. - public void AppendLine(string value) - { - EnsureCapacity(value.Length + 1); - - value.AsSpan().CopyTo(this.array.AsSpan(this.index)); - - this.index += value.Length; - - this.array[this.index++] = '\n'; - } - - /// - /// Appends a character and a new line to the current buffer. - /// - public void AppendLine(char character) - { - EnsureCapacity(2); - - this.array[this.index++] = character; - this.array[this.index++] = '\n'; - } - - /// - /// Appends a new line to the current buffer. - /// - public void AppendLine() - { - EnsureCapacity(1); - - this.array[this.index++] = '\n'; + // TODO: switch to ISpanFormattable on .NET 6 + Append(value.ToString()); } ///