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

Improve cache performance of MethodFinder.GetAllInstanceMethods #357

Merged
merged 7 commits into from
Jun 1, 2018
Merged
Changes from 2 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
11 changes: 4 additions & 7 deletions src/Castle.Core/DynamicProxy/Generators/MethodFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,14 @@ private static MethodInfo[] MakeFilteredCopy(MethodInfo[] methodsInCache, Bindin

private static object RemoveDuplicates(MethodInfo[] infos)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Unrelated to this change, and I haven't looked at the call sites... but I wonder why this method's declared return type is object, instead of MethodInfo[]? Do call sites have to cast the return value back to MethodInfo[]?)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They get stored into a field Dictionary<Type, object> cachedMethodInfosByType, so yes this field should be updated too since there is only one call site of this private method.

{
var uniqueInfos = new Dictionary<MethodInfo, object>(MethodSignatureComparer.Instance);
var uniqueInfos = new HashSet<MethodInfo>(MethodSignatureComparer.Instance);
foreach (var info in infos)
{
if (!uniqueInfos.ContainsKey(info))
{
uniqueInfos.Add(info, null);
}
uniqueInfos.Add(info);
}
Copy link
Member

@stakx stakx May 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This foreach loop probably isn't needed at all when using the HashSet(IEnumerable<T> collection, IEqualityComparer<T> comparer) ctor instead.

Copy link
Member

@jonorossi jonorossi May 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seeing the call site it looks like this method could be removed and the call site just replaced with:

type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
    .Distinct(MethodSignatureComparer.Instance)
    .ToArray();

This code is cruddy because it would have been written pre-.NET 3.5 so before LINQ and HashSet.

var result = new MethodInfo[uniqueInfos.Count];
uniqueInfos.Keys.CopyTo(result, 0);
uniqueInfos.CopyTo(result, 0);
return result;
}
}
}
}