Skip to content

Commit

Permalink
Made _ProjectedPoly* mutable to reduce GC stress
Browse files Browse the repository at this point in the history
Minor syntactic/formatting improvements
  • Loading branch information
JaffaKetchup committed Jan 23, 2024
1 parent 70a56b8 commit 9344c99
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 91 deletions.
18 changes: 8 additions & 10 deletions lib/src/layer/polygon_layer/polygon_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,16 @@ class _PolygonLayerState extends State<PolygonLayer> {
return List<_ProjectedPolygon>.generate(
polygons.length,
(i) {
final polygon = polygons[i];
final holes = polygon.holePoints;
final projectedPolygon = polygons[i];
final holes = projectedPolygon.holePoints;

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

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

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

_ProjectedPolygon._fromPolygon(Projection projection, Polygon polygon)
: this._(
polygon: polygon,
points: List<DoublePoint>.generate(
polygon.points.length,
_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,
(j) {
final (x, y) = projection.projectXY(polygon.points[j]);
return DoublePoint(x, y);
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,
),
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: 4 additions & 6 deletions lib/src/layer/polyline_layer/painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ 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
Expand Down Expand Up @@ -134,10 +133,9 @@ class _PolylinePainter<R extends Object> extends CustomPainter {

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

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

final hash = polyline.renderHashCode;
if (needsLayerSaving || (lastHash != null && lastHash != hash)) {
Expand Down
34 changes: 13 additions & 21 deletions lib/src/layer/polyline_layer/polyline_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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._(
polyline: polyline,
points: projectedPolyline.points.sublist(start, i + 1),
));
_culledPolylines.add(
projectedPolyline
..points = projectedPolyline.points.sublist(start, i + 1),
);

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

return List<_ProjectedPolyline>.generate(
polylines.length,
(i) {
final polyline = polylines[i];

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

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

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

_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,
),
_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,
);
}

0 comments on commit 9344c99

Please sign in to comment.