Lucene.Net.Core.Util.AttributeSource: Added the WeakIdentityMap as per Lucene and removed references to the WeakDictionary.
Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/dbcb4e02 Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/dbcb4e02 Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/dbcb4e02 Branch: refs/heads/api-work Commit: dbcb4e02c6fe759e83f52e83eba727fb49865012 Parents: f265acc Author: Shad Storhaug <[email protected]> Authored: Wed Mar 22 03:45:11 2017 +0700 Committer: Shad Storhaug <[email protected]> Committed: Wed Mar 22 03:45:11 2017 +0700 ---------------------------------------------------------------------- src/Lucene.Net.Core/Util/AttributeSource.cs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucenenet/blob/dbcb4e02/src/Lucene.Net.Core/Util/AttributeSource.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Core/Util/AttributeSource.cs b/src/Lucene.Net.Core/Util/AttributeSource.cs index b230cb8..9f753dd 100644 --- a/src/Lucene.Net.Core/Util/AttributeSource.cs +++ b/src/Lucene.Net.Core/Util/AttributeSource.cs @@ -55,9 +55,8 @@ namespace Lucene.Net.Util private sealed class DefaultAttributeFactory : AttributeFactory { - //LUCENE TO-DO - //internal static readonly WeakIdentityMap<Type, WeakReference> attClassImplMap = new WeakIdentityMap<Type, WeakReference>(); - internal static readonly WeakDictionary<Type, WeakReference> attClassImplMap = new WeakDictionary<Type, WeakReference>(); + internal static readonly WeakIdentityMap<Type, WeakReference> attClassImplMap = + WeakIdentityMap<Type, WeakReference>.NewConcurrentHashMap(false); internal DefaultAttributeFactory() { @@ -78,8 +77,7 @@ namespace Lucene.Net.Util internal static Type GetClassForInterface<T>() where T : IAttribute { var attClass = typeof(T); - WeakReference @ref; - attClassImplMap.TryGetValue(attClass, out @ref); + WeakReference @ref = attClassImplMap.Get(attClass); Type clazz = (@ref == null) ? null : (Type)@ref.Target; if (clazz == null) { @@ -87,7 +85,7 @@ namespace Lucene.Net.Util try { string name = attClass.FullName.Replace(attClass.Name, attClass.Name.Substring(1)) + ", " + attClass.GetTypeInfo().Assembly.FullName; - attClassImplMap.Add(attClass, new WeakReference(clazz = Type.GetType(name, true))); + attClassImplMap.Put(attClass, new WeakReference(clazz = Type.GetType(name, true))); } catch (Exception e) { @@ -257,14 +255,14 @@ namespace Lucene.Net.Util /// <summary> /// a cache that stores all interfaces for known implementation classes for performance (slow reflection) </summary> - private static readonly WeakDictionary<Type, LinkedList<WeakReference>> knownImplClasses = new WeakDictionary<Type, LinkedList<WeakReference>>(); + private static readonly WeakIdentityMap<Type, LinkedList<WeakReference>> knownImplClasses = + WeakIdentityMap<Type, LinkedList<WeakReference>>.NewConcurrentHashMap(false); internal static LinkedList<WeakReference> GetAttributeInterfaces(Type clazz) { - LinkedList<WeakReference> foundInterfaces; + LinkedList<WeakReference> foundInterfaces = knownImplClasses.Get(clazz); lock (knownImplClasses) { - knownImplClasses.TryGetValue(clazz, out foundInterfaces); if (foundInterfaces == null) { // we have the slight chance that another thread may do the same, but who cares? @@ -283,7 +281,7 @@ namespace Lucene.Net.Util } actClazz = actClazz.GetTypeInfo().BaseType; } while (actClazz != null); - knownImplClasses[clazz] = foundInterfaces; + knownImplClasses.Put(clazz, foundInterfaces); } } return foundInterfaces;
