Skip to content

Improve performance with early exits for near-optimal candidates - #131

Open
cozuya wants to merge 1 commit into
mapbox:masterfrom
cozuya:perf/early-exits-near-optimal-candidates
Open

cozuya wants to merge 1 commit into
mapbox:masterfrom
cozuya:perf/early-exits-near-optimal-candidates

Conversation

@cozuya

@cozuya cozuya commented Nov 14, 2026

Copy link
Copy Markdown

Summary

Adds an early exit when an enclosing-strip bound proves the current candidate is within the requested precision of optimal.

This works on rotated and chamfered polygons too, not just rectangles. If the bound isn't sufficient, the existing search runs unchanged.

Performance

Benchmarked against v2.1.0 (1d9039f) on Node 24.22.1.

Polygon | Before | After | Speedup -- | -- | -- | -- 1,000 × 1 rectangle | 277 ms | 0.0019 ms | 143,253× Rotated 100 × 1 rectangle | 48.9 ms | 0.0015 ms | 33,552× Chamfered 100 × 1 octagon | 18.3 ms | 0.0030 ms | 6,131× Rotated convex dodecagon | 566 ms | 0.0027 ms | 207,418×

13,700× geometric-mean speedup across 12 thin-polygon cases. All 12 complete with just two distance probes and no queued cells.

The 15 control cases were approximately unchanged overall (1.03×), with one L-shaped polygon running 8.7% slower.

Peak memory on the 1,000 × 1 rectangle dropped from 283 MiB to 42 MiB.

Testing

  • Existing tests pass unchanged.

  • Independent geometry checks confirm the returned points and distances.

  • 6,720 adversarial certificate attempts, with no false accepts.

  • No API changes or new dependencies.

The new bounds account for floating-point uncertainty and check all polygon rings, including holes.

@cozuya
cozuya requested a review from a team as a code owner November 14, 2026 23:11
@cozuya
cozuya requested review from dcoj and removed request for a team November 14, 2026 23:11

@mourner mourner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sorry, I don't think an edge case speedup is worth this much added complexity and code, unless you can radically simplify this. Can't land it in this form.

@mourner
mourner removed the request for review from dcoj November 15, 2026 11:11

This branch has not been deployed

No deployments
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