Skip to content

Optimize AssemblyNameViolationDetector to O(n) time complexity#45

Merged
mfogliatto merged 8 commits intomainfrom
optimize-fix-detectors
Dec 24, 2025
Merged

Optimize AssemblyNameViolationDetector to O(n) time complexity#45
mfogliatto merged 8 commits intomainfrom
optimize-fix-detectors

Conversation

@mfogliatto
Copy link
Copy Markdown
Owner

@mfogliatto mfogliatto commented Dec 24, 2025

Description

This PR improves the time complexity of AssemblyNameViolationDetector from O(m*n) to O(n), where m is the number of rules and n is the number of references. The optimization is achieved by reversing the lookup strategy: instead of iterating over rules for each reference, we now build a dictionary of references and check each rule against it in O(1) time.

Type of change

  • Enhancement (e.g. new feature, functional improvement, etc.)
  • Bug fix
  • Chore (e.g. refactoring, documentation, etc.)

Related Issue

Closes #28

Implementation Details

Core Changes

  • Optimized AssemblyNameViolationDetector: Added a new experimental implementation that builds a dictionary from references for O(1) lookups
  • Configuration Support: Added UseExperimentalDetectors and EnableDebugMessages flags to ReferenceCopConfig to enable the new implementation
  • Backward Compatibility: The original implementation remains available; users can opt-in to the experimental version via config

Performance Improvements

Benchmark Results

Performance testing was conducted using BenchmarkDotNet across various scenarios with different numbers of rules and references:

Scenario Rules References Original (μs) Experimental (μs) Speedup Memory Reduction
Small 10 10 6.513 5.262 1.24x 15% less
Medium 50 100 301.405 134.393 2.24x 2% less
Large 100 500 3,141.434 1,153.985 2.72x ~1% less

Key Findings:

  • Up to 2.72x faster for large codebases (100 rules, 500 references)
  • Consistent speedup ranging from 1.24x to 2.72x across all test scenarios
  • Reduced memory allocations by up to 15% in small scenarios
  • Better scaling characteristics as the number of rules increases

The experimental implementation shows the most significant improvements when dealing with:

  • Large number of rules (50-100+)
  • Moderate to large number of references (100-500+)
  • Scenarios typical of enterprise codebases

How Has This Been Tested?

  • Validated in MSBuild Task integration
  • Validated in Roslyn Analyzer integration
  • Tested using the playground project with real-world scenarios

Screenshots (if applicable)

N/A - Performance improvement, no UI changes

Additional context

  • The implementation maintains full backward compatibility
  • The original algorithm remains the default to ensure stability
  • Users can opt-in to the optimized version
  • Future work may include making the experimental version the default after additional validation
  • Changes include playground project improvements to facilitate e2e testing and validation

@mfogliatto mfogliatto merged commit 149c8a2 into main Dec 24, 2025
2 checks passed
@mfogliatto mfogliatto deleted the optimize-fix-detectors branch December 24, 2025 16:30
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.

Improve time complexity in AssemblyNameViolationDetector, ideally to O(1)

1 participant