Skip to content

Commit 3be20d6

Browse files
committed
Replaced SynchronizedDictionary with ConcurrentDictionary
1 parent 9631074 commit 3be20d6

15 files changed

Lines changed: 64 additions & 173 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/DefaultProxyBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ private void AssertValidTypeForTarget(Type type, Type target, string paramName)
152152
}
153153
}
154154

155-
private void AssertValidTypes(IEnumerable<Type>? targetTypes, string paramName)
155+
private void AssertValidTypes(Type[]? targetTypes, string paramName)
156156
{
157157
if (targetTypes != null)
158158
{

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,11 @@ private void EnsureValidBaseType(Type type)
271271
"Base type for proxy is null reference. Please set it to System.Object or some other valid type.");
272272
}
273273

274+
if (typeof(object).Equals(type))
275+
{
276+
return; // Skip GetConstructor reflection for System.Object
277+
}
278+
274279
if (!type.IsClass)
275280
{
276281
ThrowInvalidBaseType(type, "it is not a class type");

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();

0 commit comments

Comments
 (0)