Skip to content

Commit

Permalink
Merge pull request #250 from TNG/perf/assembly-loading
Browse files Browse the repository at this point in the history
perf(loader): improve assembly loading
  • Loading branch information
mak638 authored Apr 12, 2024
2 parents df1a6bb + 84e8020 commit 98f7f0e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
10 changes: 6 additions & 4 deletions ArchUnitNET/Loader/ArchBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ internal class ArchBuilder
{
private readonly ArchitectureCache _architectureCache;
private readonly ArchitectureCacheKey _architectureCacheKey;
private readonly List<IType> _architectureTypes = new List<IType>();
private readonly IDictionary<string, IType> _architectureTypes =
new Dictionary<string, IType>();
private readonly AssemblyRegistry _assemblyRegistry;
private readonly LoadTaskRegistry _loadTaskRegistry;
private readonly NamespaceRegistry _namespaceRegistry;
Expand All @@ -43,7 +44,7 @@ public ArchBuilder()
_architectureCache = ArchitectureCache.Instance;
}

public IEnumerable<IType> Types => _architectureTypes;
public IEnumerable<IType> Types => _architectureTypes.Values;
public IEnumerable<Assembly> Assemblies => _assemblyRegistry.Assemblies;
public IEnumerable<Namespace> Namespaces => _namespaceRegistry.Namespaces;

Expand Down Expand Up @@ -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();

Expand All @@ -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);
}
});

Expand Down
9 changes: 6 additions & 3 deletions ArchUnitNET/Loader/MonoCecilMemberExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 98f7f0e

Please sign in to comment.