Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce allocations in code generated by Logging generators (#61162) #62011

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,18 @@ internal class Emitter
private const int MaxLoggerMessageDefineArguments = 6;
private const int DefaultStringBuilderCapacity = 1024;

private static readonly string s_generatedTypeSummary =
"<summary> " +
"This API supports the logging infrastructure and is not intended to be used directly from your code. " +
"It is subject to change in the future. " +
"</summary>";
private static readonly string s_generatedCodeAttribute =
$"global::System.CodeDom.Compiler.GeneratedCodeAttribute(" +
$"\"{typeof(Emitter).Assembly.GetName().Name}\", " +
$"\"{typeof(Emitter).Assembly.GetName().Version}\")";
private static readonly string s_editorBrowsableAttribute =
"global::System.ComponentModel.EditorBrowsableAttribute(" +
"global::System.ComponentModel.EditorBrowsableState.Never)";
private readonly StringBuilder _builder = new StringBuilder(DefaultStringBuilderCapacity);
private bool _needEnumerationHelper;

Expand Down Expand Up @@ -127,7 +135,9 @@ namespace {lc.Namespace}
private void GenStruct(LoggerMethod lm, string nestedIndentation)
{
_builder.AppendLine($@"
{nestedIndentation}/// {s_generatedTypeSummary}
maryamariyan marked this conversation as resolved.
Show resolved Hide resolved
{nestedIndentation}[{s_generatedCodeAttribute}]
{nestedIndentation}[{s_editorBrowsableAttribute}]
{nestedIndentation}private readonly struct __{lm.Name}Struct : global::System.Collections.Generic.IReadOnlyList<global::System.Collections.Generic.KeyValuePair<string, object?>>
{nestedIndentation}{{");
GenFields(lm, nestedIndentation);
Expand Down Expand Up @@ -156,7 +166,7 @@ private void GenStruct(LoggerMethod lm, string nestedIndentation)
{nestedIndentation}}}
");
_builder.Append($@"
{nestedIndentation}public static string Format(__{lm.Name}Struct state, global::System.Exception? ex) => state.ToString();
{nestedIndentation}public static readonly global::System.Func<__{lm.Name}Struct, global::System.Exception?, string> Format = (state, ex) => state.ToString();
eerhardt marked this conversation as resolved.
Show resolved Hide resolved

{nestedIndentation}public int Count => {lm.TemplateParameters.Count + 1};

Expand Down Expand Up @@ -489,7 +499,9 @@ private void GenEnumerationHelper()
if (_needEnumerationHelper)
{
_builder.Append($@"
/// {s_generatedTypeSummary}
[{s_generatedCodeAttribute}]
[{s_editorBrowsableAttribute}]
internal static class __LoggerMessageGenerator
{{
public static string Enumerate(global::System.Collections.IEnumerable? enumerable)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# editorconfig.org

[*.generated.txt]
insert_final_newline = false
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ namespace Microsoft.Extensions.Logging.Generators.Tests.TestClasses
{
partial class TestWithDefaultValues
{
/// <summary> This API supports the logging infrastructure and is not intended to be used directly from your code. It is subject to change in the future. </summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Logging.Generators", "%VERSION%")]
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]
private readonly struct __M0Struct : global::System.Collections.Generic.IReadOnlyList<global::System.Collections.Generic.KeyValuePair<string, object?>>
{

Expand All @@ -15,7 +17,7 @@ namespace Microsoft.Extensions.Logging.Generators.Tests.TestClasses
return $"";
}

public static string Format(__M0Struct state, global::System.Exception? ex) => state.ToString();
public static readonly global::System.Func<__M0Struct, global::System.Exception?, string> Format = (state, ex) => state.ToString();

public int Count => 1;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ namespace Microsoft.Extensions.Logging.Generators.Tests.TestClasses
{
partial class TestWithDynamicLogLevel
{
/// <summary> This API supports the logging infrastructure and is not intended to be used directly from your code. It is subject to change in the future. </summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Logging.Generators", "%VERSION%")]
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]
private readonly struct __M9Struct : global::System.Collections.Generic.IReadOnlyList<global::System.Collections.Generic.KeyValuePair<string, object?>>
{

Expand All @@ -15,7 +17,7 @@ namespace Microsoft.Extensions.Logging.Generators.Tests.TestClasses
return $"M9";
}

public static string Format(__M9Struct state, global::System.Exception? ex) => state.ToString();
public static readonly global::System.Func<__M9Struct, global::System.Exception?, string> Format = (state, ex) => state.ToString();

public int Count => 1;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ namespace Microsoft.Extensions.Logging.Generators.Tests.TestClasses
{
partial class TestWithMoreThan6Params
{
/// <summary> This API supports the logging infrastructure and is not intended to be used directly from your code. It is subject to change in the future. </summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Logging.Generators", "%VERSION%")]
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]
private readonly struct __Method9Struct : global::System.Collections.Generic.IReadOnlyList<global::System.Collections.Generic.KeyValuePair<string, object?>>
{
private readonly global::System.Int32 _p1;
Expand Down Expand Up @@ -41,7 +43,7 @@ namespace Microsoft.Extensions.Logging.Generators.Tests.TestClasses
return $"M9 {p1} {p2} {p3} {p4} {p5} {p6} {p7}";
}

public static string Format(__Method9Struct state, global::System.Exception? ex) => state.ToString();
public static readonly global::System.Func<__Method9Struct, global::System.Exception?, string> Format = (state, ex) => state.ToString();

public int Count => 8;

Expand Down Expand Up @@ -88,7 +90,9 @@ namespace Microsoft.Extensions.Logging.Generators.Tests.TestClasses
}
}
}
/// <summary> This API supports the logging infrastructure and is not intended to be used directly from your code. It is subject to change in the future. </summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Logging.Generators", "%VERSION%")]
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]
internal static class __LoggerMessageGenerator
{
public static string Enumerate(global::System.Collections.IEnumerable? enumerable)
Expand Down