Skip to content

Slow startup performance can be improved with TypeCache #90

Description

@lazlo-bonin

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;
        }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions