Skip to content

[FR] Optimize XmlDependencies.IsDependenciesFile for Performance #601

Open
@chrisyarbrough

Description

@chrisyarbrough

Feature proposal

  • EDM4U Component: Android Resolver, iOS Resolver

I've observed a significant performance slowdown in the Unity Editor when using Android Resolver and iOS Resolver in a large project with numerous assets:

XmlDependencies_IsDependenciesFile_Performance

As seen in the profiler screenshot, the XmlDependencies.IsDependenciesFile method takes 30 seconds to execute in my current project setup. While reducing the calls to this method by refreshing the Unity AssetDatabase less frequently could be a solution, it may not always be feasible due to certain project limitations. Instead, optimizing this method seems more practical.

The performance bottleneck appears to be the regex pattern in the following code snippet:

internal HashSet<Regex> fileRegularExpressions = new HashSet<Regex> {
    new Regex(@".*[/\\]Editor[/\\].*Dependencies\.xml$")
};

internal bool IsDependenciesFile(string filename) {
    foreach (var regex in fileRegularExpressions) {
        if (regex.Match(filename).Success) {
            return true;
        }
    }
    return false;
}

A possible optimization involves replacing the regex pattern with simpler string manipulation methods like this example (untested):

internal bool IsDependenciesFile(string filename) {
    const string EditorFolder = "/Editor/";
    const string DependenciesFileSuffix = "Dependencies.xml";
    int editorIndex = filename.IndexOf(EditorFolder, StringComparison.OrdinalIgnoreCase);
    
    return editorIndex >= 0 && filename.EndsWith(DependenciesFileSuffix, StringComparison.OrdinalIgnoreCase);
}

However, PlayServicesResolver also uses fileRegularExpressions to add additional patterns. I'm unsure how this can be easily accommodated, but I propose the following potential improvements:

  • Replace PlayServicesResolver patterns with simpler string methods, if possible
  • Change fileRegularExpressions from a collection of Regex objects to a delegate or filter object that can be either a Regex or a simpler string manipulation
  • Examine the regex pattern and options for possible enhancements

I would be happy to investigate this issue myself and test changes in my project. Any feedback is greatly appreciated!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions