From 9694023e239a0d6c4b7c4d6db11062786dbfcf9c Mon Sep 17 00:00:00 2001 From: tangdf <520812+tangdf@users.noreply.github.com> Date: Fri, 11 May 2018 23:16:23 +0800 Subject: [PATCH 1/4] Replace Dictionary to HashSet --- .../DynamicProxy/Generators/MethodFinder.cs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/Castle.Core/DynamicProxy/Generators/MethodFinder.cs b/src/Castle.Core/DynamicProxy/Generators/MethodFinder.cs index 085e907251..5fc9131032 100644 --- a/src/Castle.Core/DynamicProxy/Generators/MethodFinder.cs +++ b/src/Castle.Core/DynamicProxy/Generators/MethodFinder.cs @@ -78,17 +78,14 @@ private static MethodInfo[] MakeFilteredCopy(MethodInfo[] methodsInCache, Bindin private static object RemoveDuplicates(MethodInfo[] infos) { - var uniqueInfos = new Dictionary(MethodSignatureComparer.Instance); + var uniqueInfos = new HashSet(MethodSignatureComparer.Instance); foreach (var info in infos) { - if (!uniqueInfos.ContainsKey(info)) - { - uniqueInfos.Add(info, null); - } + uniqueInfos.Add(info); } var result = new MethodInfo[uniqueInfos.Count]; - uniqueInfos.Keys.CopyTo(result, 0); + uniqueInfos.CopyTo(result, 0); return result; } } -} \ No newline at end of file +} From b3ffa4c7565e03060c4d35ee52570e18b112db0e Mon Sep 17 00:00:00 2001 From: tangdf Date: Mon, 14 May 2018 13:09:05 +0800 Subject: [PATCH 2/4] Replace KeyValuePair to custom struct `KeyValuePair` not implemented interface `IEquatable<>`. --- .../DynamicProxy/Internal/InvocationHelper.cs | 42 ++++++++++++++++--- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/src/Castle.Core/DynamicProxy/Internal/InvocationHelper.cs b/src/Castle.Core/DynamicProxy/Internal/InvocationHelper.cs index b7df8fce3b..423e6da0a0 100644 --- a/src/Castle.Core/DynamicProxy/Internal/InvocationHelper.cs +++ b/src/Castle.Core/DynamicProxy/Internal/InvocationHelper.cs @@ -24,8 +24,8 @@ namespace Castle.DynamicProxy.Internal public static class InvocationHelper { - private static readonly Dictionary, MethodInfo> cache = - new Dictionary, MethodInfo>(); + private static readonly Dictionary cache = + new Dictionary(); private static readonly Lock @lock = Lock.Create(); @@ -70,7 +70,7 @@ public static MethodInfo GetMethodOnType(Type type, MethodInfo proxiedMethod) private static MethodInfo GetFromCache(MethodInfo methodInfo, Type type) { - var key = new KeyValuePair(methodInfo, type); + var key = new CacheKey(methodInfo, type); MethodInfo method; cache.TryGetValue(key, out method); return method; @@ -122,8 +122,40 @@ private static MethodInfo ObtainMethod(MethodInfo proxiedMethod, Type type) private static void PutToCache(MethodInfo methodInfo, Type type, MethodInfo value) { - var key = new KeyValuePair(methodInfo, type); + var key = new CacheKey(methodInfo, type); cache.Add(key, value); } + + private struct CacheKey:IEquatable + { + public CacheKey(MethodInfo method,Type type ) + { + Method = method; + Type = type; + } + public MethodInfo Method { get; } + + public Type Type { get; } + + public bool Equals(CacheKey other) + { + return object.ReferenceEquals(Method, other.Method) && object.ReferenceEquals(Type, other.Type); + } + + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) + return false; + return obj is CacheKey @struct && Equals(@struct); + } + + public override int GetHashCode() + { + unchecked + { + return ((Method != null ? Method.GetHashCode() : 0) * 397) ^ (Type != null ? Type.GetHashCode() : 0); + } + } + } } -} \ No newline at end of file +} From a6ffe329112c8324ebe5486395bad0d8fd7c31c1 Mon Sep 17 00:00:00 2001 From: tangdf Date: Thu, 31 May 2018 23:01:25 +0800 Subject: [PATCH 3/4] Remove the RemoveDuplicates method --- .../DynamicProxy/Generators/MethodFinder.cs | 25 ++++++------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/src/Castle.Core/DynamicProxy/Generators/MethodFinder.cs b/src/Castle.Core/DynamicProxy/Generators/MethodFinder.cs index 5fc9131032..84fc19d773 100644 --- a/src/Castle.Core/DynamicProxy/Generators/MethodFinder.cs +++ b/src/Castle.Core/DynamicProxy/Generators/MethodFinder.cs @@ -24,7 +24,7 @@ namespace Castle.DynamicProxy.Generators /// public class MethodFinder { - private static readonly Dictionary cachedMethodInfosByType = new Dictionary(); + private static readonly Dictionary cachedMethodInfosByType = new Dictionary(); private static readonly object lockObject = new object(); public static MethodInfo[] GetAllInstanceMethods(Type type, BindingFlags flags) @@ -38,16 +38,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)); } @@ -76,16 +78,5 @@ private static MethodInfo[] MakeFilteredCopy(MethodInfo[] methodsInCache, Bindin return result.ToArray(); } - private static object RemoveDuplicates(MethodInfo[] infos) - { - var uniqueInfos = new HashSet(MethodSignatureComparer.Instance); - foreach (var info in infos) - { - uniqueInfos.Add(info); - } - var result = new MethodInfo[uniqueInfos.Count]; - uniqueInfos.CopyTo(result, 0); - return result; - } } } From 857367bedc17ec9b803635d7c984890efa9d424f Mon Sep 17 00:00:00 2001 From: tangdf Date: Thu, 31 May 2018 23:05:14 +0800 Subject: [PATCH 4/4] Add namespace references --- src/Castle.Core/DynamicProxy/Generators/MethodFinder.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Castle.Core/DynamicProxy/Generators/MethodFinder.cs b/src/Castle.Core/DynamicProxy/Generators/MethodFinder.cs index 84fc19d773..85c611f33c 100644 --- a/src/Castle.Core/DynamicProxy/Generators/MethodFinder.cs +++ b/src/Castle.Core/DynamicProxy/Generators/MethodFinder.cs @@ -16,6 +16,7 @@ namespace Castle.DynamicProxy.Generators { using System; using System.Collections.Generic; + using System.Linq; using System.Reflection; ///