From 7652f6ca6bcd8e21e9768a0064efc9debedb7889 Mon Sep 17 00:00:00 2001 From: Siarhei Fedartsou Date: Fri, 14 Jun 2024 16:04:19 +0200 Subject: [PATCH] Apply micro-optimisation for Match API (#6945) --- CHANGELOG.md | 1 + include/engine/api/match_api.hpp | 34 ++++++++++++++++---------------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2edfeded4..2bd3782cd 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 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) - CHANGED: Use std::unordered_map::emplace instead of operator[] when producing JSONs. [#6936](https://github.com/Project-OSRM/osrm-backend/pull/6936) diff --git a/include/engine/api/match_api.hpp b/include/engine/api/match_api.hpp index 012c01176..d7956bba4 100644 --- a/include/engine/api/match_api.hpp +++ b/include/engine/api/match_api.hpp @@ -77,19 +77,19 @@ class MatchAPI final : public RouteAPI sub_routes[index].unpacked_path_segments, sub_routes[index].source_traversed_in_reverse, sub_routes[index].target_traversed_in_reverse); - route.values["confidence"] = sub_matchings[index].confidence; - routes.values.push_back(std::move(route)); + route.values.emplace("confidence", sub_matchings[index].confidence); + routes.values.emplace_back(std::move(route)); } if (!parameters.skip_waypoints) { - response.values["tracepoints"] = MakeTracepoints(sub_matchings); + response.values.emplace("tracepoints", MakeTracepoints(sub_matchings)); } - response.values["matchings"] = std::move(routes); - response.values["code"] = "Ok"; + response.values.emplace("matchings", std::move(routes)); + 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); } } @@ -132,13 +132,13 @@ class MatchAPI final : public RouteAPI if (tidy_result.can_be_removed[trace_index]) { - waypoints.push_back(fbresult::WaypointBuilder(fb_result).Finish()); + waypoints.emplace_back(fbresult::WaypointBuilder(fb_result).Finish()); continue; } auto matching_index = trace_idx_to_matching_idx[trace_index]; if (matching_index.NotMatched()) { - waypoints.push_back(fbresult::WaypointBuilder(fb_result).Finish()); + waypoints.emplace_back(fbresult::WaypointBuilder(fb_result).Finish()); continue; } const auto &phantom = @@ -165,7 +165,7 @@ class MatchAPI final : public RouteAPI { waypoint->add_waypoint_index(matching_index.point_index); } - waypoints.push_back(waypoint->Finish()); + waypoints.emplace_back(waypoint->Finish()); } return fb_result.CreateVector(waypoints); @@ -186,23 +186,23 @@ class MatchAPI final : public RouteAPI { if (tidy_result.can_be_removed[trace_index]) { - waypoints.values.push_back(util::json::Null()); + waypoints.values.emplace_back(util::json::Null()); continue; } auto matching_index = trace_idx_to_matching_idx[trace_index]; if (matching_index.NotMatched()) { - waypoints.values.push_back(util::json::Null()); + waypoints.values.emplace_back(util::json::Null()); continue; } const auto &phantom = sub_matchings[matching_index.sub_matching_index].nodes[matching_index.point_index]; auto waypoint = BaseAPI::MakeWaypoint({phantom}); - waypoint.values["matchings_index"] = matching_index.sub_matching_index; - waypoint.values["waypoint_index"] = matching_index.point_index; - waypoint.values["alternatives_count"] = - sub_matchings[matching_index.sub_matching_index] - .alternatives_count[matching_index.point_index]; + waypoint.values.emplace("matchings_index", matching_index.sub_matching_index); + waypoint.values.emplace("waypoint_index", matching_index.point_index); + waypoint.values.emplace("alternatives_count", + sub_matchings[matching_index.sub_matching_index] + .alternatives_count[matching_index.point_index]); // waypoint indices need to be adjusted if route legs were collapsed // waypoint parameter assumes there is only one match object if (!parameters.waypoints.empty()) @@ -217,7 +217,7 @@ class MatchAPI final : public RouteAPI waypoint.values["waypoint_index"] = util::json::Null(); } } - waypoints.values.push_back(std::move(waypoint)); + waypoints.values.emplace_back(std::move(waypoint)); } return waypoints;