Skip to content

Commit 53ad5c4

Browse files
committed
NH-3885 - Replace ThreadSafeDictionary wrapper
with Framework's ConcurrentDictionary.
1 parent e6a0b47 commit 53ad5c4

File tree

7 files changed

+27
-206
lines changed

7 files changed

+27
-206
lines changed

src/NHibernate.Test/UtilityTest/ThreadSafeDictionaryFixture.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Concurrent;
23
using System.Collections.Generic;
34
using System.Threading;
45
using log4net;
@@ -100,10 +101,10 @@ public void MultiThreadAccess()
100101
},
101102
};
102103
MultiThreadRunner<IDictionary<int, int>> mtr = new MultiThreadRunner<IDictionary<int, int>>(20, actions);
103-
IDictionary<int, int> wrapper = new ThreadSafeDictionary<int, int>(new Dictionary<int, int>());
104+
IDictionary<int, int> wrapper = new ConcurrentDictionary<int, int>();
104105
mtr.EndTimeout = 2000;
105106
mtr.Run(wrapper);
106107
log.DebugFormat("{0} reads, {1} writes -- elements {2}", read, write, wrapper.Count);
107108
}
108109
}
109-
}
110+
}

src/NHibernate/Impl/SessionFactoryImpl.cs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Concurrent;
23
using System.Collections.Generic;
34
using System.Data.Common;
45
using System.Linq;
@@ -101,8 +102,8 @@ public void HandleEntityNotFound(string entityName, object id)
101102
private static readonly IIdentifierGenerator UuidGenerator = new UUIDHexGenerator();
102103

103104
[NonSerialized]
104-
private readonly ThreadSafeDictionary<string, ICache> allCacheRegions =
105-
new ThreadSafeDictionary<string, ICache>(new Dictionary<string, ICache>());
105+
private readonly ConcurrentDictionary<string, ICache> allCacheRegions =
106+
new ConcurrentDictionary<string, ICache>();
106107

107108
[NonSerialized]
108109
private readonly IDictionary<string, IClassMetadata> classMetadata;
@@ -155,7 +156,7 @@ public void HandleEntityNotFound(string entityName, object id)
155156
private readonly IQueryCache queryCache;
156157

157158
[NonSerialized]
158-
private readonly IDictionary<string, IQueryCache> queryCaches;
159+
private readonly ConcurrentDictionary<string, IQueryCache> queryCaches;
159160
[NonSerialized]
160161
private readonly SchemaExport schemaExport;
161162
[NonSerialized]
@@ -168,7 +169,7 @@ public void HandleEntityNotFound(string entityName, object id)
168169
[NonSerialized]
169170
private readonly UpdateTimestampsCache updateTimestampsCache;
170171
[NonSerialized]
171-
private readonly IDictionary<string, string[]> entityNameImplementorsMap = new ThreadSafeDictionary<string, string[]>(new Dictionary<string, string[]>(100));
172+
private readonly IDictionary<string, string[]> entityNameImplementorsMap = new ConcurrentDictionary<string, string[]>(4 * System.Environment.ProcessorCount, 100);
172173
private readonly string uuid;
173174
private bool disposed;
174175

@@ -255,7 +256,7 @@ public SessionFactoryImpl(Configuration cfg, IMapping mapping, Settings settings
255256
if (cache != null)
256257
{
257258
caches.Add(cacheRegion, cache);
258-
allCacheRegions.Add(cache.RegionName, cache.Cache);
259+
((IDictionary<string, ICache>)allCacheRegions).Add(cache.RegionName, cache.Cache);
259260
}
260261
}
261262
IEntityPersister cp = PersisterFactory.CreateClassPersister(model, cache, this, mapping);
@@ -383,7 +384,7 @@ public SessionFactoryImpl(Configuration cfg, IMapping mapping, Settings settings
383384
{
384385
updateTimestampsCache = new UpdateTimestampsCache(settings, properties);
385386
queryCache = settings.QueryCacheFactory.GetQueryCache(null, updateTimestampsCache, settings, properties);
386-
queryCaches = new ThreadSafeDictionary<string, IQueryCache>(new Dictionary<string, IQueryCache>());
387+
queryCaches = new ConcurrentDictionary<string, IQueryCache>();
387388
}
388389
else
389390
{
@@ -975,10 +976,7 @@ public UpdateTimestampsCache UpdateTimestampsCache
975976

976977
public IDictionary<string, ICache> GetAllSecondLevelCacheRegions()
977978
{
978-
lock (allCacheRegions.SyncRoot)
979-
{
980-
return new Dictionary<string, ICache>(allCacheRegions);
981-
}
979+
return allCacheRegions.ToDictionary(kv => kv.Key, kv => kv.Value);
982980
}
983981

984982
public ICache GetSecondLevelCacheRegion(string regionName)
@@ -1010,18 +1008,14 @@ public IQueryCache GetQueryCache(string cacheRegion)
10101008
return null;
10111009
}
10121010

1013-
lock (allCacheRegions.SyncRoot)
1014-
{
1015-
IQueryCache currentQueryCache;
1016-
if (!queryCaches.TryGetValue(cacheRegion, out currentQueryCache))
1011+
return queryCaches.GetOrAdd(
1012+
cacheRegion,
1013+
cr =>
10171014
{
1018-
currentQueryCache =
1019-
settings.QueryCacheFactory.GetQueryCache(cacheRegion, updateTimestampsCache, settings, properties);
1020-
queryCaches[cacheRegion] = currentQueryCache;
1015+
IQueryCache currentQueryCache = settings.QueryCacheFactory.GetQueryCache(cacheRegion, updateTimestampsCache, settings, properties);
10211016
allCacheRegions[currentQueryCache.RegionName] = currentQueryCache.Cache;
1022-
}
1023-
return currentQueryCache;
1024-
}
1017+
return currentQueryCache;
1018+
});
10251019
}
10261020

