-
Notifications
You must be signed in to change notification settings - Fork 31
Materialize trigger collections to eliminate ConcatIterator CPU waste #215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| using System; | ||
| using System.Collections.Concurrent; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Reflection; | ||
| using EntityFrameworkCore.Triggered.Lifecycles; | ||
|
|
||
| namespace EntityFrameworkCore.Triggered.Extensions | ||
| { | ||
| /// <summary> | ||
| /// Internal helpers shared between <c>ServiceCollectionExtensions</c> and | ||
| /// <c>TriggersContextOptionsBuilderExtensions</c> for trigger-type discovery. | ||
| /// </summary> | ||
| static internal class TriggerTypeHelper | ||
| { | ||
| // Open generic trigger interfaces — add new generic lifecycle interfaces here | ||
| private readonly static HashSet<Type> _genericTriggerTypes = new HashSet<Type> | ||
| { | ||
| typeof(IBeforeSaveTrigger<>), | ||
| typeof(IBeforeSaveAsyncTrigger<>), | ||
| typeof(IAfterSaveTrigger<>), | ||
| typeof(IAfterSaveAsyncTrigger<>), | ||
| typeof(IAfterSaveFailedTrigger<>), | ||
| typeof(IAfterSaveFailedAsyncTrigger<>), | ||
| }; | ||
|
|
||
| // Non-generic lifecycle interfaces — add new non-generic lifecycle interfaces here | ||
| private readonly static HashSet<Type> _nonGenericTriggerTypes = new HashSet<Type> | ||
| { | ||
| typeof(IBeforeSaveStartingTrigger), | ||
| typeof(IBeforeSaveStartingAsyncTrigger), | ||
| typeof(IBeforeSaveCompletedTrigger), | ||
| typeof(IBeforeSaveCompletedAsyncTrigger), | ||
| typeof(IAfterSaveFailedStartingTrigger), | ||
| typeof(IAfterSaveFailedStartingAsyncTrigger), | ||
| typeof(IAfterSaveFailedCompletedTrigger), | ||
| typeof(IAfterSaveFailedCompletedAsyncTrigger), | ||
| typeof(IAfterSaveStartingTrigger), | ||
| typeof(IAfterSaveStartingAsyncTrigger), | ||
| typeof(IAfterSaveCompletedTrigger), | ||
| typeof(IAfterSaveCompletedAsyncTrigger), | ||
| }; | ||
|
|
||
| // Caches the resolved trigger interfaces per implementation type to avoid repeated reflection | ||
| private readonly static ConcurrentDictionary<Type, Type[]> _triggerInterfaceCache = | ||
| new ConcurrentDictionary<Type, Type[]>(); | ||
|
|
||
| /// <summary> | ||
| /// Returns the types defined in <paramref name="assembly"/>, gracefully handling | ||
| /// <see cref="ReflectionTypeLoadException"/> that occurs when some types cannot be | ||
| /// loaded due to missing dependencies (e.g. optional assemblies not present at runtime). | ||
| /// </summary> | ||
| private static IEnumerable<Type> GetAssemblyTypes(Assembly assembly) | ||
| { | ||
| try | ||
| { | ||
| return assembly.GetTypes(); | ||
| } | ||
| catch (ReflectionTypeLoadException e) | ||
| { | ||
| // Return only the types that could be loaded; nulls represent types that failed | ||
| return e.Types.OfType<Type>(); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns the non-abstract classes defined in <paramref name="assembly"/>, gracefully handling | ||
| /// <see cref="ReflectionTypeLoadException"/> that occurs when some types cannot be loaded due to missing | ||
| /// dependencies (e.g. optional assemblies not present at runtime). | ||
| /// </summary> | ||
| static internal IEnumerable<Type> GetAssemblyConcreteClasses(Assembly assembly) => | ||
| GetAssemblyTypes(assembly).Where(t => t is { IsClass: true, IsAbstract: false }); | ||
|
|
||
| /// <summary> | ||
| /// Returns the subset of <paramref name="triggerImplementationType"/>'s interfaces that | ||
| /// match a known trigger interface, using a per-type cache. | ||
| /// </summary> | ||
| static internal Type[] GetTriggerInterfaces(Type triggerImplementationType) | ||
| => _triggerInterfaceCache.GetOrAdd(triggerImplementationType, t => | ||
| { | ||
| var interfaces = t.GetInterfaces(); | ||
| var result = new List<Type>(interfaces.Length); | ||
|
|
||
| foreach (var iface in interfaces) | ||
| { | ||
| if (iface.IsConstructedGenericType) | ||
| { | ||
| if (_genericTriggerTypes.Contains(iface.GetGenericTypeDefinition())) | ||
| { | ||
| result.Add(iface); | ||
| } | ||
| } | ||
| else if (_nonGenericTriggerTypes.Contains(iface)) | ||
| { | ||
| result.Add(iface); | ||
| } | ||
| } | ||
|
|
||
| return result.ToArray(); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,6 +1,7 @@ | ||||||||||||||
| using System; | ||||||||||||||
| using System.Linq; | ||||||||||||||
| using System.Reflection; | ||||||||||||||
| using EntityFrameworkCore.Triggered.Extensions; | ||||||||||||||
| using EntityFrameworkCore.Triggered.Infrastructure; | ||||||||||||||
| using Microsoft.Extensions.DependencyInjection; | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -24,14 +25,20 @@ public static TriggersContextOptionsBuilder AddAssemblyTriggers(this TriggersCon | |||||||||||||
| throw new ArgumentNullException(nameof(assemblies)); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| var assemblyTypes = assemblies | ||||||||||||||
| .SelectMany(x => x.GetTypes()) | ||||||||||||||
| .Where(x => x.IsClass) | ||||||||||||||
| .Where(x => !x.IsAbstract); | ||||||||||||||
| if (assemblies.Length == 0) | ||||||||||||||
| { | ||||||||||||||
| return builder; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| var assemblyTypes = assemblies.SelectMany(TriggerTypeHelper.GetAssemblyConcreteClasses); | ||||||||||||||
|
|
||||||||||||||
| foreach (var assemblyType in assemblyTypes) | ||||||||||||||
| { | ||||||||||||||
| builder.AddTrigger(assemblyType, lifetime); | ||||||||||||||
| // Only register types that actually implement a known trigger interface | ||||||||||||||
| if (TriggerTypeHelper.GetTriggerInterfaces(assemblyType).Length > 0) | ||||||||||||||
| { | ||||||||||||||
| builder.AddTrigger(assemblyType, lifetime); | ||||||||||||||
| } | ||||||||||||||
|
Comment on lines
+37
to
+41
|
||||||||||||||
| // Only register types that actually implement a known trigger interface | |
| if (TriggerTypeHelper.GetTriggerInterfaces(assemblyType).Length > 0) | |
| { | |
| builder.AddTrigger(assemblyType, lifetime); | |
| } | |
| builder.AddTrigger(assemblyType, lifetime); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TriggerTypeHelper’s interface sets are hard-coded to the base save-change lifecycles. If a consumer adds additional lifecycle interfaces via options (e.g., EntityFrameworkCore.Triggered.Transactions lifecycles or other custom lifecycles), GetTriggerInterfaces will return an empty array and assembly scanning helpers will not register those triggers. Consider making the “known trigger types” list extensible (e.g., allow registering additional interface types) or avoid using this helper for options-builder scanning where the valid trigger-type list is configurable.