Skip to content

Commit 898184b

Browse files
committed
NH-3885 - Replace ThreadSafeDictionary wrapper
with Framework's ConcurrentDictionary.
1 parent 16c1459 commit 898184b

File tree

7 files changed

+28
-207
lines changed

7 files changed

+28
-207
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: 15 additions & 21 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;
45
using System.Linq;
@@ -93,8 +94,8 @@ public void HandleEntityNotFound(string entityName, object id)
9394
private static readonly IIdentifierGenerator UuidGenerator = new UUIDHexGenerator();
9495

9596
[NonSerialized]
96-
private readonly ThreadSafeDictionary<string, ICache> allCacheRegions =
97-
new ThreadSafeDictionary<string, ICache>(new Dictionary<string, ICache>());
97+
private readonly ConcurrentDictionary<string, ICache> allCacheRegions =
98+
new ConcurrentDictionary<string, ICache>();
9899

99100
[NonSerialized]
100101
private readonly IDictionary<string, IClassMetadata> classMetadata;
@@ -147,7 +148,7 @@ public void HandleEntityNotFound(string entityName, object id)
147148
private readonly IQueryCache queryCache;
148149

149150
[NonSerialized]
150-
private readonly IDictionary<string, IQueryCache> queryCaches;
151+
private readonly ConcurrentDictionary<string, IQueryCache> queryCaches;
151152
[NonSerialized]
152153
private readonly SchemaExport schemaExport;
153154
[NonSerialized]
@@ -160,7 +161,7 @@ public void HandleEntityNotFound(string entityName, object id)
160161
[NonSerialized]
161162
private readonly UpdateTimestampsCache updateTimestampsCache;
162163
[NonSerialized]
163-
private readonly IDictionary<string, string[]> entityNameImplementorsMap = new ThreadSafeDictionary<string, string[]>(new Dictionary<string, string[]>(100));
164+
private readonly IDictionary<string, string[]> entityNameImplementorsMap = new ConcurrentDictionary<string, string[]>(4 * System.Environment.ProcessorCount, 100);
164165
private readonly string uuid;
165166
private bool disposed;
166167

@@ -247,7 +248,7 @@ public SessionFactoryImpl(Configuration cfg, IMapping mapping, Settings settings
247248
if (cache != null)
248249
{
249250
caches.Add(cacheRegion, cache);
250-
allCacheRegions.Add(cache.RegionName, cache.Cache);
251+
((IDictionary<string, ICache>)allCacheRegions).Add(cache.RegionName, cache.Cache);
251252
}
252253
}
253254
IEntityPersister cp = PersisterFactory.CreateClassPersister(model, cache, this, mapping);
@@ -375,7 +376,7 @@ public SessionFactoryImpl(Configuration cfg, IMapping mapping, Settings settings
375376
{
376377
updateTimestampsCache = new UpdateTimestampsCache(settings, properties);
377378
queryCache = settings.QueryCacheFactory.GetQueryCache(null, updateTimestampsCache, settings, properties);
378-
queryCaches = new ThreadSafeDictionary<string, IQueryCache>(new Dictionary<string, IQueryCache>());
379+
queryCaches = new ConcurrentDictionary<string, IQueryCache>();
379380
}
380381
else
381382
{
@@ -967,10 +968,7 @@ public UpdateTimestampsCache UpdateTimestampsCache
967968

968969
public IDictionary<string, ICache> GetAllSecondLevelCacheRegions()
969970
{
970-
lock (allCacheRegions.SyncRoot)
971-
{
972-
return new Dictionary<string, ICache>(allCacheRegions);
973-
}
971+
return allCacheRegions.ToDictionary(kv => kv.Key, kv => kv.Value);
974972
}
975973

976974
public ICache GetSecondLevelCacheRegion(string regionName)
@@ -1002,18 +1000,14 @@ public IQueryCache GetQueryCache(string cacheRegion)
10021000
return null;
10031001
}
10041002

1005-
lock (allCacheRegions.SyncRoot)
1006-
{
1007-
IQueryCache currentQueryCache;
1008-
if (!queryCaches.TryGetValue(cacheRegion, out currentQueryCache))
1003+
return queryCaches.GetOrAdd(
1004+
cacheRegion,
1005+
cr =>
10091006
{
1010-
currentQueryCache =
1011-
settings.QueryCacheFactory.GetQueryCache(cacheRegion, updateTimestampsCache, settings, properties);
1012-
queryCaches[cacheRegion] = currentQueryCache;
1007+
IQueryCache currentQueryCache = settings.QueryCacheFactory.GetQueryCache(cacheRegion, updateTimestampsCache, settings, properties);
10131008
allCacheRegions[currentQueryCache.RegionName] = currentQueryCache.Cache;
1014-
}
1015-
return currentQueryCache;
1016-
}
1009+
return currentQueryCache;
1010+
});
10171011
}
10181012

10191013
public void EvictQueries()
@@ -1277,4 +1271,4 @@ public string Uuid
12771271

12781272
#endregion
12791273
}
1280-
}
1274+
}

src/NHibernate/NHibernate.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1783,7 +1783,6 @@
17831783
<Compile Include="Util\SimpleMRUCache.cs" />
17841784
<Compile Include="Util\SingletonEnumerable.cs" />
17851785
<Compile Include="Util\SoftLimitMRUCache.cs" />
1786-
<Compile Include="Util\ThreadSafeDictionary.cs" />
17871786
<Compile Include="Util\TypeNameParser.cs" />
17881787
<Compile Include="Util\UnmodifiableDictionary.cs" />
17891788
<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;
@@ -15,7 +16,7 @@ public static class SqlTypeFactory
1516
// key = typeof(sqlType).Name : ie - BinarySqlType(l), BooleanSqlType, DecimalSqlType(p,s)
1617
// value = SqlType
1718
private static readonly IDictionary<string, SqlType> SqlTypes =
18-
new ThreadSafeDictionary<string, SqlType>(new Dictionary<string, SqlType>(128));
19+
new ConcurrentDictionary<string, SqlType>(4 * System.Environment.ProcessorCount, 128);
1920

2021
public static readonly SqlType Guid = new SqlType(DbType.Guid);
2122
public static readonly SqlType Boolean = new SqlType(DbType.Boolean);
@@ -112,4 +113,4 @@ private static string GetKeyForPrecisionScaleBased(string name, byte precision,
112113
return name + "(" + precision + ", " + scale + ")";
113114
}
114115
}
115-
}
116+
}

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;
@@ -61,13 +62,13 @@ private enum TypeClassification
6162
*/
6263

6364
private static readonly IDictionary<string, IType> typeByTypeOfName =
64-
new ThreadSafeDictionary<string, IType>(new Dictionary<string, IType>());
65+
new ConcurrentDictionary<string, IType>();
6566

6667
private static readonly IDictionary<string, GetNullableTypeWithLength> getTypeDelegatesWithLength =
67-
new ThreadSafeDictionary<string, GetNullableTypeWithLength>(new Dictionary<string, GetNullableTypeWithLength>());
68+
new ConcurrentDictionary<string, GetNullableTypeWithLength>();
6869

6970
private static readonly IDictionary<string, GetNullableTypeWithPrecision> getTypeDelegatesWithPrecision =
70-
new ThreadSafeDictionary<string, GetNullableTypeWithPrecision>(new Dictionary<string, GetNullableTypeWithPrecision>());
71+
new ConcurrentDictionary<string, GetNullableTypeWithPrecision>();
7172

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

src/NHibernate/Util/ThreadSafeDictionary.cs

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

0 commit comments

Comments
 (0)