From 37af113fcf3571814e974467e7be80283553f685 Mon Sep 17 00:00:00 2001 From: Marius Ungureanu Date: Thu, 22 Sep 2016 15:23:47 +0300 Subject: [Core] FilePath no longer creates extra strings on IsChildPathof. Rewrote the algorithm so it creates fewer strings. The only do the check when we get a match on the first path, so it shouldn't slow down the code a lot. On 10,000,000 iterations, the benchmark results are as given: New with slash: 1331ms Old with slash: 1311ms New without slash: 1397ms Old without slash: 3088ms This fixes memory pressure, slightly makes the slash variant slower, but greatly improved the variant without slash. --- .../src/core/MonoDevelop.Core/MonoDevelop.Core/FilePath.cs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'main') diff --git a/main/src/core/MonoDevelop.Core/MonoDevelop.Core/FilePath.cs b/main/src/core/MonoDevelop.Core/MonoDevelop.Core/FilePath.cs index 0f8f88efd2..f5a55cf727 100644 --- a/main/src/core/MonoDevelop.Core/MonoDevelop.Core/FilePath.cs +++ b/main/src/core/MonoDevelop.Core/MonoDevelop.Core/FilePath.cs @@ -172,11 +172,15 @@ namespace MonoDevelop.Core } public bool IsChildPathOf (FilePath basePath) - { - if (basePath.fileName [basePath.fileName.Length - 1] != Path.DirectorySeparatorChar) - return fileName.StartsWith (basePath.fileName + Path.DirectorySeparatorChar, PathComparison); - else - return fileName.StartsWith (basePath.fileName, PathComparison); + { + bool startsWith = fileName.StartsWith (basePath.fileName, PathComparison); + if (startsWith && basePath.fileName [basePath.fileName.Length - 1] != Path.DirectorySeparatorChar) { + // If the last character isn't a path separator character, check whether the string we're searching in + // has more characters than the string we're looking for then check the character. + if (fileName.Length > basePath.fileName.Length) + startsWith &= fileName [basePath.fileName.Length] == Path.DirectorySeparatorChar; + } + return startsWith; } public FilePath ChangeExtension (string ext) -- cgit v1.2.3