Skip to content

Commit

Permalink
Optimize path manipulations (Microsoft.IO.Redist)
Browse files Browse the repository at this point in the history
  • Loading branch information
ladipro committed Oct 15, 2021
1 parent 89f7cc6 commit e800fa8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
7 changes: 5 additions & 2 deletions src/Build/FileSystem/DirectoryCacheFileSystemWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
using System.IO;
using System.Linq;

#if FEATURE_MSIOREDIST
using Path = Microsoft.IO.Path;
#endif

namespace Microsoft.Build.FileSystem
{
internal class DirectoryCacheFileSystemWrapper : IFileSystem
Expand Down Expand Up @@ -81,8 +85,7 @@ private IEnumerable<string> EnumerateFullFileSystemPaths(string path, string sea
{
return FileMatcher.IsAllFilesWildcard(searchPattern) || FileMatcher.IsMatch(fileName, searchPattern);
};
// TODO: Don't create a new string and use Path.Join when it becomes available.
FindTransform<string> transform = (ref ReadOnlySpan<char> fileName) => Path.Combine(path, fileName.ToString());
FindTransform<string> transform = (ref ReadOnlySpan<char> fileName) => Path.Join(path.AsSpan(), fileName);

IEnumerable<string> directories = includeDirectories
? _directoryCache.EnumerateDirectories(path, searchPattern, predicate, transform)
Expand Down
35 changes: 28 additions & 7 deletions src/Shared/FileMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ internal FileMatcher(IFileSystem fileSystem, GetFileSystemEntries getFileSystemE
directory,
false));
IEnumerable<string> filteredEntriesForPath = (pattern != null && !IsAllFilesWildcard(pattern))
? allEntriesForPath.Where(o => IsMatch(Path.GetFileName(o), pattern))
? allEntriesForPath.Where(o => IsFileNameMatch(o, pattern))
: allEntriesForPath;
return stripProjectDirectory
? RemoveProjectDirectory(filteredEntriesForPath, directory).ToArray()
Expand Down Expand Up @@ -268,7 +268,7 @@ private static IReadOnlyList<string> GetAccessibleFilesAndDirectories(IFileSyste
{
return (ShouldEnforceMatching(pattern)
? fileSystem.EnumerateFileSystemEntries(path, pattern)
.Where(o => IsMatch(Path.GetFileName(o), pattern))
.Where(o => IsFileNameMatch(o, pattern))
: fileSystem.EnumerateFileSystemEntries(path, pattern)
).ToArray();
}
Expand Down Expand Up @@ -351,7 +351,7 @@ bool stripProjectDirectory
files = fileSystem.EnumerateFiles(dir, filespec);
if (ShouldEnforceMatching(filespec))
{
files = files.Where(o => IsMatch(Path.GetFileName(o), filespec));
files = files.Where(o => IsFileNameMatch(o, filespec));
}
}
// If the Item is based on a relative path we need to strip
Expand Down Expand Up @@ -414,7 +414,7 @@ string pattern
directories = fileSystem.EnumerateDirectories((path.Length == 0) ? s_thisDirectory : path, pattern);
if (ShouldEnforceMatching(pattern))
{
directories = directories.Where(o => IsMatch(Path.GetFileName(o), pattern));
directories = directories.Where(o => IsFileNameMatch(o, pattern));
}
}

Expand Down Expand Up @@ -956,7 +956,7 @@ private void GetFilesRecursive(
for (int i = 0; i < excludeNextSteps.Length; i++)
{
if (excludeNextSteps[i].NeedsDirectoryRecursion &&
(excludeNextSteps[i].DirectoryPattern == null || IsMatch(Path.GetFileName(subdir), excludeNextSteps[i].DirectoryPattern)))
(excludeNextSteps[i].DirectoryPattern == null || IsFileNameMatch(subdir, excludeNextSteps[i].DirectoryPattern)))
{
RecursionState thisExcludeStep = searchesToExclude[i];
thisExcludeStep.BaseDirectory = subdir;
Expand Down Expand Up @@ -1097,7 +1097,7 @@ private static bool MatchFileRecursionStep(RecursionState recursionState, string
}
else if (recursionState.SearchData.Filespec != null)
{
return IsMatch(Path.GetFileName(file), recursionState.SearchData.Filespec);
return IsFileNameMatch(file, recursionState.SearchData.Filespec);
}

// if no file-spec provided, match the file to the regular expression
Expand Down Expand Up @@ -1664,7 +1664,28 @@ internal Result()
internal string wildcardDirectoryPart = string.Empty;
}

// TODO: Remove this overload and fix callers to pass ReadOnlySpan<char>.
/// <summary>
/// A wildcard (* and ?) matching algorithm that tests whether the input path file name matches against the pattern.
/// </summary>
/// <param name="path">The path whose file name is matched against the pattern.</param>
/// <param name="pattern">The pattern.</param>
internal static bool IsFileNameMatch(string path, string pattern)
{
// Use a span-based Path.GetFileName if it is available.
#if FEATURE_MSIOREDIST
return IsMatch(Microsoft.IO.Path.GetFileName(path.AsSpan()), pattern);
#elif NETSTANDARD2_0
return IsMatch(Path.GetFileName(path), pattern);
#else
return IsMatch(Path.GetFileName(path.AsSpan()), pattern);
#endif
}

/// <summary>
/// A wildcard (* and ?) matching algorithm that tests whether the input string matches against the pattern.
/// </summary>
/// <param name="input">String which is matched against the pattern.</param>
/// <param name="pattern">Pattern against which string is matched.</param>
internal static bool IsMatch(string input, string pattern)
{
return IsMatch(input.AsSpan(), pattern);
Expand Down

0 comments on commit e800fa8

Please sign in to comment.