From 752b440e68a8a67d56b8dc5b43e931525efc058f Mon Sep 17 00:00:00 2001 From: Pasha Date: Sat, 24 Jan 2026 22:26:59 +0000 Subject: [PATCH] Optimised ClassAttributeClass a bit more --- Assets/Pacore/Runtime/ClassAttributeCache.cs | 70 ++++++++------------ 1 file changed, 28 insertions(+), 42 deletions(-) diff --git a/Assets/Pacore/Runtime/ClassAttributeCache.cs b/Assets/Pacore/Runtime/ClassAttributeCache.cs index c1f7191..52c97b2 100644 --- a/Assets/Pacore/Runtime/ClassAttributeCache.cs +++ b/Assets/Pacore/Runtime/ClassAttributeCache.cs @@ -22,52 +22,38 @@ namespace PashaBibko.Pacore [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)] private static void ScanAllAssemblies() { - using (CodeProfiler.Start("Assembly scan")) + /* Fetches all the class types in all loaded assemblies */ + Type[] classes = AppDomain.CurrentDomain.GetAssemblies() // Assembly[] + .SelectMany(assembly => assembly.GetTypes()) // IEnumerable + .Where(type => type.IsClass && !type.IsAbstract) // IEnumerable + .ToArray(); + + /* Allocates space for the cache */ + AttributeCache = classes // Type[] + .Where(type => typeof(Attribute).IsAssignableFrom(type)) // IEnumerable + .ToDictionary + ( + keySelector: t => t, + elementSelector: _ => new List() + ); // Dictionary> + + /* Finds which attributes are attached to what classes */ + HashSet seen = new(); + foreach (Type current in classes) { - /* Fetches all the class types in all loaded assemblies */ - Type[] classes = AppDomain.CurrentDomain.GetAssemblies() // Assembly[] - .SelectMany(assembly => - { - try - { - return assembly.GetTypes(); - } - - catch (ReflectionTypeLoadException err) - { - return err.Types.Where(t => t != null); - } - }) // IEnumerable - .Where(type => type.IsClass && !type.IsAbstract) // IEnumerable - .ToArray(); - - /* Allocates space for the cache */ - AttributeCache = classes // Type[] - .Where(type => typeof(Attribute).IsAssignableFrom(type)) // IEnumerable - .ToDictionary - ( - keySelector: t => t, - elementSelector: _ => new List() - ); // Dictionary> - - /* Finds which attributes are attached to what classes */ - HashSet seen = new(); - foreach (Type current in classes) + /* Tracks the seen attributes */ + seen.Clear(); + foreach (object attr in current.GetCustomAttributes(inherit: true)) { - /* Tracks the seen attributes */ - seen.Clear(); - foreach (object attr in current.GetCustomAttributes(inherit: true)) - { - seen.Add(attr.GetType()); - } + seen.Add(attr.GetType()); + } - /* Adds the class type to each attribute in the dictionary */ - foreach (Type type in seen) - { - AttributeCache[type].Add(current); - } + /* Adds the class type to each attribute in the dictionary */ + foreach (Type type in seen) + { + AttributeCache[type].Add(current); } } } } -} \ No newline at end of file +}