From ea30bdb8e7ad488819d5b7dbc0a5221782b43420 Mon Sep 17 00:00:00 2001 From: Alexander Linne Date: Fri, 12 Apr 2024 06:53:09 +0200 Subject: [PATCH] perf(loader): improve assembly loading Signed-off-by: Alexander Linne --- ArchUnitNET/Loader/ArchBuilder.cs | 10 ++++++---- ArchUnitNET/Loader/MonoCecilMemberExtensions.cs | 9 ++++++--- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/ArchUnitNET/Loader/ArchBuilder.cs b/ArchUnitNET/Loader/ArchBuilder.cs index 12b470cc..ce24e6fd 100644 --- a/ArchUnitNET/Loader/ArchBuilder.cs +++ b/ArchUnitNET/Loader/ArchBuilder.cs @@ -19,7 +19,8 @@ internal class ArchBuilder { private readonly ArchitectureCache _architectureCache; private readonly ArchitectureCacheKey _architectureCacheKey; - private readonly List _architectureTypes = new List(); + private readonly IDictionary _architectureTypes = + new Dictionary(); private readonly AssemblyRegistry _assemblyRegistry; private readonly LoadTaskRegistry _loadTaskRegistry; private readonly NamespaceRegistry _namespaceRegistry; @@ -43,7 +44,7 @@ public ArchBuilder() _architectureCache = ArchitectureCache.Instance; } - public IEnumerable Types => _architectureTypes; + public IEnumerable Types => _architectureTypes.Select(pair => pair.Value); public IEnumerable Assemblies => _assemblyRegistry.Assemblies; public IEnumerable Namespaces => _namespaceRegistry.Namespaces; @@ -83,6 +84,7 @@ public void LoadTypesForModule(ModuleDefinition module, string namespaceFilter) t.FullName != "Microsoft.CodeAnalysis.EmbeddedAttribute" && t.FullName != "System.Runtime.CompilerServices.NullableAttribute" && t.FullName != "System.Runtime.CompilerServices.NullableContextAttribute" + && !t.FullName.StartsWith("Coverlet") ) .ToList(); @@ -109,10 +111,10 @@ public void LoadTypesForModule(ModuleDefinition module, string namespaceFilter) .ForEach(typeDefinition => { var type = _typeFactory.GetOrCreateTypeFromTypeReference(typeDefinition); - if (!_architectureTypes.Contains(type) && !type.IsCompilerGenerated) + if (!_architectureTypes.ContainsKey(type.FullName) && !type.IsCompilerGenerated) { currentTypes.Add(type); - _architectureTypes.Add(type); + _architectureTypes.Add(type.FullName, type); } }); diff --git a/ArchUnitNET/Loader/MonoCecilMemberExtensions.cs b/ArchUnitNET/Loader/MonoCecilMemberExtensions.cs index faef7edb..a50c7720 100644 --- a/ArchUnitNET/Loader/MonoCecilMemberExtensions.cs +++ b/ArchUnitNET/Loader/MonoCecilMemberExtensions.cs @@ -312,15 +312,18 @@ TypeFactory typeFactory internal static bool IsCompilerGenerated(this MemberReference memberReference) { + if (memberReference.Name.HasCompilerGeneratedName()) + { + return true; + } var declaringType = memberReference.Resolve()?.DeclaringType ?? memberReference.DeclaringType; - return declaringType != null && declaringType.Name.HasCompilerGeneratedName() - || memberReference.Name.HasCompilerGeneratedName(); + return declaringType != null && declaringType.Name.HasCompilerGeneratedName(); } internal static bool HasCompilerGeneratedName(this string name) { - return name.StartsWith("<") || name.StartsWith("!"); + return name[0] == '<' || name[0] == '!'; } internal static MethodForm GetMethodForm(this MethodDefinition methodDefinition)