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

perf(loader): improve assembly loading #250

Merged
merged 2 commits into from
Apr 12, 2024
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
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
Loading