Apply micro-optimisation for Route API (#6948)

This commit is contained in:
Siarhei Fedartsou 2024-06-15 18:56:40 +02:00 committed by GitHub
parent 7652f6ca6b
commit a0eda3e7d7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 40 additions and 31 deletions

View File

@ -24,6 +24,7 @@
- NodeJS: - NodeJS:
- CHANGED: Use node-api instead of NAN. [#6452](https://github.com/Project-OSRM/osrm-backend/pull/6452) - CHANGED: Use node-api instead of NAN. [#6452](https://github.com/Project-OSRM/osrm-backend/pull/6452)
- Misc: - 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 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: 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: Avoid copy of intersection in totalTurnAngle. [#6938](https://github.com/Project-OSRM/osrm-backend/pull/6938)

View File

@ -110,14 +110,14 @@ class RouteAPI : public BaseAPI
if (!parameters.skip_waypoints) 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.emplace("routes", std::move(jsRoutes));
response.values["code"] = "Ok"; response.values.emplace("code", "Ok");
auto data_timestamp = facade.GetTimestamp(); auto data_timestamp = facade.GetTimestamp();
if (!data_timestamp.empty()) if (!data_timestamp.empty())
{ {
response.values["data_version"] = data_timestamp; response.values.emplace("data_version", data_timestamp);
} }
} }
@ -784,8 +784,9 @@ class RouteAPI : public BaseAPI
if (requested_annotations & RouteParameters::AnnotationsType::Speed) if (requested_annotations & RouteParameters::AnnotationsType::Speed)
{ {
double prev_speed = 0; double prev_speed = 0;
annotation.values["speed"] = GetAnnotations( annotation.values.emplace(
leg_geometry, "speed",
GetAnnotations(leg_geometry,
[&prev_speed](const guidance::LegGeometry::Annotation &anno) [&prev_speed](const guidance::LegGeometry::Annotation &anno)
{ {
if (anno.duration < std::numeric_limits<double>::min()) if (anno.duration < std::numeric_limits<double>::min())
@ -794,39 +795,46 @@ class RouteAPI : public BaseAPI
} }
else else
{ {
auto speed = std::round(anno.distance / anno.duration * 10.) / 10.; auto speed =
std::round(anno.distance / anno.duration * 10.) /
10.;
prev_speed = speed; prev_speed = speed;
return util::json::clamp_float(speed); return util::json::clamp_float(speed);
} }
}); }));
} }
if (requested_annotations & RouteParameters::AnnotationsType::Duration) if (requested_annotations & RouteParameters::AnnotationsType::Duration)
{ {
annotation.values["duration"] = annotation.values.emplace(
"duration",
GetAnnotations(leg_geometry, GetAnnotations(leg_geometry,
[](const guidance::LegGeometry::Annotation &anno) [](const guidance::LegGeometry::Annotation &anno)
{ return anno.duration; }); { return anno.duration; }));
} }
if (requested_annotations & RouteParameters::AnnotationsType::Distance) if (requested_annotations & RouteParameters::AnnotationsType::Distance)
{ {
annotation.values["distance"] = annotation.values.emplace(
"distance",
GetAnnotations(leg_geometry, GetAnnotations(leg_geometry,
[](const guidance::LegGeometry::Annotation &anno) [](const guidance::LegGeometry::Annotation &anno)
{ return anno.distance; }); { return anno.distance; }));
} }
if (requested_annotations & RouteParameters::AnnotationsType::Weight) if (requested_annotations & RouteParameters::AnnotationsType::Weight)
{ {
annotation.values["weight"] = GetAnnotations( annotation.values.emplace(
leg_geometry, "weight",
[](const guidance::LegGeometry::Annotation &anno) { return anno.weight; }); GetAnnotations(leg_geometry,
[](const guidance::LegGeometry::Annotation &anno)
{ return anno.weight; }));
} }
if (requested_annotations & RouteParameters::AnnotationsType::Datasources) if (requested_annotations & RouteParameters::AnnotationsType::Datasources)
{ {
annotation.values["datasources"] = annotation.values.emplace(
"datasources",
GetAnnotations(leg_geometry, GetAnnotations(leg_geometry,
[](const guidance::LegGeometry::Annotation &anno) [](const guidance::LegGeometry::Annotation &anno)
{ return anno.datasource; }); { return anno.datasource; }));
} }
if (requested_annotations & RouteParameters::AnnotationsType::Nodes) if (requested_annotations & RouteParameters::AnnotationsType::Nodes)
{ {
@ -837,7 +845,7 @@ class RouteAPI : public BaseAPI
nodes.values.push_back( nodes.values.push_back(
static_cast<std::uint64_t>(facade.GetOSMNodeIDOfNode(node_id))); static_cast<std::uint64_t>(facade.GetOSMNodeIDOfNode(node_id)));
} }
annotation.values["nodes"] = std::move(nodes); annotation.values.emplace("nodes", std::move(nodes));
} }
// Add any supporting metadata, if needed // Add any supporting metadata, if needed
if (requested_annotations & RouteParameters::AnnotationsType::Datasources) if (requested_annotations & RouteParameters::AnnotationsType::Datasources)
@ -853,8 +861,8 @@ class RouteAPI : public BaseAPI
break; break;
datasource_names.values.push_back(std::string(facade.GetDatasourceName(i))); datasource_names.values.push_back(std::string(facade.GetDatasourceName(i)));
} }
metadata.values["datasource_names"] = datasource_names; metadata.values.emplace("datasource_names", datasource_names);
annotation.values["metadata"] = metadata; annotation.values.emplace("metadata", metadata);
} }
annotations.push_back(std::move(annotation)); annotations.push_back(std::move(annotation));