Optimize setWaypoints performance (#312)

Loading a track as route with lots of waypoints (simplify tolerance of 0) caused a long pause before even handling the first route request.

This seems to be caused by a repaint for every added marker/layer. Adding them all at once in FeatureGroup.addTo(map) helps (although still added in a loop).
This commit is contained in:
Norbert Renner 2020-07-01 10:35:50 +02:00
parent 4892c2a1db
commit bacf60a3ba
3 changed files with 22 additions and 6 deletions

View file

@ -36,6 +36,7 @@ BR.Routing = L.Routing.extend({
onAdd: function(map) {
this._segmentsCasing = new L.FeatureGroup().addTo(map);
this._loadingTrailerGroup = new L.FeatureGroup().addTo(map);
var container = L.Routing.prototype.onAdd.call(this, map);
@ -276,9 +277,24 @@ BR.Routing = L.Routing.extend({
};
this.fire('routing:setWaypointsStart');
// Workaround to optimize performance.
// Add markers/layers to map "at once" and avoid a repaint for every single one.
// Therefore remove and re-add FeatureGroup from/to map, also need to unset map reference,
// as LayerGroup.addLayer would add to map anyway.
this._waypoints.remove();
this._waypoints._map = null;
this._loadingTrailerGroup.remove();
this._loadingTrailerGroup._map = null;
for (i = 0; latLngs && i < latLngs.length; i++) {
this.addWaypoint(latLngs[i], this._waypoints._last, null, callback);
}
this._loadingTrailerGroup._map = this._map;
this._loadingTrailerGroup.addTo(this._map);
this._waypoints._map = this._map;
this._waypoints.addTo(this._map);
},
// patch to fix error when line is null or error line
@ -313,7 +329,7 @@ BR.Routing = L.Routing.extend({
dashArray: [10, 10],
className: 'loading-trailer'
});
loadingTrailer.addTo(this._map);
this._loadingTrailerGroup.addLayer(loadingTrailer);
}
L.Routing.prototype._routeSegment.call(
@ -322,7 +338,7 @@ BR.Routing = L.Routing.extend({
m2,
L.bind(function(err, data) {
if (loadingTrailer) {
this._map.removeLayer(loadingTrailer);
this._loadingTrailerGroup.removeLayer(loadingTrailer);
}
cb(err, data);
}, this)