Skip to content

Commit

Permalink
Improve XmlDependencies.IsDependenciesFile performance
Browse files Browse the repository at this point in the history
This simpler string manipulation checks the same logic as before but much faster in the context of a large project. If the OnPostprocessAllAssets callback is invoked multiple times with thousands of files, as can happen with non-trivial Unity projects, the regex call becomes quite expensive.

For profiling data see: googlesamples#601
  • Loading branch information
Christopher Yarbrough committed Feb 9, 2024
1 parent 85fa4e7 commit 8b3b7fb
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 10 deletions.
10 changes: 3 additions & 7 deletions source/AndroidResolver/src/XmlDependencies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,9 @@ internal class XmlDependencies {
/// </summary>
/// <param name="filename"></param>
/// <returns>true if it is a match, false otherwise.</returns>
internal bool IsDependenciesFile(string filename) {
foreach (var regex in fileRegularExpressions) {
if (regex.Match(filename).Success) {
return true;
}
}
return false;
internal static bool IsDependenciesFile(string filename) {
bool isInEditorFolder = filename.Contains("/Editor/") || filename.Contains(@"\Editor\");
return isInEditorFolder && filename.EndsWith("Dependencies.xml");
}

/// <summary>
Expand Down
5 changes: 2 additions & 3 deletions source/AndroidResolverTests/XmlDependenciesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ public class XmlDependenciesTests
[TestCase("Assets/MyEditorCode/SomeDependencies.xml")]
[TestCase("Assets/Editor/")]
[TestCase("Assets/Editor/Dependendencies")]
public void Test1(string path) {
var dependencies = new XmlDependencies();
bool actualResult = dependencies.IsDependenciesFile(path);
public void IsDependenciesFileReturnsExpected(string path) {
bool actualResult = XmlDependencies.IsDependenciesFile(path);

// This logic was part of the previous unoptimized implementation and can act as a test reference.
bool expectedResult = Regex.IsMatch(input: path, pattern: @".*[/\\]Editor[/\\].*Dependencies\.xml$");
Expand Down

0 comments on commit 8b3b7fb

Please sign in to comment.