Unity-Technologies / AutoLOD

Automatic LOD generation + scene optimization
https://blogs.unity3d.com/2018/01/12/unity-labs-autolod-experimenting-with-automatic-performance-improvements/
Other
1.84k stars 215 forks source link

Slow startup performance can be improved with TypeCache #90

Open lazlo-bonin opened 1 year ago

lazlo-bonin commented 1 year ago

AutoLOD has a performance bottleneck on assembly reload due to the following call hierarchy

From there on, AutoLOD iterates over every type of every loaded assembly, which is very time consuming.

This is precisely why Unity introduced the TypeCache API. With it, we can remove the ForEachType and ForEachAssembly routines in AutoLOD (used in 3 places), thus greatly increasing its startup performance.

Here's my reimplementation of ObjectUtils.GetAssignableTypes(), for example:

        static IEnumerable<Type> GetAssignableTypes(Type type, Func<Type, bool> predicate = null)
        {
            var list = new List<Type>();

            foreach (var t in TypeCache.GetTypesDerivedFrom(type))
            {
                if (!t.IsInterface && !t.IsAbstract && (predicate == null || predicate(t))
                    && t.GetCustomAttribute<HideInInspector>() == null)
                    list.Add(t);
            };

            return list;
        }