Skip to content

Conversation

Copy link

Copilot AI commented Jul 18, 2025

This PR implements a new Roslyn analyzer (GIS001) that detects performance issues with NetTopologySuite's Geometry.Union method when called repeatedly inside loops.

Problem

Calling Geometry.Union() repeatedly in loops has poor performance characteristics:

// ❌ Poor performance - O(n²) complexity in many cases
Geometry result = null;
foreach (var geom in geometries)
{
    result = result.Union(geom);  // GIS001 warning here
}

This pattern is problematic because:

  • Each union operation can be expensive
  • Doing them individually creates quadratic complexity
  • Performance degrades significantly with larger geometry collections

Solution

The analyzer detects this pattern and provides a code fix that replaces it with CascadedPolygonUnion.Union():

// ✅ Much better performance - optimized batch operations
Geometry result = CascadedPolygonUnion.Union(geometries);

Implementation Details

Analyzer Features

  • GIS001: Detects Geometry.Union calls inside for, foreach, and while loops
  • Scope: Only targets NetTopologySuite geometry types (checks namespace and inheritance)
  • Precision: Ignores non-geometry Union calls (like LINQ Enumerable.Union)

Code Fix Provider

  • Automatically replaces loop patterns with CascadedPolygonUnion.Union() calls
  • Adds necessary using NetTopologySuite.Operation.Union; directive
  • Preserves variable declarations and maintains code structure

Testing

  • 6 comprehensive tests covering all loop types and edge cases
  • MSTest framework following repository conventions
  • CSharpCodeFixVerifier for analyzer and code fix validation
  • All existing tests continue to pass (43/43)

Files Changed

  • Optilizer/GeometryUnionInLoopAnalyzer.cs - Main analyzer implementation
  • Optilizer.CodeFixes/GeometryUnionInLoopCodeFixProvider.cs - Code fix provider
  • Optilizer.Test/GeometryUnionInLoopTests.cs - Comprehensive test suite
  • README.md - Updated documentation with GIS001 details and examples

The implementation follows all repository conventions and integrates seamlessly with the existing Optilizer analyzer suite.

This pull request was created as a result of the following prompt from Copilot chat.

Create a new Roslyn analyzer (GIS001) that detects calls to NetTopologySuite's Geometry.Union inside a loop (for, foreach, while) and reports a warning due to performance concerns. Implement a code fixer to replace such code with NetTopologySuite.Operation.Union.CascadedPolygonUnion.Union or the most performant batch-union alternative. Add comprehensive analyzer and code fix tests. Follow repo conventions for structure and testing (MSTest & CSharpCodeFixVerifier).


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 3 commits July 18, 2025 14:52
Co-authored-by: petrovicz <10641425+petrovicz@users.noreply.github.com>
Co-authored-by: petrovicz <10641425+petrovicz@users.noreply.github.com>
Co-authored-by: petrovicz <10641425+petrovicz@users.noreply.github.com>
Copilot AI changed the title [WIP] Analyzer & Fixer: Detect/replace Geometry.Union in loop with CascadedPolygonUnion.Union (GIS001) Implement GIS001 analyzer to detect inefficient Geometry.Union calls in loops Jul 18, 2025
Copilot AI requested a review from petrovicz July 18, 2025 15:04
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.

2 participants