Speed up generation of step geometries when there are lots of legs #4936
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.
Issue
While profiling the
match
plugin, I noticed that performance was about 10x worse when returning step geometries in GeoJSON format compared to returningpolyline
orpolyline6
geometries.A little bit of digging revealed that we were resizing the
step_geometries
vector for every leg in the route. For long map-matches, there are lots and lots of legs.Combine this with the fact that
util::json::Value
is quite expensive to copy (we should probably look at that separately), it looks like if you dosteps=true&geometries=geojson
on anymatch
request, the response time is completely dominated by calls to.reserve()
at the link above.This change simply pre-allocates the whole vector before looping over all the legs, avoiding the repeated
.reserve()
calls and all the copies of the data within thestep_geometries
vector.There will be little to no performance change on requests with only 1 leg (i.e. simple
/route
requests).For the 1200 coordinate
/match
request I was testing, this simple change reduces the query time from 7854ms down to 560ms.Tasklist