From 955bdc2a0fa92ee494c6a6707eb6bdd86b5e9b63 Mon Sep 17 00:00:00 2001 From: David Fowler Date: Mon, 22 Mar 2021 12:14:45 -0700 Subject: [PATCH 1/4] Remove some allocations related to logging configuration - Today we allocate enumerators when enumerating custom attributes, this changes a for loop into a foreach and removes the inner foreach loop. --- .../ProviderAliasUtilities.cs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/libraries/Common/src/Extensions/ProviderAliasUtilities/ProviderAliasUtilities.cs b/src/libraries/Common/src/Extensions/ProviderAliasUtilities/ProviderAliasUtilities.cs index 40e80be6447651..76295c46765385 100644 --- a/src/libraries/Common/src/Extensions/ProviderAliasUtilities/ProviderAliasUtilities.cs +++ b/src/libraries/Common/src/Extensions/ProviderAliasUtilities/ProviderAliasUtilities.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; +using System.Collections.Generic; using System.Diagnostics; using System.Reflection; @@ -13,16 +14,24 @@ internal static class ProviderAliasUtilities internal static string GetAlias(Type providerType) { - foreach (CustomAttributeData attributeData in CustomAttributeData.GetCustomAttributes(providerType)) + IList attributes = CustomAttributeData.GetCustomAttributes(providerType); + + for (int i = 0; i < attributes.Count; i++) { + CustomAttributeData attributeData = attributes[i]; if (attributeData.AttributeType.FullName == AliasAttibuteTypeFullName) { - foreach (CustomAttributeTypedArgument arg in attributeData.ConstructorArguments) + if (attributeData.ConstructorArguments.Count == 0) { - Debug.Assert(arg.ArgumentType == typeof(string)); - - return arg.Value?.ToString(); + // Maybe the user defined their own ProviderAliasAttribute? + continue; } + + CustomAttributeTypedArgument arg = attributeData.ConstructorArguments[0]; + + Debug.Assert(arg.ArgumentType == typeof(string)); + + return arg.Value?.ToString(); } } From e929473cd6d287d29c1997d5a0a4686afb868b81 Mon Sep 17 00:00:00 2001 From: David Fowler Date: Mon, 22 Mar 2021 12:49:58 -0700 Subject: [PATCH 2/4] Remove enumerator allocation from rules selector --- .../Microsoft.Extensions.Logging/src/LoggerFilterOptions.cs | 5 ++++- .../Microsoft.Extensions.Logging/src/LoggerRuleSelector.cs | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.Logging/src/LoggerFilterOptions.cs b/src/libraries/Microsoft.Extensions.Logging/src/LoggerFilterOptions.cs index 68066e0c5e272f..57f2a3ba76c9e0 100644 --- a/src/libraries/Microsoft.Extensions.Logging/src/LoggerFilterOptions.cs +++ b/src/libraries/Microsoft.Extensions.Logging/src/LoggerFilterOptions.cs @@ -28,6 +28,9 @@ public LoggerFilterOptions() { } /// /// Gets the collection of used for filtering log messages. /// - public IList Rules { get; } = new List(); + public IList Rules => RulesInternal; + + // Concrete represenation of the rule list + internal List RulesInternal { get; } = new List(); } } diff --git a/src/libraries/Microsoft.Extensions.Logging/src/LoggerRuleSelector.cs b/src/libraries/Microsoft.Extensions.Logging/src/LoggerRuleSelector.cs index c2876ed1b6e570..a24e9838d0b538 100644 --- a/src/libraries/Microsoft.Extensions.Logging/src/LoggerRuleSelector.cs +++ b/src/libraries/Microsoft.Extensions.Logging/src/LoggerRuleSelector.cs @@ -22,7 +22,7 @@ public static void Select(LoggerFilterOptions options, Type providerType, string string providerAlias = ProviderAliasUtilities.GetAlias(providerType); LoggerFilterRule current = null; - foreach (LoggerFilterRule rule in options.Rules) + foreach (LoggerFilterRule rule in options.RulesInternal) { if (IsBetter(rule, current, providerType.FullName, category) || (!string.IsNullOrEmpty(providerAlias) && IsBetter(rule, current, providerAlias, category))) From e89576be135607645df0c0c8f025ae7df24b7c2e Mon Sep 17 00:00:00 2001 From: David Fowler Date: Mon, 22 Mar 2021 13:53:49 -0700 Subject: [PATCH 3/4] Make condition clearer --- .../ProviderAliasUtilities/ProviderAliasUtilities.cs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/libraries/Common/src/Extensions/ProviderAliasUtilities/ProviderAliasUtilities.cs b/src/libraries/Common/src/Extensions/ProviderAliasUtilities/ProviderAliasUtilities.cs index 76295c46765385..69e7687e62665f 100644 --- a/src/libraries/Common/src/Extensions/ProviderAliasUtilities/ProviderAliasUtilities.cs +++ b/src/libraries/Common/src/Extensions/ProviderAliasUtilities/ProviderAliasUtilities.cs @@ -19,14 +19,9 @@ internal static string GetAlias(Type providerType) for (int i = 0; i < attributes.Count; i++) { CustomAttributeData attributeData = attributes[i]; - if (attributeData.AttributeType.FullName == AliasAttibuteTypeFullName) + if (attributeData.AttributeType.FullName == AliasAttibuteTypeFullName && + attributeData.ConstructorArguments.Count > 0) { - if (attributeData.ConstructorArguments.Count == 0) - { - // Maybe the user defined their own ProviderAliasAttribute? - continue; - } - CustomAttributeTypedArgument arg = attributeData.ConstructorArguments[0]; Debug.Assert(arg.ArgumentType == typeof(string)); From 014b87efd086d5e0e145937430a733b90bae03db Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Mon, 22 Mar 2021 22:29:07 -0400 Subject: [PATCH 4/4] Update src/libraries/Microsoft.Extensions.Logging/src/LoggerFilterOptions.cs Co-authored-by: Jonas Nyrup <78789299+jn-foreflight@users.noreply.github.com> --- .../Microsoft.Extensions.Logging/src/LoggerFilterOptions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/Microsoft.Extensions.Logging/src/LoggerFilterOptions.cs b/src/libraries/Microsoft.Extensions.Logging/src/LoggerFilterOptions.cs index 57f2a3ba76c9e0..3c8165e4d23713 100644 --- a/src/libraries/Microsoft.Extensions.Logging/src/LoggerFilterOptions.cs +++ b/src/libraries/Microsoft.Extensions.Logging/src/LoggerFilterOptions.cs @@ -30,7 +30,7 @@ public LoggerFilterOptions() { } /// public IList Rules => RulesInternal; - // Concrete represenation of the rule list + // Concrete representation of the rule list internal List RulesInternal { get; } = new List(); } }