From 9e3aff1fa8b0d1b8200655e41144b6e9ee17d86a Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Thu, 12 Nov 2020 17:12:36 -0500 Subject: [PATCH] Rewrite AnalyzeFileReference.GetSupportedLanguages without LINQ I did an allocation profile of csc.dll for building a simple "hello world" with the configuration `dotnet build` employs on a simple `dotnet new console` app. There are ~194K allocations / ~19MB. And ~30K / ~1MB of those are occurring unnecessarily because of use of LINQ in one function. This just rewrites that function to not use LINQ. --- .../AnalyzerFileReference.cs | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerFileReference.cs b/src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerFileReference.cs index 438c8ed501be7..46d1b3ed487f7 100644 --- a/src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerFileReference.cs +++ b/src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerFileReference.cs @@ -243,12 +243,20 @@ from supportedLanguage in supportedLanguages private static IEnumerable GetSupportedLanguages(TypeDefinition typeDef, PEModule peModule, Type attributeType, AttributeLanguagesFunc languagesFunc) { - var attributeLanguagesList = from customAttrHandle in typeDef.GetCustomAttributes() - where peModule.IsTargetAttribute(customAttrHandle, attributeType.Namespace!, attributeType.Name, ctor: out _) - let attributeSupportedLanguages = languagesFunc(peModule, customAttrHandle) - where attributeSupportedLanguages != null - select attributeSupportedLanguages; - return attributeLanguagesList.SelectMany(x => x); + foreach (CustomAttributeHandle customAttrHandle in typeDef.GetCustomAttributes()) + { + if (peModule.IsTargetAttribute(customAttrHandle, attributeType.Namespace!, attributeType.Name, ctor: out _)) + { + IEnumerable? attributeSupportedLanguages = languagesFunc(peModule, customAttrHandle); + if (attributeSupportedLanguages != null) + { + foreach (string item in attributeSupportedLanguages) + { + yield return item; + } + } + } + } } private static IEnumerable GetDiagnosticsAnalyzerSupportedLanguages(PEModule peModule, CustomAttributeHandle customAttrHandle)