-
-
Notifications
You must be signed in to change notification settings - Fork 835
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1341 from autofac/feature/reflection-cache-clear
Move all reflection caches out of statics to a centrally-stored location
- Loading branch information
Showing
31 changed files
with
1,079 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (c) Autofac Project. All rights reserved. | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
using System.Reflection; | ||
|
||
namespace Autofac.Core; | ||
|
||
/// <summary> | ||
/// Delegate for predicates that can choose whether to remove a member from the | ||
/// reflection cache. | ||
/// </summary> | ||
/// <param name="assembly"> | ||
/// The assembly the cache entry relates to (i.e. the source of a type of | ||
/// member). | ||
/// </param> | ||
/// <param name="member"> | ||
/// The member information (will be an instance of a more-derived type). This | ||
/// value may be null if the cache entry relates only to an assembly. | ||
/// </param> | ||
/// <returns> | ||
/// True to remove the member from the cache, false to leave it. | ||
/// </returns> | ||
public delegate bool ReflectionCacheClearPredicate(Assembly assembly, MemberInfo? member); | ||
|
||
/// <summary> | ||
/// Defines an individual store of cached reflection data. | ||
/// </summary> | ||
public interface IReflectionCache | ||
{ | ||
/// <summary> | ||
/// Gets a value indicating when the cache is used. | ||
/// </summary> | ||
ReflectionCacheUsage Usage { get; } | ||
|
||
/// <summary> | ||
/// Clear the cache. | ||
/// </summary> | ||
void Clear(); | ||
|
||
/// <summary> | ||
/// Conditionally clear the cache, based on the provided predicate. | ||
/// </summary> | ||
/// <param name="predicate">A predicate that returns true for cache entries that should be cleared.</param> | ||
void Clear(ReflectionCacheClearPredicate predicate); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright (c) Autofac Project. All rights reserved. | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
using System.Reflection; | ||
using Autofac.Core.Activators.Reflection; | ||
using Autofac.Util; | ||
using Autofac.Util.Cache; | ||
|
||
namespace Autofac.Core; | ||
|
||
/// <summary> | ||
/// Defines known, internal-only caches that are used in relatively hot paths, so we want to | ||
/// avoid the additional dictionary lookup in <see cref="ReflectionCacheSet.GetOrCreateCache(string)"/>. | ||
/// </summary> | ||
internal class InternalReflectionCaches | ||
{ | ||
/// <summary> | ||
/// Gets the cache used by <see cref="Util.AssemblyExtensions.GetPermittedTypesForAssemblyScanning"/>. | ||
/// </summary> | ||
public ReflectionCacheAssemblyDictionary<Assembly, IEnumerable<Type>> AssemblyScanAllowedTypes { get; } | ||
|
||
/// <summary> | ||
/// Gets the cache used by <see cref="InternalTypeExtensions.IsGenericEnumerableInterfaceType"/>. | ||
/// </summary> | ||
public ReflectionCacheDictionary<Type, bool> IsGenericEnumerableInterface { get; } | ||
|
||
/// <summary> | ||
/// Gets the cache used by <see cref="InternalTypeExtensions.IsGenericListOrCollectionInterfaceType"/>. | ||
/// </summary> | ||
public ReflectionCacheDictionary<Type, bool> IsGenericListOrCollectionInterfaceType { get; } | ||
|
||
/// <summary> | ||
/// Gets the cache used by <see cref="InternalTypeExtensions.IsGenericTypeDefinedBy"/>. | ||
/// </summary> | ||
public ReflectionCacheTupleDictionary<Type, bool> IsGenericTypeDefinedBy { get; } | ||
|
||
/// <summary> | ||
/// Gets the cache used by <see cref="ConstructorBinder"/>. | ||
/// </summary> | ||
public ReflectionCacheDictionary<ConstructorInfo, Func<object?[], object>> ConstructorBinderFactory { get; } | ||
|
||
/// <summary> | ||
/// Gets a cache used by <see cref="AutowiringPropertyInjector.InjectProperties"/>. | ||
/// </summary> | ||
public ReflectionCacheDictionary<PropertyInfo, Action<object, object?>> AutowiringPropertySetters { get; } | ||
|
||
/// <summary> | ||
/// Gets a cache used by <see cref="AutowiringPropertyInjector.InjectProperties"/>. | ||
/// </summary> | ||
public ReflectionCacheDictionary<Type, IReadOnlyList<PropertyInfo>> AutowiringInjectableProperties { get; } | ||
|
||
/// <summary> | ||
/// Gets a cache used by <see cref="DefaultConstructorFinder"/>. | ||
/// </summary> | ||
public ReflectionCacheDictionary<Type, ConstructorInfo[]> DefaultPublicConstructors { get; } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="InternalReflectionCaches"/> class. | ||
/// </summary> | ||
/// <param name="set">The cache set used to retrieve the required caches.</param> | ||
public InternalReflectionCaches(ReflectionCacheSet set) | ||
{ | ||
AssemblyScanAllowedTypes = set.GetOrCreateCache(nameof(AssemblyScanAllowedTypes), _ => new ReflectionCacheAssemblyDictionary<Assembly, IEnumerable<Type>> | ||
{ | ||
Usage = ReflectionCacheUsage.Registration, | ||
}); | ||
|
||
IsGenericEnumerableInterface = set.GetOrCreateCache<ReflectionCacheDictionary<Type, bool>>(nameof(IsGenericEnumerableInterface)); | ||
IsGenericListOrCollectionInterfaceType = set.GetOrCreateCache<ReflectionCacheDictionary<Type, bool>>(nameof(IsGenericListOrCollectionInterfaceType)); | ||
IsGenericTypeDefinedBy = set.GetOrCreateCache<ReflectionCacheTupleDictionary<Type, bool>>(nameof(IsGenericTypeDefinedBy)); | ||
ConstructorBinderFactory = set.GetOrCreateCache<ReflectionCacheDictionary<ConstructorInfo, Func<object?[], object>>>(nameof(ConstructorBinderFactory)); | ||
AutowiringPropertySetters = set.GetOrCreateCache<ReflectionCacheDictionary<PropertyInfo, Action<object, object?>>>(nameof(AutowiringPropertySetters)); | ||
AutowiringInjectableProperties = set.GetOrCreateCache<ReflectionCacheDictionary<Type, IReadOnlyList<PropertyInfo>>>(nameof(AutowiringInjectableProperties)); | ||
DefaultPublicConstructors = set.GetOrCreateCache<ReflectionCacheDictionary<Type, ConstructorInfo[]>>(nameof(DefaultPublicConstructors)); | ||
} | ||
} |
Oops, something went wrong.