Skip to content

Commit c139a0d

Browse files
committed
Replaced SynchronizedDictionary with ConcurrentDictionary
1 parent 9631074 commit c139a0d

12 files changed

Lines changed: 52 additions & 166 deletions

src/Castle.Core/Components.DictionaryAdapter/DictionaryAdapterFactory.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ namespace Castle.Components.DictionaryAdapter
1616
{
1717
using System;
1818
using System.Collections;
19+
using System.Collections.Concurrent;
1920
using System.Collections.Generic;
2021
using System.Collections.Specialized;
2122
using System.ComponentModel;
@@ -34,8 +35,8 @@ namespace Castle.Components.DictionaryAdapter
3435
/// </summary>
3536
public class DictionaryAdapterFactory : IDictionaryAdapterFactory
3637
{
37-
private readonly SynchronizedDictionary<Type, DictionaryAdapterMeta> interfaceToMeta =
38-
new SynchronizedDictionary<Type, DictionaryAdapterMeta>();
38+
private readonly ConcurrentDictionary<Type, DictionaryAdapterMeta> interfaceToMeta =
39+
new ConcurrentDictionary<Type, DictionaryAdapterMeta>();
3940

4041
#region IDictionaryAdapterFactory
4142

src/Castle.Core/Core/Internal/SynchronizedDictionary.cs

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

src/Castle.Core/DynamicProxy/Contributors/ClassProxyTargetContributor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ private Type GetDelegateType(MetaMethod method, ClassEmitter @class)
176176
GetCacheKeyTypes(method),
177177
null);
178178

179-
return scope.TypeCache.GetOrAddWithoutTakingLock(key, _ =>
179+
return scope.GetOrAddFromCache(key, _ =>
180180
new DelegateTypeGenerator(method, targetType)
181181
.Generate(@class, namingScope)
182182
.BuildType());

src/Castle.Core/DynamicProxy/Contributors/ClassProxyWithTargetTargetContributor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ private Type GetDelegateType(MetaMethod method, ClassEmitter @class)
121121
GetCacheKeyTypes(method),
122122
null);
123123

124-
return scope.TypeCache.GetOrAddWithoutTakingLock(key, _ =>
124+
return scope.GetOrAddFromCache(key, _ =>
125125
new DelegateTypeGenerator(method, targetType)
126126
.Generate(@class, namingScope)
127127
.BuildType());
@@ -136,7 +136,7 @@ private Type GetInvocationType(MetaMethod method, ClassEmitter @class)
136136

137137
// no locking required as we're already within a lock
138138

139-
return scope.TypeCache.GetOrAddWithoutTakingLock(key, _ => BuildInvocationType(method, @class));
139+
return scope.GetOrAddFromCache(key, _ => BuildInvocationType(method, @class));
140140
}
141141

142142
private MethodGenerator IndirectlyCalledMethodGenerator(MetaMethod method, ClassEmitter proxy,

src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyTargetContributor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ private Type GetInvocationType(MetaMethod method, ClassEmitter @class)
8888

8989
// no locking required as we're already within a lock
9090

91-
return scope.TypeCache.GetOrAddWithoutTakingLock(key, _ =>
91+
return scope.GetOrAddFromCache(key, _ =>
9292
new CompositionInvocationTypeGenerator(method.Method.DeclaringType,
9393
method,
9494
method.Method,

src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyWithoutTargetContributor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ private Type GetInvocationType(MetaMethod method, ClassEmitter emitter)
9595

9696
// no locking required as we're already within a lock
9797

98-
return scope.TypeCache.GetOrAddWithoutTakingLock(key, _ =>
98+
return scope.GetOrAddFromCache(key, _ =>
9999
new CompositionInvocationTypeGenerator(methodInfo.DeclaringType,
100100
method,
101101
methodInfo,

src/Castle.Core/DynamicProxy/Contributors/MixinContributor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ private Type GetInvocationType(MetaMethod method, ClassEmitter emitter)
140140

141141
// no locking required as we're already within a lock
142142

143-
return scope.TypeCache.GetOrAddWithoutTakingLock(key, _ =>
143+
return scope.GetOrAddFromCache(key, _ =>
144144
new CompositionInvocationTypeGenerator(method.Method.DeclaringType,
145145
method,
146146
method.Method,

src/Castle.Core/DynamicProxy/Generators/BaseProxyGenerator.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ internal abstract class BaseProxyGenerator
4040
protected readonly Type[] interfaces;
4141
private readonly ModuleScope scope;
4242
private ILogger logger = NullLogger.Instance;
43-
private ProxyGenerationOptions proxyGenerationOptions;
43+
private readonly ProxyGenerationOptions proxyGenerationOptions;
4444

4545
protected BaseProxyGenerator(ModuleScope scope, Type targetType, Type[] interfaces, ProxyGenerationOptions proxyGenerationOptions)
4646
{
@@ -74,9 +74,11 @@ public Type GetProxyType()
7474
{
7575
bool notFoundInTypeCache = false;
7676

77-
var proxyType = Scope.TypeCache.GetOrAdd(GetCacheKey(), cacheKey =>
77+
var cacheKey = GetCacheKey();
78+
var proxyType = Scope.GetOrAddFromCache(cacheKey, key =>
7879
{
7980
notFoundInTypeCache = true;
81+
8082
Logger.DebugFormat("No cached proxy type was found for target type {0}.", targetType.FullName);
8183

8284
EnsureOptionsOverrideEqualsAndGetHashCode();

src/Castle.Core/DynamicProxy/Generators/CacheKey.cs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace Castle.DynamicProxy.Generators
2020
#if FEATURE_SERIALIZATION
2121
[Serializable]
2222
#endif
23-
internal class CacheKey
23+
internal struct CacheKey : IEquatable<CacheKey>
2424
{
2525
private readonly MemberInfo target;
2626
private readonly Type[] interfaces;
@@ -73,37 +73,31 @@ public override int GetHashCode()
7373

7474
public override bool Equals(object obj)
7575
{
76-
if (this == obj)
77-
{
78-
return true;
79-
}
80-
81-
var cacheKey = obj as CacheKey;
82-
if (cacheKey == null)
83-
{
84-
return false;
85-
}
76+
return obj is CacheKey other && Equals(other);
77+
}
8678

87-
if (!Equals(type, cacheKey.type))
79+
public bool Equals(CacheKey other)
80+
{
81+
if (!Equals(type, other.type))
8882
{
8983
return false;
9084
}
91-
if (!Equals(target, cacheKey.target))
85+
if (!Equals(target, other.target))
9286
{
9387
return false;
9488
}
95-
if (interfaces.Length != cacheKey.interfaces.Length)
89+
if (interfaces.Length != other.interfaces.Length)
9690
{
9791
return false;
9892
}
9993
for (var i = 0; i < interfaces.Length; i++)
10094
{
101-
if (!Equals(interfaces[i], cacheKey.interfaces[i]))
95+
if (!Equals(interfaces[i], other.interfaces[i]))
10296
{
10397
return false;
10498
}
10599
}
106-
if (!Equals(options, cacheKey.options))
100+
if (!Equals(options, other.options))
107101
{
108102
return false;
109103
}

src/Castle.Core/DynamicProxy/Internal/InvocationHelper.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,16 @@
1515
namespace Castle.DynamicProxy.Internal
1616
{
1717
using System;
18-
using System.Collections.Generic;
18+
using System.Collections.Concurrent;
1919
using System.Diagnostics;
2020
using System.Reflection;
21-
using System.Threading;
2221

23-
using Castle.Core.Internal;
2422
using Castle.DynamicProxy.Generators;
2523

2624
internal static class InvocationHelper
2725
{
28-
private static readonly SynchronizedDictionary<CacheKey, MethodInfo> cache =
29-
new SynchronizedDictionary<CacheKey, MethodInfo>();
26+
private static readonly ConcurrentDictionary<CacheKey, MethodInfo> cache =
27+
new ConcurrentDictionary<CacheKey, MethodInfo>();
3028

3129
public static MethodInfo GetMethodOnObject(object target, MethodInfo proxiedMethod)
3230
{

0 commit comments

Comments
 (0)