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
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
31 changes: 10 additions & 21 deletions src/Castle.Core/DynamicProxy/Generators/MethodFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace Castle.DynamicProxy.Generators
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

/// <summary>
Expand All @@ -24,7 +25,7 @@ namespace Castle.DynamicProxy.Generators
/// </summary>
public class MethodFinder
{
private static readonly Dictionary<Type, object> cachedMethodInfosByType = new Dictionary<Type, object>();
private static readonly Dictionary<Type, MethodInfo[]> cachedMethodInfosByType = new Dictionary<Type, MethodInfo[]>();
private static readonly object lockObject = new object();

public static MethodInfo[] GetAllInstanceMethods(Type type, BindingFlags flags)
Expand All @@ -38,16 +39,18 @@ public static MethodInfo[] GetAllInstanceMethods(Type type, BindingFlags flags)

lock (lockObject)
{
if (!cachedMethodInfosByType.ContainsKey(type))
if (!cachedMethodInfosByType.TryGetValue(type, out methodsInCache))
{
// We always load all instance methods into the cache, we will filter them later
methodsInCache = type.GetMethods(
BindingFlags.Public | BindingFlags.NonPublic
| BindingFlags.Instance)
.Distinct(MethodSignatureComparer.Instance)
.ToArray();
cachedMethodInfosByType.Add(
type,
RemoveDuplicates(type.GetMethods(
BindingFlags.Public | BindingFlags.NonPublic
| BindingFlags.Instance)));
methodsInCache);
}
methodsInCache = (MethodInfo[])cachedMethodInfosByType[type];
}
return MakeFilteredCopy(methodsInCache, flags & (BindingFlags.Public | BindingFlags.NonPublic));
}
Expand Down Expand Up @@ -76,19 +79,5 @@ private static MethodInfo[] MakeFilteredCopy(MethodInfo[] methodsInCache, Bindin
return result.ToArray();
}

private static object RemoveDuplicates(MethodInfo[] infos)
{
var uniqueInfos = new Dictionary<MethodInfo, object>(MethodSignatureComparer.Instance);
foreach (var info in infos)
{
if (!uniqueInfos.ContainsKey(info))
{
uniqueInfos.Add(info, null);
}
}
var result = new MethodInfo[uniqueInfos.Count];
uniqueInfos.Keys.CopyTo(result, 0);
return result;
}
}
}
}
2 changes: 1 addition & 1 deletion src/Castle.Core/DynamicProxy/Internal/InvocationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,4 @@ public override int GetHashCode()
}
}
}
}
}