Content-Length: 474010 | pFad | http://github.com/fleaflet/flutter_map/pull/1805/commits/7ef68dbd69bd26e94c56a0b47e10b3f3688f74d2

D9 perf: pre-project polylines, and improve simplification & culling by ignatz · Pull Request #1805 · fleaflet/flutter_map · GitHub
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: pre-project polylines, and improve simplification & culling #1805

Merged
merged 9 commits into from
Jan 23, 2024
Prev Previous commit
Revert "Made _ProjectedPoly* mutable to reduce GC stress"
This reverts commit 9344c99.
  • Loading branch information
JaffaKetchup committed Jan 23, 2024
commit 7ef68dbd69bd26e94c56a0b47e10b3f3688f74d2
18 changes: 10 additions & 8 deletions lib/src/layer/polygon_layer/polygon_layer.dart
Original file line number Diff line number Diff line change
@@ -135,16 +135,17 @@ class _PolygonLayerState extends State<PolygonLayer> {
return List<_ProjectedPolygon>.generate(
polygons.length,
(i) {
final projectedPolygon = polygons[i];
final holes = projectedPolygon.holePoints;
final polygon = polygons[i];
final holes = polygon.holePoints;

return projectedPolygon
..points = simplifyPoints(
points: projectedPolygon.points,
return _ProjectedPolygon._(
polygon: polygon.polygon,
points: simplifyPoints(
points: polygon.points,
tolerance: tolerance,
highQuality: true,
)
..holePoints = holes == null
),
holePoints: holes == null
? null
: List<List<DoublePoint>>.generate(
holes.length,
@@ -154,7 +155,8 @@ class _PolygonLayerState extends State<PolygonLayer> {
highQuality: true,
),
growable: false,
);
),
);
},
growable: false,
);
66 changes: 37 additions & 29 deletions lib/src/layer/polygon_layer/projected_polygon.dart
Original file line number Diff line number Diff line change
@@ -1,39 +1,47 @@
part of 'polygon_layer.dart';

@immutable
class _ProjectedPolygon {
final Polygon polygon;
final List<DoublePoint> points;
final List<List<DoublePoint>>? holePoints;

// Mutable to reduce GC stress from repetitive allocation
List<DoublePoint> points;
List<List<DoublePoint>>? holePoints;
const _ProjectedPolygon._({
required this.polygon,
required this.points,
this.holePoints,
});

_ProjectedPolygon._fromPolygon(Projection projection, this.polygon)
: points = List<DoublePoint>.generate(
polygon.points.length,
(j) {
final (x, y) = projection.projectXY(polygon.points[j]);
return DoublePoint(x, y);
},
growable: false,
),
holePoints = (() {
final holes = polygon.holePointsList;
if (holes == null) return null;

return List<List<DoublePoint>>.generate(
holes.length,
_ProjectedPolygon._fromPolygon(Projection projection, Polygon polygon)
: this._(
polygon: polygon,
points: List<DoublePoint>.generate(
polygon.points.length,
(j) {
final points = holes[j];
return List<DoublePoint>.generate(
points.length,
(k) {
final (x, y) = projection.projectXY(points[k]);
return DoublePoint(x, y);
},
growable: false,
);
final (x, y) = projection.projectXY(polygon.points[j]);
return DoublePoint(x, y);
},
growable: false,
);
}());
),
holePoints: () {
final holes = polygon.holePointsList;
if (holes == null) return null;

return List<List<DoublePoint>>.generate(
holes.length,
(j) {
final points = holes[j];
return List<DoublePoint>.generate(
points.length,
(k) {
final (x, y) = projection.projectXY(points[k]);
return DoublePoint(x, y);
},
growable: false,
);
},
growable: false,
);
}(),
);
}
10 changes: 6 additions & 4 deletions lib/src/layer/polyline_layer/painter.dart
Original file line number Diff line number Diff line change
@@ -31,8 +31,9 @@ class _PolylinePainter<R extends Object> extends CustomPainter {

for (final projectedPolyline in polylines.reversed) {
final polyline = projectedPolyline.polyline as Polyline<R>;

if (polyline.hitValue == null) continue;
if (polyline.hitValue == null) {
continue;
}

// TODO: For efficiency we'd ideally filter by bounding box here. However
// we'd need to compute an extended bounding box that accounts account for
@@ -133,9 +134,10 @@ class _PolylinePainter<R extends Object> extends CustomPainter {

for (final projectedPolyline in polylines) {
final polyline = projectedPolyline.polyline;

final offsets = getOffsetsXY(camera, origen, projectedPolyline.points);
if (offsets.isEmpty) continue;
if (offsets.isEmpty) {
continue;
}

final hash = polyline.renderHashCode;
if (needsLayerSaving || (lastHash != null && lastHash != hash)) {
34 changes: 21 additions & 13 deletions lib/src/layer/polyline_layer/polyline_layer.dart
Original file line number Diff line number Diff line change
@@ -191,10 +191,10 @@ class _PolylineLayerState<R extends Object> extends State<PolylineLayer<R>> {
} else {
// If we cannot see this segment but have seen previous ones, flush the last polyline fragment.
if (start != -1) {
_culledPolylines.add(
projectedPolyline
..points = projectedPolyline.points.sublist(start, i + 1),
);
_culledPolylines.add(_ProjectedPolyline._(
polyline: polyline,
points: projectedPolyline.points.sublist(start, i + 1),
));

// Reset start.
start = -1;
@@ -208,9 +208,11 @@ class _PolylineLayerState<R extends Object> extends State<PolylineLayer<R>> {
_culledPolylines.add(
start == 0
? projectedPolyline
// Special case: the entire polyline is visible
: (projectedPolyline
..points = projectedPolyline.points.sublist(start)),
: _ProjectedPolyline._(
polyline: polyline,
// Special case: the entire polyline is visible
points: projectedPolyline.points.sublist(start),
),
);
}
}
@@ -231,12 +233,18 @@ class _PolylineLayerState<R extends Object> extends State<PolylineLayer<R>> {

return List<_ProjectedPolyline>.generate(
polylines.length,
(i) => polylines[i]
..points = simplifyPoints(
points: polylines[i].points,
tolerance: tolerance,
highQuality: true,
),
(i) {
final polyline = polylines[i];

return _ProjectedPolyline._(
polyline: polyline.polyline,
points: simplifyPoints(
points: polyline.points,
tolerance: tolerance,
highQuality: true,
),
);
},
growable: false,
);
}
27 changes: 17 additions & 10 deletions lib/src/layer/polyline_layer/projected_polyline.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
part of 'polyline_layer.dart';

@immutable
class _ProjectedPolyline<R extends Object> {
final Polyline<R> polyline;
final List<DoublePoint> points;

// Mutable to reduce GC stress from repetitive allocation
List<DoublePoint> points;
const _ProjectedPolyline._({
required this.polyline,
required this.points,
});

_ProjectedPolyline._fromPolyline(Projection projection, this.polyline)
: points = List<DoublePoint>.generate(
polyline.points.length,
(j) {
final (x, y) = projection.projectXY(polyline.points[j]);
return DoublePoint(x, y);
},
growable: false,
_ProjectedPolyline._fromPolyline(Projection projection, Polyline<R> polyline)
: this._(
polyline: polyline,
points: List<DoublePoint>.generate(
polyline.points.length,
(j) {
final (x, y) = projection.projectXY(polyline.points[j]);
return DoublePoint(x, y);
},
growable: false,
),
);
}








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/fleaflet/flutter_map/pull/1805/commits/7ef68dbd69bd26e94c56a0b47e10b3f3688f74d2

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy