Refactor FrameworkFileUtilities for better performance#13143
Merged
AR-May merged 7 commits intodotnet:mainfrom Feb 6, 2026
Merged
Refactor FrameworkFileUtilities for better performance#13143AR-May merged 7 commits intodotnet:mainfrom
AR-May merged 7 commits intodotnet:mainfrom
Conversation
Member
Author
|
Perf numbers (V2 is the new implementation), measured on butch of paths:
|
Contributor
There was a problem hiding this comment.
Pull request overview
Purpose: This PR refactors the FrameworkFileUtilities class to improve performance by avoiding unnecessary struct allocations. The refactoring adds early-return optimizations to AbsolutePath overload methods that check if modifications are needed before creating new instances.
Changes:
- Added helper methods to detect when path normalization is needed (
HasWindowsDirectorySeparatorOnUnix,HasUnixDirectorySeparatorOnWindows,HasRelativeSegment) - Added early-return optimizations to
AbsolutePathoverload methods to avoid creating new instances when no changes are needed - Removed the
EndsWithSlashmethod and inlined its logic where used within the file
Comments suppressed due to low confidence (2)
src/Framework/FileUtilities.cs:66
- The
EndsWithSlashmethod is removed but there are still multiple external references toFrameworkFileUtilities.EndsWithSlashthroughout the codebase. This will cause compilation failures. References exist in:
src/Build/Definition/Toolset.cs(line 369)src/Shared/FileUtilities.cs(lines 694, 789)src/Shared/Modifiers.cs(line 217)src/Utilities/TrackedDependencies/FileTracker.cs(line 618)src/Shared/UnitTests/FileUtilities_Tests.cs(multiple lines)src/Shared/UnitTests/FileMatcher_Tests.cs(line 2302)
Either restore the method or update all external references to use the inlined equivalent (path.Length > 0 && IsSlash(path[path.Length - 1])).
/// <summary>
/// Fixes backslashes to forward slashes on Unix. This allows to recognise windows style paths on Unix.
/// However, this leads to incorrect path on Linux if backslash was part of the file/directory name.
/// </summary>
internal static string FixFilePath(string path)
{
return string.IsNullOrEmpty(path) || Path.DirectorySeparatorChar == WindowsDirectorySeparator ? path : path.Replace(WindowsDirectorySeparator, UnixDirectorySeparator);
}
/// <summary>
/// If the given path doesn't have a trailing slash then add one.
/// If the path is an empty string, does not modify it.
/// </summary>
src/Framework/FileUtilities.cs:121
- The new optimization logic in the
AbsolutePathoverloads (specifically the early return paths that avoid allocating new instances) lacks test coverage. Consider adding tests to verify:
- That the method returns the same instance when no modification is needed
- That the method creates a new instance when modification is needed
- Edge cases like empty paths, paths with/without trailing slashes, and paths with different separator types on Unix vs Windows
These optimizations are the core value of this PR, so they should be thoroughly tested.
internal static AbsolutePath EnsureTrailingSlash(AbsolutePath path)
{
if (string.IsNullOrEmpty(path.Value))
{
return path;
}
// Check if already has trailing slash and no separator fixing needed on unix
// (EnsureTrailingSlash also should fix the paths on unix).
if (IsSlash(path.Value[path.Value.Length - 1]) && !HasWindowsDirectorySeparatorOnUnix(path.Value))
{
return path;
}
return new AbsolutePath(EnsureTrailingSlash(path.Value),
original: path.OriginalValue,
ignoreRootedCheck: true);
}
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
JanProvaznik
reviewed
Feb 5, 2026
JanProvaznik
approved these changes
Feb 6, 2026
JanProvaznik
pushed a commit
to JanProvaznik/msbuild
that referenced
this pull request
Feb 25, 2026
### Context Refactoring the AbsoluteClass functions in FrameworkFileUtilities. Avoid to create a new struct when no changes needed. --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
Refactoring the AbsoluteClass functions in FrameworkFileUtilities. Avoid to create a new struct when no changes needed.