From a4ee2ccb13f78dbde4084bbc0f15a7057a06984a Mon Sep 17 00:00:00 2001 From: Daniel Patterson Date: Mon, 5 Mar 2018 22:45:23 -0800 Subject: [PATCH] Avoid copying json::Value objects a lot when reallocating vector --- CHANGELOG.md | 2 ++ include/engine/api/route_api.hpp | 8 ++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c65757c498..599a069fb42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,8 @@ - CHANGED #4845: Updated segregated intersection identification - Documentation: - ADDED: Add documentation about OSM node ids in nearest service response [#4436](https://github.com/Project-OSRM/osrm-backend/pull/4436) + - Performance + - FIXED: Speed up response time when lots of legs exist and geojson is used with `steps=true` [#4936](https://github.com/Project-OSRM/osrm-backend/pull/4936) # 5.16.0 diff --git a/include/engine/api/route_api.hpp b/include/engine/api/route_api.hpp index 6b5242fc7f7..285bc5dfcd9 100644 --- a/include/engine/api/route_api.hpp +++ b/include/engine/api/route_api.hpp @@ -211,12 +211,16 @@ class RouteAPI : public BaseAPI } std::vector step_geometries; + const auto total_step_count = + std::accumulate(legs.begin(), legs.end(), 0, [](const auto &v, const auto &leg) { + return v + leg.steps.size(); + }); + step_geometries.reserve(total_step_count); + for (const auto idx : util::irange(0UL, legs.size())) { auto &leg_geometry = leg_geometries[idx]; - step_geometries.reserve(step_geometries.size() + legs[idx].steps.size()); - std::transform( legs[idx].steps.begin(), legs[idx].steps.end(),