From a0eda3e7d730a0dd3624f8b44b74f6af2ea02f89 Mon Sep 17 00:00:00 2001 From: Siarhei Fedartsou Date: Sat, 15 Jun 2024 18:56:40 +0200 Subject: [PATCH] Apply micro-optimisation for Route API (#6948) --- CHANGELOG.md | 1 + include/engine/api/route_api.hpp | 70 ++++++++++++++++++-------------- 2 files changed, 40 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2bd3782cd..4ed316ac8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ - NodeJS: - CHANGED: Use node-api instead of NAN. [#6452](https://github.com/Project-OSRM/osrm-backend/pull/6452) - Misc: + - CHANGED: Apply micro-optimisation for Route API. [#6948](https://github.com/Project-OSRM/osrm-backend/pull/6948) - CHANGED: Apply micro-optimisation for Match API. [#6945](https://github.com/Project-OSRM/osrm-backend/pull/6945) - CHANGED: Apply micro-optimisation for Nearest API. [#6944](https://github.com/Project-OSRM/osrm-backend/pull/6944) - CHANGED: Avoid copy of intersection in totalTurnAngle. [#6938](https://github.com/Project-OSRM/osrm-backend/pull/6938) diff --git a/include/engine/api/route_api.hpp b/include/engine/api/route_api.hpp index b6907ddca..211be7f8d 100644 --- a/include/engine/api/route_api.hpp +++ b/include/engine/api/route_api.hpp @@ -110,14 +110,14 @@ class RouteAPI : public BaseAPI if (!parameters.skip_waypoints) { - response.values["waypoints"] = BaseAPI::MakeWaypoints(waypoint_candidates); + response.values.emplace("waypoints", BaseAPI::MakeWaypoints(waypoint_candidates)); } - response.values["routes"] = std::move(jsRoutes); - response.values["code"] = "Ok"; + response.values.emplace("routes", std::move(jsRoutes)); + response.values.emplace("code", "Ok"); auto data_timestamp = facade.GetTimestamp(); if (!data_timestamp.empty()) { - response.values["data_version"] = data_timestamp; + response.values.emplace("data_version", data_timestamp); } } @@ -784,49 +784,57 @@ class RouteAPI : public BaseAPI if (requested_annotations & RouteParameters::AnnotationsType::Speed) { double prev_speed = 0; - annotation.values["speed"] = GetAnnotations( - leg_geometry, - [&prev_speed](const guidance::LegGeometry::Annotation &anno) - { - if (anno.duration < std::numeric_limits::min()) - { - return prev_speed; - } - else - { - auto speed = std::round(anno.distance / anno.duration * 10.) / 10.; - prev_speed = speed; - return util::json::clamp_float(speed); - } - }); + annotation.values.emplace( + "speed", + GetAnnotations(leg_geometry, + [&prev_speed](const guidance::LegGeometry::Annotation &anno) + { + if (anno.duration < std::numeric_limits::min()) + { + return prev_speed; + } + else + { + auto speed = + std::round(anno.distance / anno.duration * 10.) / + 10.; + prev_speed = speed; + return util::json::clamp_float(speed); + } + })); } if (requested_annotations & RouteParameters::AnnotationsType::Duration) { - annotation.values["duration"] = + annotation.values.emplace( + "duration", GetAnnotations(leg_geometry, [](const guidance::LegGeometry::Annotation &anno) - { return anno.duration; }); + { return anno.duration; })); } if (requested_annotations & RouteParameters::AnnotationsType::Distance) { - annotation.values["distance"] = + annotation.values.emplace( + "distance", GetAnnotations(leg_geometry, [](const guidance::LegGeometry::Annotation &anno) - { return anno.distance; }); + { return anno.distance; })); } if (requested_annotations & RouteParameters::AnnotationsType::Weight) { - annotation.values["weight"] = GetAnnotations( - leg_geometry, - [](const guidance::LegGeometry::Annotation &anno) { return anno.weight; }); + annotation.values.emplace( + "weight", + GetAnnotations(leg_geometry, + [](const guidance::LegGeometry::Annotation &anno) + { return anno.weight; })); } if (requested_annotations & RouteParameters::AnnotationsType::Datasources) { - annotation.values["datasources"] = + annotation.values.emplace( + "datasources", GetAnnotations(leg_geometry, [](const guidance::LegGeometry::Annotation &anno) - { return anno.datasource; }); + { return anno.datasource; })); } if (requested_annotations & RouteParameters::AnnotationsType::Nodes) { @@ -837,7 +845,7 @@ class RouteAPI : public BaseAPI nodes.values.push_back( static_cast(facade.GetOSMNodeIDOfNode(node_id))); } - annotation.values["nodes"] = std::move(nodes); + annotation.values.emplace("nodes", std::move(nodes)); } // Add any supporting metadata, if needed if (requested_annotations & RouteParameters::AnnotationsType::Datasources) @@ -853,8 +861,8 @@ class RouteAPI : public BaseAPI break; datasource_names.values.push_back(std::string(facade.GetDatasourceName(i))); } - metadata.values["datasource_names"] = datasource_names; - annotation.values["metadata"] = metadata; + metadata.values.emplace("datasource_names", datasource_names); + annotation.values.emplace("metadata", metadata); } annotations.push_back(std::move(annotation));