Skip to content

Optimize PossibleIntersection and StablePriorityQueue#16

Merged
JimBobSquarePants merged 3 commits intomainfrom
js/optimize-possible-intersection
Jul 15, 2025
Merged

Optimize PossibleIntersection and StablePriorityQueue#16
JimBobSquarePants merged 3 commits intomainfrom
js/optimize-possible-intersection

Conversation

@JimBobSquarePants
Copy link
Copy Markdown
Member

@JimBobSquarePants JimBobSquarePants commented Jul 15, 2025

A rework of #12 and #13 with added unsafe code!

Note: I've discovered that Clipper2 does not return the correct result for the test data so pay attention to our own results only.

CC @stefannikolei

BenchmarkDotNet v0.14.0, Windows 11 (10.0.26100.4652)
11th Gen Intel Core i7-11370H 3.30GHz, 1 CPU, 8 logical and 4 physical cores
.NET SDK 9.0.302
  [Host]     : .NET 9.0.7 (9.0.725.31616), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI
  DefaultJob : .NET 9.0.7 (9.0.725.31616), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI

Main

Method Mean Error StdDev Ratio RatioSD Gen0 Gen1 Allocated Alloc Ratio
Clipper 48.65 us 0.501 us 0.444 us 2.90 0.03 7.8125 0.9155 47.87 KB 1.50
Clipper2 16.78 us 0.151 us 0.118 us 1.00 0.01 5.1880 0.3357 31.84 KB 1.00

PR (PossibleIntersection)

Method Mean Error StdDev Ratio RatioSD Gen0 Gen1 Allocated Alloc Ratio
PolygonClipper 47.07 us 0.467 us 0.414 us 2.73 0.03 7.6904 0.6714 47.23 KB 1.48
Clipper2 17.25 us 0.160 us 0.150 us 1.00 0.01 5.1880 0.3357 31.84 KB 1.00

PR (StablePriorityQueue)

Method Mean Error StdDev Ratio RatioSD Gen0 Gen1 Allocated Alloc Ratio
PolygonClipper 44.23 us 0.246 us 0.218 us 2.60 0.02 6.7139 0.4883 41.34 KB 1.30
Clipper2 16.99 us 0.157 us 0.139 us 1.00 0.01 5.1880 0.3357 31.84 KB 1.00

This comment was marked as outdated.

@stefannikolei
Copy link
Copy Markdown
Contributor

LGTM.

But whoever designed that Unsafe API... .Add to retrieve a value 🙈🙈

@JimBobSquarePants
Copy link
Copy Markdown
Member Author

LGTM.

But whoever designed that Unsafe API... .Add to retrieve a value 🙈🙈

Haha yeah, it can be clunky!

I just pushed an update for the StablePriorityQueue also. This takes a few percent off the time.

@JimBobSquarePants JimBobSquarePants changed the title Use unsafe and remove allocation in PossibleIntersection Optimize PossibleIntersection and StablePriorityQueue Jul 15, 2025
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR applies performance optimizations by replacing temporary allocations in the sweep‐line intersection logic and the priority queue with a reusable buffer and unsafe memory access, and updates supporting tests and benchmarks to use the new APIs.

  • Introduce a reusable Span<SweepEvent> workspace in PossibleIntersection and switch from list allocations to Unsafe/MemoryMarshal operations.
  • Add a capacity-aware constructor to StablePriorityQueue and refactor its Up/Down methods to use CollectionsMarshal and Unsafe.Add for faster heap adjustments.
  • Update tests, utilities, and benchmarks to call the new overloads and utilize C# 12 collection expressions.

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tests/PolygonClipper.Tests/TestPolygonUtilitiesTests.cs New unit tests for ConvertToPolygon and union operations
tests/PolygonClipper.Tests/TestPolygonUtilities.cs Conversion helpers updated to C# 12 collection expressions
tests/PolygonClipper.Tests/TestData.cs Added null-forgiving operators on deserialization
tests/PolygonClipper.Tests/PolygonClipper.Tests.csproj Added Clipper2 package reference for tests
tests/PolygonClipper.Tests/GenericTestCases.cs Replaced .ToList() with a collection expression for features
tests/PolygonClipper.Benchmarks/PolygonClipper.Benchmarks.csproj Linked updated test utilities and bumped Clipper2 version
tests/PolygonClipper.Benchmarks/ClippingLibraryComparison.cs Benchmarks updated to use TestPolygonUtilities and renamed method
src/PolygonClipper/StablePriorityQueue{T,TComparer}.cs Added capacity constructor; refactored Up/Down to use unsafe spans
src/PolygonClipper/PolygonClipper.cs Added workspace span to PossibleIntersection and updated calls
Comments suppressed due to low confidence (1)

tests/PolygonClipper.Tests/TestPolygonUtilities.cs:89

  • This comment appears in ConvertToClipper2Polygon but the method returns a Clipper2 PathsD, not your internal Polygon type. Update the comment to reflect the actual return type, for example: // Convert GeoJSON Polygon to Clipper2 PathsD.
            // Convert GeoJSON Polygon to our Polygon type

Comment thread tests/PolygonClipper.Tests/TestPolygonUtilities.cs
/// </summary>
/// <param name="index">The index of the item to move upward.</param>
private void Up(int index)
/// <param name="index">The index of the newly added item to sift upward.</param>
Copy link
Copy Markdown
Contributor

@stefannikolei stefannikolei Jul 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo sift --> shift

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, that's intentional, sift is standard terminology in heap operations

@JimBobSquarePants JimBobSquarePants merged commit 5e53144 into main Jul 15, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants