Skip to content

Commit 0188d2b

Browse files
Use std::unordered_map::emplace instead of operator[] when producing JSONs (#6936)
1 parent 054161f commit 0188d2b

File tree

2 files changed

+69
-51
lines changed

2 files changed

+69
-51
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
- NodeJS:
2525
- CHANGED: Use node-api instead of NAN. [#6452](https://github.com/Project-OSRM/osrm-backend/pull/6452)
2626
- Misc:
27+
- CHANGED: Use std::unordered_map::emplace instead of operator[] when producing JSONs. [#6936](https://github.com/Project-OSRM/osrm-backend/pull/6936)
2728
- CHANGED: Avoid copy of vectors in MakeRoute function. [#6939](https://github.com/Project-OSRM/osrm-backend/pull/6939)
2829
- FIXED: Fix bugprone-unused-return-value clang-tidy warning. [#6934](https://github.com/Project-OSRM/osrm-backend/pull/6934)
2930
- FIXED: Fix performance-noexcept-move-constructor clang-tidy warning. [#6931](https://github.com/Project-OSRM/osrm-backend/pull/6933)

src/engine/api/json_factory.cpp

Lines changed: 68 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,22 @@ util::json::Array lanesFromIntersection(const guidance::IntermediateIntersection
4444
{
4545
BOOST_ASSERT(intersection.lanes.lanes_in_turn >= 1);
4646
util::json::Array result;
47+
result.values.reserve(intersection.lane_description.size());
4748
LaneID lane_id = intersection.lane_description.size();
4849

4950
for (const auto &lane_desc : intersection.lane_description)
5051
{
5152
--lane_id;
5253
util::json::Object lane;
53-
lane.values["indications"] = toJSON(lane_desc);
54+
lane.values.emplace("indications", toJSON(lane_desc));
5455
if (lane_id >= intersection.lanes.first_lane_from_the_right &&
5556
lane_id <
5657
intersection.lanes.first_lane_from_the_right + intersection.lanes.lanes_in_turn)
57-
lane.values["valid"] = util::json::True();
58+
lane.values.emplace("valid", util::json::True());
5859
else
59-
lane.values["valid"] = util::json::False();
60+
lane.values.emplace("valid", util::json::False());
6061

61-
result.values.push_back(lane);
62+
result.values.emplace_back(std::move(lane));
6263
}
6364

6465
return result;
@@ -77,6 +78,7 @@ std::string waypointTypeToString(const guidance::WaypointType waypoint_type)
7778
util::json::Value coordinateToLonLat(const util::Coordinate &coordinate)
7879
{
7980
util::json::Array array;
81+
array.values.reserve(2);
8082
array.values.push_back(static_cast<double>(util::toFloating(coordinate.lon)));
8183
array.values.push_back(static_cast<double>(util::toFloating(coordinate.lat)));
8284
return util::json::Value{std::move(array)};
@@ -98,17 +100,20 @@ util::json::Object makeStepManeuver(const guidance::StepManeuver &maneuver)
98100
// These invalid responses should never happen: log if they do happen
99101
BOOST_ASSERT_MSG(maneuver_type != "invalid", "unexpected invalid maneuver type");
100102

101-
step_maneuver.values["type"] = std::move(maneuver_type);
103+
step_maneuver.values.emplace("type", std::move(maneuver_type));
102104

103105
if (detail::isValidModifier(maneuver))
104-
step_maneuver.values["modifier"] =
105-
osrm::guidance::instructionModifierToString(maneuver.instruction.direction_modifier);
106-
107-
step_maneuver.values["location"] = detail::coordinateToLonLat(maneuver.location);
108-
step_maneuver.values["bearing_before"] = detail::roundAndClampBearing(maneuver.bearing_before);
109-
step_maneuver.values["bearing_after"] = detail::roundAndClampBearing(maneuver.bearing_after);
106+
step_maneuver.values.emplace(
107+
"modifier",
108+
osrm::guidance::instructionModifierToString(maneuver.instruction.direction_modifier));
109+
110+
step_maneuver.values.emplace("location", detail::coordinateToLonLat(maneuver.location));
111+
step_maneuver.values.emplace("bearing_before",
112+
detail::roundAndClampBearing(maneuver.bearing_before));
113+
step_maneuver.values.emplace("bearing_after",
114+
detail::roundAndClampBearing(maneuver.bearing_after));
110115
if (maneuver.exit != 0)
111-
step_maneuver.values["exit"] = maneuver.exit;
116+
step_maneuver.values.emplace("exit", maneuver.exit);
112117

113118
return step_maneuver;
114119
}
@@ -137,16 +142,16 @@ util::json::Object makeIntersection(const guidance::IntermediateIntersection &in
137142
return util::json::False();
138143
});
139144

140-
result.values["location"] = detail::coordinateToLonLat(intersection.location);
141-
result.values["bearings"] = bearings;
142-
result.values["entry"] = entry;
145+
result.values.emplace("location", detail::coordinateToLonLat(intersection.location));
146+
result.values.emplace("bearings", bearings);
147+
result.values.emplace("entry", entry);
143148
if (intersection.in != guidance::IntermediateIntersection::NO_INDEX)
144-
result.values["in"] = intersection.in;
149+
result.values.emplace("in", intersection.in);
145150
if (intersection.out != guidance::IntermediateIntersection::NO_INDEX)
146-
result.values["out"] = intersection.out;
151+
result.values.emplace("out", intersection.out);
147152

148153
if (detail::hasValidLanes(intersection))
149-
result.values["lanes"] = detail::lanesFromIntersection(intersection);
154+
result.values.emplace("lanes", detail::lanesFromIntersection(intersection));
150155

151156
if (!intersection.classes.empty())
152157
{
@@ -157,7 +162,7 @@ util::json::Object makeIntersection(const guidance::IntermediateIntersection &in
157162
std::back_inserter(classes.values),
158163
[](const std::string &class_name)
159164
{ return util::json::String{class_name}; });
160-
result.values["classes"] = std::move(classes);
165+
result.values.emplace("classes", std::move(classes));
161166
}
162167

163168
return result;
@@ -166,39 +171,44 @@ util::json::Object makeIntersection(const guidance::IntermediateIntersection &in
166171
util::json::Object makeRouteStep(guidance::RouteStep step, util::json::Value geometry)
167172
{
168173
util::json::Object route_step;
169-
route_step.values["distance"] = std::round(step.distance * 10) / 10.;
170-
route_step.values["duration"] = step.duration;
171-
route_step.values["weight"] = step.weight;
172-
route_step.values["name"] = std::move(step.name);
174+
route_step.values.reserve(15);
175+
176+
route_step.values.emplace("distance", std::round(step.distance * 10) / 10.);
177+
route_step.values.emplace("duration", step.duration);
178+
route_step.values.emplace("weight", step.weight);
179+
route_step.values.emplace("name", step.name);
180+
173181
if (!step.ref.empty())
174-
route_step.values["ref"] = std::move(step.ref);
182+
route_step.values.emplace("ref", step.ref);
175183
if (!step.pronunciation.empty())
176-
route_step.values["pronunciation"] = std::move(step.pronunciation);
184+
route_step.values.emplace("pronunciation", step.pronunciation);
177185
if (!step.destinations.empty())
178-
route_step.values["destinations"] = std::move(step.destinations);
186+
route_step.values.emplace("destinations", step.destinations);
179187
if (!step.exits.empty())
180-
route_step.values["exits"] = std::move(step.exits);
188+
route_step.values.emplace("exits", step.exits);
181189
if (!step.rotary_name.empty())
182190
{
183-
route_step.values["rotary_name"] = std::move(step.rotary_name);
191+
route_step.values.emplace("rotary_name", step.rotary_name);
184192
if (!step.rotary_pronunciation.empty())
185193
{
186-
route_step.values["rotary_pronunciation"] = std::move(step.rotary_pronunciation);
194+
route_step.values.emplace("rotary_pronunciation", step.rotary_pronunciation);
187195
}
188196
}
189197

190-
route_step.values["mode"] = extractor::travelModeToString(step.mode);
191-
route_step.values["maneuver"] = makeStepManeuver(step.maneuver);
192-
route_step.values["geometry"] = std::move(geometry);
193-
route_step.values["driving_side"] = step.is_left_hand_driving ? "left" : "right";
198+
route_step.values.emplace("mode", extractor::travelModeToString(step.mode));
199+
route_step.values.emplace("maneuver", makeStepManeuver(step.maneuver));
200+
route_step.values.emplace("geometry", std::move(geometry));
201+
route_step.values.emplace("driving_side", step.is_left_hand_driving ? "left" : "right");
194202

195203
util::json::Array intersections;
196204
intersections.values.reserve(step.intersections.size());
205+
197206
std::transform(step.intersections.begin(),
198207
step.intersections.end(),
199208
std::back_inserter(intersections.values),
200209
makeIntersection);
201-
route_step.values["intersections"] = std::move(intersections);
210+
211+
route_step.values.emplace("intersections", std::move(intersections));
202212

203213
return route_step;
204214
}
@@ -209,14 +219,16 @@ util::json::Object makeRoute(const guidance::Route &route,
209219
const char *weight_name)
210220
{
211221
util::json::Object json_route;
212-
json_route.values["distance"] = route.distance;
213-
json_route.values["duration"] = route.duration;
214-
json_route.values["weight"] = route.weight;
215-
json_route.values["weight_name"] = weight_name;
216-
json_route.values["legs"] = std::move(legs);
222+
json_route.values.reserve(6);
223+
224+
json_route.values.emplace("distance", route.distance);
225+
json_route.values.emplace("duration", route.duration);
226+
json_route.values.emplace("weight", route.weight);
227+
json_route.values.emplace("weight_name", weight_name);
228+
json_route.values.emplace("legs", std::move(legs));
217229
if (geometry)
218230
{
219-
json_route.values["geometry"] = *std::move(geometry);
231+
json_route.values.emplace("geometry", *std::move(geometry));
220232
}
221233
return json_route;
222234
}
@@ -225,9 +237,11 @@ util::json::Object
225237
makeWaypoint(const util::Coordinate &location, const double &distance, std::string name)
226238
{
227239
util::json::Object waypoint;
228-
waypoint.values["location"] = detail::coordinateToLonLat(location);
229-
waypoint.values["name"] = std::move(name);
230-
waypoint.values["distance"] = distance;
240+
waypoint.values.reserve(3);
241+
242+
waypoint.values.emplace("location", detail::coordinateToLonLat(location));
243+
waypoint.values.emplace("name", std::move(name));
244+
waypoint.values.emplace("distance", distance);
231245
return waypoint;
232246
}
233247

@@ -237,26 +251,29 @@ util::json::Object makeWaypoint(const util::Coordinate &location,
237251
const Hint &location_hints)
238252
{
239253
auto waypoint = makeWaypoint(location, distance, std::move(name));
240-
waypoint.values["hint"] = location_hints.ToBase64();
254+
waypoint.values.reserve(1);
255+
waypoint.values.emplace("hint", location_hints.ToBase64());
241256
return waypoint;
242257
}
243258

244259
util::json::Object makeRouteLeg(guidance::RouteLeg leg, util::json::Array steps)
245260
{
246261
util::json::Object route_leg;
247-
route_leg.values["distance"] = leg.distance;
248-
route_leg.values["duration"] = leg.duration;
249-
route_leg.values["weight"] = leg.weight;
250-
route_leg.values["summary"] = std::move(leg.summary);
251-
route_leg.values["steps"] = std::move(steps);
262+
route_leg.values.reserve(5);
263+
264+
route_leg.values.emplace("distance", leg.distance);
265+
route_leg.values.emplace("duration", leg.duration);
266+
route_leg.values.emplace("weight", leg.weight);
267+
route_leg.values.emplace("summary", std::move(leg.summary));
268+
route_leg.values.emplace("steps", std::move(steps));
252269
return route_leg;
253270
}
254-
255271
util::json::Object
256272
makeRouteLeg(guidance::RouteLeg leg, util::json::Array steps, util::json::Object annotation)
257273
{
258274
util::json::Object route_leg = makeRouteLeg(std::move(leg), std::move(steps));
259-
route_leg.values["annotation"] = std::move(annotation);
275+
route_leg.values.reserve(1);
276+
route_leg.values.emplace("annotation", std::move(annotation));
260277
return route_leg;
261278
}
262279

0 commit comments

Comments
 (0)