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:
- 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)

View File

@ -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<double>::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<double>::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<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
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));