Skip to content
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

Slow startup performance can be improved with TypeCache #90

Open
lazlo-bonin opened this issue Jan 11, 2023 · 0 comments
Open

Slow startup performance can be improved with TypeCache #90

lazlo-bonin opened this issue Jan 11, 2023 · 0 comments

Comments

@lazlo-bonin
Copy link

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

  • AutoLOD..ctor() (Called because it has [InitializeOnLoad])
  • AutoLOD.UpdateDependencies()
  • AutoLOD.get_meshSimplifierType()
  • AutoLOD.get_meshSimplifiers()
  • ObjectUtils.GetImplementationsOfInterface()
  • ObjectUtils.GetAssignableTypes()

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;
        }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant