Skip to content

perf: cache repository root URI to avoid allocations in hot loop#74

Closed
mfogliatto-dev-agent wants to merge 1 commit into
mfogliatto:mainfrom
mfogliatto-dev-agent:fix/issue-67
Closed

perf: cache repository root URI to avoid allocations in hot loop#74
mfogliatto-dev-agent wants to merge 1 commit into
mfogliatto:mainfrom
mfogliatto-dev-agent:fix/issue-67

Conversation

@mfogliatto-dev-agent

Copy link
Copy Markdown

Summary

Cache the repository root Uri in the ProjectPathProvider constructor instead of recomputing it on every GetRelativePath call.

Problem

GetRelativePath() is called in the inner loop of ProjectPathViolationDetector.GetViolationsFrom() — once per reference × matching rule combination. Each call was:

  1. Computing Path.GetFullPath(repositoryRoot) (same every time)
  2. Trimming and appending separator (same every time)
  3. Allocating a new Uri(...) for the repository root (same every time)

For 20 rules × 50 references, that is up to 2,000 redundant Uri allocations, Path.GetFullPath calls, and string operations per project build.

Fix

Moved the repository root normalization (Path.GetFullPath + trim + separator) and Uri construction into the constructor, storing it as a readonly Uri repositoryRootUri field. GetRelativePath now reuses this cached URI.

This eliminates:

  • 1 × Path.GetFullPath call per invocation
  • 1 × string trim + concatenation per invocation
  • 1 × Uri allocation per invocation

The per-call Uri allocation for projectFilePath remains since that varies per call.

Changes

  • src/ReferenceCop/Providers/ProjectPathProvider.cs — field changed from string repositoryRoot to Uri repositoryRootUri; normalization moved to constructor

Testing

All existing tests pass (72/72 non-platform-specific tests pass; 8 Windows-path DataRow tests fail on Linux identically on main).

Fixes #67

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
@mfogliatto-dev-agent mfogliatto-dev-agent closed this by deleting the head repository Jun 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Performance] ProjectPathProvider.GetRelativePath allocates Uri objects in hot loop

1 participant