perf: cache repository root URI to avoid allocations in hot loop#74
Closed
mfogliatto-dev-agent wants to merge 1 commit into
Closed
perf: cache repository root URI to avoid allocations in hot loop#74mfogliatto-dev-agent wants to merge 1 commit into
mfogliatto-dev-agent wants to merge 1 commit into
Conversation
Move Path.GetFullPath, string normalization, and Uri construction for the repository root into the constructor so it runs once per provider instance instead of once per GetRelativePath call. This eliminates one Uri allocation, one Path.GetFullPath call, and the associated string trimming/concatenation from every inner-loop iteration in ProjectPathViolationDetector.GetViolationsFrom. Fixes #67
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.
Summary
Cache the repository root
Uriin theProjectPathProviderconstructor instead of recomputing it on everyGetRelativePathcall.Problem
GetRelativePath()is called in the inner loop ofProjectPathViolationDetector.GetViolationsFrom()— once per reference × matching rule combination. Each call was:Path.GetFullPath(repositoryRoot)(same every time)new Uri(...)for the repository root (same every time)For 20 rules × 50 references, that is up to 2,000 redundant
Uriallocations,Path.GetFullPathcalls, and string operations per project build.Fix
Moved the repository root normalization (
Path.GetFullPath+ trim + separator) andUriconstruction into the constructor, storing it as areadonly Uri repositoryRootUrifield.GetRelativePathnow reuses this cached URI.This eliminates:
Path.GetFullPathcall per invocationUriallocation per invocationThe per-call
Uriallocation forprojectFilePathremains since that varies per call.Changes
src/ReferenceCop/Providers/ProjectPathProvider.cs— field changed fromstring repositoryRoottoUri repositoryRootUri; normalization moved to constructorTesting
All existing tests pass (72/72 non-platform-specific tests pass; 8 Windows-path DataRow tests fail on Linux identically on
main).Fixes #67