Skip to content

Commit

Permalink
Replace GetOrAddToCache[Unsafe] methods with TypeCache property
Browse files Browse the repository at this point in the history
  • Loading branch information
stakx committed Jun 23, 2018
1 parent 08380e8 commit 24e57b9
Show file tree
Hide file tree
Showing 8 changed files with 10 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ private Type GetDelegateType(MetaMethod method, ClassEmitter @class, ProxyGenera
ToArray(),
null);

return scope.GetOrAddToCacheUnsafe(key, _ =>
return scope.TypeCache.GetOrAddUnsafe(key, _ =>
new DelegateTypeGenerator(method, targetType)
.Generate(@class, options, namingScope)
.BuildType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private Type GetDelegateType(MetaMethod method, ClassEmitter @class, ProxyGenera
ToArray(),
null);

return scope.GetOrAddToCacheUnsafe(key, _ =>
return scope.TypeCache.GetOrAddUnsafe(key, _ =>
new DelegateTypeGenerator(method, targetType)
.Generate(@class, options, namingScope)
.BuildType());
Expand All @@ -141,7 +141,7 @@ private Type GetInvocationType(MetaMethod method, ClassEmitter @class, ProxyGene

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

return scope.GetOrAddToCacheUnsafe(key, _ => BuildInvocationType(method, @class, options));
return scope.TypeCache.GetOrAddUnsafe(key, _ => BuildInvocationType(method, @class, options));
}

private MethodGenerator IndirectlyCalledMethodGenerator(MetaMethod method, ClassEmitter proxy,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private Type GetInvocationType(MetaMethod method, ClassEmitter emitter, ProxyGen

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

return scope.GetOrAddToCacheUnsafe(key, _ =>
return scope.TypeCache.GetOrAddUnsafe(key, _ =>
new CompositionInvocationTypeGenerator(method.Method.DeclaringType,
method,
method.Method,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private Type GetInvocationType(MetaMethod method, ClassEmitter @class, ProxyGene

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

return scope.GetOrAddToCacheUnsafe(key, _ =>
return scope.TypeCache.GetOrAddUnsafe(key, _ =>
new CompositionInvocationTypeGenerator(method.Method.DeclaringType,
method,
method.Method,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private Type GetInvocationType(MetaMethod method, ClassEmitter emitter, ProxyGen

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

return scope.GetOrAddToCacheUnsafe(key, _ =>
return scope.TypeCache.GetOrAddUnsafe(key, _ =>
new CompositionInvocationTypeGenerator(method.Method.DeclaringType,
method,
method.Method,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ private Type GetInvocationType(MetaMethod method, ClassEmitter emitter, ProxyGen

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

return scope.GetOrAddToCacheUnsafe(key, _ =>
return scope.TypeCache.GetOrAddUnsafe(key, _ =>
new CompositionInvocationTypeGenerator(method.Method.DeclaringType,
method,
method.Method,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ protected Type ObtainProxyType(CacheKey cacheKey, Func<string, INamingScope, Typ
{
bool notFoundInTypeCache = false;

var proxyType = Scope.GetOrAddToCache(cacheKey, _ =>
var proxyType = Scope.TypeCache.GetOrAdd(cacheKey, _ =>
{
notFoundInTypeCache = true;
Logger.DebugFormat("No cached proxy type was found for target type {0}.", targetType.FullName);
Expand Down
40 changes: 2 additions & 38 deletions src/Castle.Core/DynamicProxy/ModuleScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ public Lock Lock
get { return cacheLock; }
}

internal SynchronizedDictionary<CacheKey, Type> TypeCache => typeCache;

/// <summary>
/// Returns a type from this scope's type cache, or null if the key cannot be found.
/// </summary>
Expand All @@ -191,44 +193,6 @@ public void RegisterInCache(CacheKey key, Type type)
typeCache.AddOrUpdateUnsafe(key, type);
}

/// <summary>
/// Returns a type from this scope's type cache, or adds it to the cache if the key cannot be found.
/// </summary>
/// <param name="key">The key to be looked up in the cache.</param>
/// <param name="valueFactory">A function producing the type to be added to the cache.</param>
/// <returns>The type from this scope's type cache matching the key, or null if the key cannot be found</returns>
/// <remarks>
/// This method does not synchronize access to this scope's type cache, i.e. no read/write locks are taken.
/// Only use this method when you know for sure that a method further up in the call stack already holds
/// a write lock. The function must not access this scope's type cache.
/// </remarks>
internal Type GetOrAddToCacheUnsafe(CacheKey key, Func<CacheKey, Type> valueFactory)
{
Debug.Assert(key != null);
Debug.Assert(valueFactory != null);

return typeCache.GetOrAddUnsafe(key, valueFactory);
}

/// <summary>
/// Returns a type from this scope's type cache, or adds it to the cache if the key cannot be found.
/// </summary>
/// <param name="key">The key to be looked up in the cache.</param>
/// <param name="valueFactory">A function producing the type to be added to the cache.</param>
/// <returns>The type from this scope's type cache matching the key, or null if the key cannot be found</returns>
/// <remarks>
/// This method synchronizes accesses to this scope's type cache using read/write locks.
/// If the specified key cannot be found, the factory function is invoked while a write lock is held.
/// The function must not access this scope's type cache.
/// </remarks>
internal Type GetOrAddToCache(CacheKey key, Func<CacheKey, Type> valueFactory)
{
Debug.Assert(key != null);
Debug.Assert(valueFactory != null);

return typeCache.GetOrAdd(key, valueFactory);
}

/// <summary>
/// Gets the key pair used to sign the strong-named assembly generated by this <see cref = "ModuleScope" />.
/// </summary>
Expand Down

0 comments on commit 24e57b9

Please sign in to comment.