10271021
public void EvictQueries()

src/NHibernate/NHibernate.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1802,7 +1802,6 @@
18021802
<Compile Include="Util\SimpleMRUCache.cs" />
18031803
<Compile Include="Util\SingletonEnumerable.cs" />
18041804
<Compile Include="Util\SoftLimitMRUCache.cs" />
1805-
<Compile Include="Util\ThreadSafeDictionary.cs" />
18061805
<Compile Include="Util\TypeNameParser.cs" />
18071806
<Compile Include="Util\UnmodifiableDictionary.cs" />
18081807
<Compile Include="Util\WeakHashtable.cs" />

src/NHibernate/Proxy/DynamicProxy/ProxyCache.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66

77
#endregion
88

9+
using System.Collections.Concurrent;
910
using System.Collections.Generic;
1011
using NHibernate.Util;
1112

1213
namespace NHibernate.Proxy.DynamicProxy
1314
{
1415
public class ProxyCache : IProxyCache
1516
{
16-
private static readonly IDictionary<ProxyCacheEntry, System.Type> cache = new ThreadSafeDictionary<ProxyCacheEntry, System.Type>(new Dictionary<ProxyCacheEntry, System.Type>());
17+
private static readonly IDictionary<ProxyCacheEntry, System.Type> cache = new ConcurrentDictionary<ProxyCacheEntry, System.Type>();
1718

1819
#region IProxyCache Members
1920

@@ -53,4 +54,4 @@ public void StoreProxyType(System.Type result, System.Type baseType, params Syst
5354

5455
#endregion
5556
}
56-
}
57+
}

src/NHibernate/SqlTypes/SqlTypeFactory.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Concurrent;
23
using System.Data;
34
using System.Collections.Generic;
45
using System.Runtime.CompilerServices;
@@ -17,7 +18,7 @@ public static class SqlTypeFactory
1718
// key = typeof(sqlType).Name : ie - BinarySqlType(l), BooleanSqlType, DecimalSqlType(p,s)
1819
// value = SqlType
1920
private static readonly IDictionary<string, SqlType> SqlTypes =
20-
new ThreadSafeDictionary<string, SqlType>(new Dictionary<string, SqlType>(128));
21+
new ConcurrentDictionary<string, SqlType>(4 * System.Environment.ProcessorCount, 128);
2122

2223
public static readonly SqlType Guid = new SqlType(DbType.Guid);
2324
public static readonly SqlType Boolean = new SqlType(DbType.Boolean);
@@ -116,4 +117,4 @@ private static string GetKeyForPrecisionScaleBased(string name, byte precision,
116117
return name + "(" + precision + ", " + scale + ")";
117118
}
118119
}
119-
}
120+
}

src/NHibernate/Type/TypeFactory.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections;
3+
using System.Collections.Concurrent;
34
using System.Collections.Generic;
45
using System.Globalization;
56
using System.Reflection;
@@ -62,13 +63,13 @@ private enum TypeClassification
6263
*/
6364

6465
private static readonly IDictionary<string, IType> typeByTypeOfName =
65-
new ThreadSafeDictionary<string, IType>(new Dictionary<string, IType>());
66+
new ConcurrentDictionary<string, IType>();
6667

6768
private static readonly IDictionary<string, GetNullableTypeWithLength> getTypeDelegatesWithLength =
68-
new ThreadSafeDictionary<string, GetNullableTypeWithLength>(new Dictionary<string, GetNullableTypeWithLength>());
69+
new ConcurrentDictionary<string, GetNullableTypeWithLength>();
6970

7071
private static readonly IDictionary<string, GetNullableTypeWithPrecision> getTypeDelegatesWithPrecision =
71-
new ThreadSafeDictionary<string, GetNullableTypeWithPrecision>(new Dictionary<string, GetNullableTypeWithPrecision>());
72+
new ConcurrentDictionary<string, GetNullableTypeWithPrecision>();
7273

7374
private delegate NullableType GetNullableTypeWithLength(int length); // Func<int, NullableType>
7475

src/NHibernate/Util/ThreadSafeDictionary.cs

Lines changed: 0 additions & 176 deletions
This file was deleted.

0 commit comments

Comments
 (0)