perf: avoid quadratic hole-point iteration in offset generation & triangulation#2211
Open
ben-milanko wants to merge 2 commits into
Open
perf: avoid quadratic hole-point iteration in offset generation & triangulation#2211ben-milanko wants to merge 2 commits into
ben-milanko wants to merge 2 commits into
Conversation
Widget- and kernel-level CPU benchmarks for the polyline, polygon, and
marker layers, plus a direct getOffsetsXY benchmark. Lives in
benchmark/ (not test/) so it is opt-in and does not extend CI runtime:
flutter test benchmark/feature_layer_benchmark_test.dart
Numbers are JIT and only meaningful relative to each other (before vs
after a change on the same machine).
…angulation points.followedBy(holePoints.expand(...)) indexed with per-element elementAt() walks the lazy iterable from the start on every call, making OffsetHelper.getOffsetsXY and the Earcut coordinate flattening O(n^2) in the total point count - on every frame. Flatten into a fixed-length list instead. Benchmark (benchmark/feature_layer_benchmark_test.dart, JIT): getOffsetsXY, 500-point polygon + 10 holes x 200 points: 26,761 us/call -> 33 us/call (~800x)
This was referenced Jun 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
While profiling flutter_map in a production app on low-powered hardware, I found that
OffsetHelper.getOffsetsXYand thePolygonLayerEarcut coordinate flattening index a lazily concatenated iterable (points.followedBy(holePoints.expand(...))) withelementAt(i). On a lazy iterable that walks from the start on every call, so both paths are O(n²) in the total point count — every frame for any polygon with holes.This PR flattens the points + hole points into a fixed-length list before the loops (and writes the Earcut input into a
Float64Listdirectly).Results
Measured with the benchmark harness added in the first commit (
flutter test benchmark/feature_layer_benchmark_test.dart, JIT, best-of-reps):getOffsetsXY, 500-point polygon + 10 holes × 200 pointsAll existing tests pass. No behavioural change intended — output offsets are identical.
The harness commit is shared with #2212 and #2213 (identical file content, so whichever PR merges first, it collapses out of the others' diffs). It lives in
benchmark/rather thantest/so it doesn't extend CI runtime; numbers are only meaningful relative to each other on the same machine.