From 8b3b7fbada715a655623c35789f1c5c70c988219 Mon Sep 17 00:00:00 2001 From: Christopher Yarbrough Date: Fri, 17 Mar 2023 18:25:33 +0100 Subject: [PATCH] Improve XmlDependencies.IsDependenciesFile performance 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: https://github.com/googlesamples/unity-jar-resolver/issues/601 --- source/AndroidResolver/src/XmlDependencies.cs | 10 +++------- source/AndroidResolverTests/XmlDependenciesTests.cs | 5 ++--- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/source/AndroidResolver/src/XmlDependencies.cs b/source/AndroidResolver/src/XmlDependencies.cs index 24a02594..7621e3b2 100644 --- a/source/AndroidResolver/src/XmlDependencies.cs +++ b/source/AndroidResolver/src/XmlDependencies.cs @@ -46,13 +46,9 @@ internal class XmlDependencies { /// /// /// true if it is a match, false otherwise. - 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"); } /// diff --git a/source/AndroidResolverTests/XmlDependenciesTests.cs b/source/AndroidResolverTests/XmlDependenciesTests.cs index 9495f1b4..998e46cb 100644 --- a/source/AndroidResolverTests/XmlDependenciesTests.cs +++ b/source/AndroidResolverTests/XmlDependenciesTests.cs @@ -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$");