Skip to content

Commit

Permalink
Improve performance of method, property and event overrides analyzers
Browse files Browse the repository at this point in the history
  • Loading branch information
ElektroKill committed Sep 22, 2023
1 parent 48cb130 commit 88da2fd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 38 deletions.
26 changes: 13 additions & 13 deletions Extensions/dnSpy.Analyzer/TreeNodes/EventOverriddenNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,23 @@ protected override void Write(ITextColorWriter output, IDecompiler decompiler) =
output.Write(BoxedTextColor.Text, dnSpy_Analyzer_Resources.OverridesTreeNode);

protected override IEnumerable<AnalyzerTreeNodeData> FetchChildren(CancellationToken ct) {
AddTypeEquivalentTypes(Context.DocumentService, analyzedTypes[0], analyzedTypes);
foreach (var declType in analyzedTypes) {
var analyzedAccessor = GetVirtualAccessor(analyzedEvent.AddMethod) ?? GetVirtualAccessor(analyzedEvent.RemoveMethod) ?? GetVirtualAccessor(analyzedEvent.InvokeMethod);
if (analyzedAccessor?.Overrides is IList<MethodOverride> overrides && overrides.Count > 0) {
bool matched = false;
foreach (var o in overrides) {
if (o.MethodDeclaration.ResolveMethodDef() is MethodDef method && (method.IsVirtual || method.IsAbstract)) {
if (method.DeclaringType.Events.FirstOrDefault(a => (object?)a.AddMethod == method || (object?)a.RemoveMethod == method || (object?)a.InvokeMethod == method) is EventDef eventDef) {
matched = true;
yield return new EventNode(eventDef) { Context = Context };
}
var analyzedAccessor = GetVirtualAccessor(analyzedEvent.AddMethod) ?? GetVirtualAccessor(analyzedEvent.RemoveMethod) ?? GetVirtualAccessor(analyzedEvent.InvokeMethod);
if (analyzedAccessor?.Overrides is IList<MethodOverride> overrides && overrides.Count > 0) {
bool matched = false;
foreach (var o in overrides) {
if (o.MethodDeclaration.ResolveMethodDef() is MethodDef method && (method.IsVirtual || method.IsAbstract)) {
if (method.DeclaringType.Events.FirstOrDefault(a => (object?)a.AddMethod == method || (object?)a.RemoveMethod == method || (object?)a.InvokeMethod == method) is EventDef eventDef) {
matched = true;
yield return new EventNode(eventDef) { Context = Context };
}
}
if (matched)
yield break;
}
if (matched)
yield break;
}

AddTypeEquivalentTypes(Context.DocumentService, analyzedTypes[0], analyzedTypes);
foreach (var declType in analyzedTypes) {
foreach (var eventDef in TypesHierarchyHelpers.FindBaseEvents(analyzedEvent, declType)) {
var anyAccessor = GetVirtualAccessor(eventDef.AddMethod) ?? GetVirtualAccessor(eventDef.RemoveMethod) ?? GetVirtualAccessor(eventDef.InvokeMethod);
if (anyAccessor is null || !(anyAccessor.IsVirtual || anyAccessor.IsAbstract))
Expand Down
23 changes: 12 additions & 11 deletions Extensions/dnSpy.Analyzer/TreeNodes/MethodOverriddenNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,21 @@ protected override void Write(ITextColorWriter output, IDecompiler decompiler) =
output.Write(BoxedTextColor.Text, dnSpy_Analyzer_Resources.OverridesTreeNode);

protected override IEnumerable<AnalyzerTreeNodeData> FetchChildren(CancellationToken ct) {
AddTypeEquivalentTypes(Context.DocumentService, analyzedTypes[0], analyzedTypes);
var overrides = analyzedMethod.Overrides;
foreach (var declType in analyzedTypes) {
if (overrides.Count > 0) {
bool matched = false;
foreach (var o in overrides) {
if (o.MethodDeclaration.ResolveMethodDef() is MethodDef method && (method.IsVirtual || method.IsAbstract)) {
matched = true;
yield return new MethodNode(method) { Context = Context };
}
if (overrides.Count > 0) {
bool matched = false;
foreach (var o in overrides) {
if (o.MethodDeclaration.ResolveMethodDef() is MethodDef method && (method.IsVirtual || method.IsAbstract)) {
matched = true;
yield return new MethodNode(method) { Context = Context };
}
if (matched)
yield break;
}
if (matched)
yield break;
}

AddTypeEquivalentTypes(Context.DocumentService, analyzedTypes[0], analyzedTypes);
foreach (var declType in analyzedTypes) {
foreach (var method in TypesHierarchyHelpers.FindBaseMethods(analyzedMethod, declType)) {
if (!(method.IsVirtual || method.IsAbstract))
continue;
Expand Down
28 changes: 14 additions & 14 deletions Extensions/dnSpy.Analyzer/TreeNodes/PropertyOverriddenNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,28 @@ protected override void Write(ITextColorWriter output, IDecompiler decompiler) =
output.Write(BoxedTextColor.Text, dnSpy_Analyzer_Resources.OverridesTreeNode);

protected override IEnumerable<AnalyzerTreeNodeData> FetchChildren(CancellationToken ct) {
AddTypeEquivalentTypes(Context.DocumentService, analyzedTypes[0], analyzedTypes);
foreach (var declType in analyzedTypes) {
var analyzedAccessor = GetVirtualAccessor(analyzedProperty.GetMethod) ?? GetVirtualAccessor(analyzedProperty.SetMethod);
if (analyzedAccessor?.Overrides is IList<MethodOverride> overrides && overrides.Count > 0) {
bool matched = false;
foreach (var o in overrides) {
if (o.MethodDeclaration.ResolveMethodDef() is MethodDef method && (method.IsVirtual || method.IsAbstract)) {
if (method.DeclaringType.Properties.FirstOrDefault(a => (object?)a.GetMethod == method || (object?)a.SetMethod == method) is PropertyDef property) {
matched = true;
yield return new PropertyNode(property) { Context = Context };
}
var analyzedAccessor = GetVirtualAccessor(analyzedProperty.GetMethod) ?? GetVirtualAccessor(analyzedProperty.SetMethod);
if (analyzedAccessor?.Overrides is IList<MethodOverride> overrides && overrides.Count > 0) {
bool matched = false;
foreach (var o in overrides) {
if (o.MethodDeclaration.ResolveMethodDef() is MethodDef method && (method.IsVirtual || method.IsAbstract)) {
if (method.DeclaringType.Properties.FirstOrDefault(a => (object?)a.GetMethod == method || (object?)a.SetMethod == method) is PropertyDef property) {
matched = true;
yield return new PropertyNode(property) { Context = Context };
}
}
if (matched)
yield break;
}
if (matched)
yield break;
}

AddTypeEquivalentTypes(Context.DocumentService, analyzedTypes[0], analyzedTypes);
foreach (var declType in analyzedTypes) {
foreach (var property in TypesHierarchyHelpers.FindBaseProperties(analyzedProperty, declType)) {
var anyAccessor = GetVirtualAccessor(property.GetMethod) ?? GetVirtualAccessor(property.SetMethod);
if (anyAccessor is null || !(anyAccessor.IsVirtual || anyAccessor.IsAbstract))
continue;
yield return new PropertyNode(property) { Context = Context };
yield return new PropertyNode(property) { Context = Context };
yield break;
}
}
Expand Down

0 comments on commit 88da2fd

Please sign in to comment.