From f88ac989ea3003d19ba10f34bff7a1e1e497b097 Mon Sep 17 00:00:00 2001 From: Moritz Kobitzsch Date: Fri, 11 Nov 2016 14:09:04 +0100 Subject: [PATCH] reserve when possible --- include/engine/api/json_factory.hpp | 3 ++- include/engine/api/route_api.hpp | 12 ++++----- include/engine/guidance/assemble_steps.hpp | 1 - include/engine/polyline_compressor.hpp | 19 +++++++------ .../guidance/node_based_graph_walker.hpp | 12 ++++----- include/util/debug.hpp | 7 ++--- include/util/guidance/bearing_class.hpp | 2 ++ src/engine/guidance/post_processing.cpp | 2 +- .../guidance/intersection_generator.cpp | 2 ++ src/extractor/guidance/sliproad_handler.cpp | 27 +++++++++---------- src/extractor/guidance/turn_lane_data.cpp | 1 + src/extractor/guidance/turn_lane_handler.cpp | 2 ++ src/util/guidance/bearing_class.cpp | 2 ++ unit_tests/engine/geometry_string.cpp | 1 - 14 files changed, 51 insertions(+), 42 deletions(-) diff --git a/include/engine/api/json_factory.hpp b/include/engine/api/json_factory.hpp index 609db4b33..cfb734f2b 100644 --- a/include/engine/api/json_factory.hpp +++ b/include/engine/api/json_factory.hpp @@ -42,7 +42,8 @@ std::string modeToString(const extractor::TravelMode mode); } // namespace detail -template util::json::String makePolyline(ForwardIter begin, ForwardIter end) +template +util::json::String makePolyline(ForwardIter begin, ForwardIter end) { return {encodePolyline(begin, end)}; } diff --git a/include/engine/api/route_api.hpp b/include/engine/api/route_api.hpp index fa83eca8e..f36390d75 100644 --- a/include/engine/api/route_api.hpp +++ b/include/engine/api/route_api.hpp @@ -196,16 +196,16 @@ class RouteAPI : public BaseAPI [this, &leg_geometry](const guidance::RouteStep &step) { if (parameters.geometries == RouteParameters::GeometriesType::Polyline) { - return static_cast( - json::makePolyline<100000>(leg_geometry.locations.begin() + step.geometry_begin, - leg_geometry.locations.begin() + step.geometry_end)); + return static_cast(json::makePolyline<100000>( + leg_geometry.locations.begin() + step.geometry_begin, + leg_geometry.locations.begin() + step.geometry_end)); } if (parameters.geometries == RouteParameters::GeometriesType::Polyline6) { - return static_cast( - json::makePolyline<1000000>(leg_geometry.locations.begin() + step.geometry_begin, - leg_geometry.locations.begin() + step.geometry_end)); + return static_cast(json::makePolyline<1000000>( + leg_geometry.locations.begin() + step.geometry_begin, + leg_geometry.locations.begin() + step.geometry_end)); } BOOST_ASSERT(parameters.geometries == RouteParameters::GeometriesType::GeoJSON); diff --git a/include/engine/guidance/assemble_steps.hpp b/include/engine/guidance/assemble_steps.hpp index d0e917a16..93b32d950 100644 --- a/include/engine/guidance/assemble_steps.hpp +++ b/include/engine/guidance/assemble_steps.hpp @@ -233,7 +233,6 @@ inline std::vector assembleSteps(const datafacade::BaseDataFacade &fa WaypointType::Arrive, 0}; - BOOST_ASSERT(!leg_geometry.locations.empty()); steps.push_back(RouteStep{target_node.name_id, facade.GetNameForID(target_node.name_id), diff --git a/include/engine/polyline_compressor.hpp b/include/engine/polyline_compressor.hpp index f1261a384..96d735930 100644 --- a/include/engine/polyline_compressor.hpp +++ b/include/engine/polyline_compressor.hpp @@ -3,8 +3,8 @@ #include "util/coordinate.hpp" -#include #include +#include #include #include @@ -13,17 +13,17 @@ namespace osrm namespace engine { namespace detail - { - constexpr double POLYLINE_DECODING_PRECISION = 1e5; - constexpr double POLYLINE_TO_COORDINATE = COORDINATE_PRECISION / POLYLINE_DECODING_PRECISION; +{ +constexpr double POLYLINE_DECODING_PRECISION = 1e5; +constexpr double POLYLINE_TO_COORDINATE = COORDINATE_PRECISION / POLYLINE_DECODING_PRECISION; - std::string encode(std::vector &numbers); - } +std::string encode(std::vector &numbers); +} using CoordVectorForwardIter = std::vector::const_iterator; // Encodes geometry into polyline format. // See: https://developers.google.com/maps/documentation/utilities/polylinealgorithm -template +template std::string encodePolyline(CoordVectorForwardIter begin, CoordVectorForwardIter end) { double coordinate_to_polyline = POLYLINE_PRECISION / COORDINATE_PRECISION; @@ -39,7 +39,10 @@ std::string encodePolyline(CoordVectorForwardIter begin, CoordVectorForwardIter int current_lat = 0; int current_lon = 0; std::for_each( - begin, end, [&delta_numbers, ¤t_lat, ¤t_lon, coordinate_to_polyline](const util::Coordinate loc) { + begin, + end, + [&delta_numbers, ¤t_lat, ¤t_lon, coordinate_to_polyline]( + const util::Coordinate loc) { const int lat_diff = std::round(static_cast(loc.lat) * coordinate_to_polyline) - current_lat; const int lon_diff = diff --git a/include/extractor/guidance/node_based_graph_walker.hpp b/include/extractor/guidance/node_based_graph_walker.hpp index dbbbba43c..0da9f9e77 100644 --- a/include/extractor/guidance/node_based_graph_walker.hpp +++ b/include/extractor/guidance/node_based_graph_walker.hpp @@ -87,7 +87,8 @@ struct LengthLimitedCoordinateAccumulator }; /* - * The SelectRoadByNameOnlyChoiceAndStraightness tries to follow a given name along a route. We offer methods to skip + * The SelectRoadByNameOnlyChoiceAndStraightness tries to follow a given name along a route. We + * offer methods to skip * over bridges/similar situations if desired, following narrow turns * This struct offers an example implementation of a possible road selector for traversing the * node-based graph using the NodeBasedGraphWalker @@ -115,14 +116,13 @@ struct SelectRoadByNameOnlyChoiceAndStraightness // find the next intersection given a hop limit struct IntersectionFinderAccumulator { - IntersectionFinderAccumulator(const std::uint8_t hop_limit, const IntersectionGenerator &intersection_generator); + IntersectionFinderAccumulator(const std::uint8_t hop_limit, + const IntersectionGenerator &intersection_generator); // true if the path has traversed enough distance bool terminate(); // update the accumulator - void update(const NodeID from_node, - const EdgeID via_edge, - const NodeID to_node); + void update(const NodeID from_node, const EdgeID via_edge, const NodeID to_node); std::uint8_t hops; const std::uint8_t hop_limit; @@ -136,8 +136,6 @@ struct IntersectionFinderAccumulator Intersection intersection; }; - - template boost::optional> NodeBasedGraphWalker::TraverseRoad(NodeID current_node_id, diff --git a/include/util/debug.hpp b/include/util/debug.hpp index 1ae7f8569..69eb745ee 100644 --- a/include/util/debug.hpp +++ b/include/util/debug.hpp @@ -25,8 +25,8 @@ inline void print(const engine::guidance::RouteStep &step) { std::cout << static_cast(step.maneuver.instruction.type) << " " << static_cast(step.maneuver.instruction.direction_modifier) << " " - << static_cast(step.maneuver.waypoint_type) << " " - << step.maneuver.location << " " + << static_cast(step.maneuver.waypoint_type) << " " << step.maneuver.location + << " " << " Duration: " << step.duration << " Distance: " << step.distance << " Geometry: " << step.geometry_begin << " " << step.geometry_end << "\n\tIntersections: " << step.intersections.size() << " ["; @@ -44,7 +44,8 @@ inline void print(const engine::guidance::RouteStep &step) std::cout << " " << (entry ? "true" : "false"); std::cout << ")"; } - std::cout << "] name[" << step.name_id << "]: " << step.name << " Ref: " << step.ref << " Pronunciation: " << step.pronunciation; + std::cout << "] name[" << step.name_id << "]: " << step.name << " Ref: " << step.ref + << " Pronunciation: " << step.pronunciation; } inline void print(const std::vector &steps) diff --git a/include/util/guidance/bearing_class.hpp b/include/util/guidance/bearing_class.hpp index 1f2f9ac4c..ce727b67e 100644 --- a/include/util/guidance/bearing_class.hpp +++ b/include/util/guidance/bearing_class.hpp @@ -39,6 +39,8 @@ namespace guidance class BearingClass { public: + BearingClass(); + // Add a bearing to the set void add(const DiscreteBearing bearing); diff --git a/src/engine/guidance/post_processing.cpp b/src/engine/guidance/post_processing.cpp index 15d453f28..18b6912bd 100644 --- a/src/engine/guidance/post_processing.cpp +++ b/src/engine/guidance/post_processing.cpp @@ -818,7 +818,7 @@ void collapseTurnAt(std::vector &steps, // bool isStaggeredIntersection(const RouteStep &previous, const RouteStep ¤t) { - //don't touch roundabouts + // don't touch roundabouts if (entersRoundabout(previous.maneuver.instruction) || entersRoundabout(current.maneuver.instruction)) return false; diff --git a/src/extractor/guidance/intersection_generator.cpp b/src/extractor/guidance/intersection_generator.cpp index 0d66ae22a..ad1d54655 100644 --- a/src/extractor/guidance/intersection_generator.cpp +++ b/src/extractor/guidance/intersection_generator.cpp @@ -56,6 +56,8 @@ Intersection IntersectionGenerator::GetConnectedRoads(const NodeID from_node, { Intersection intersection; const NodeID turn_node = node_based_graph.GetTarget(via_eid); + // reserve enough items (+ the possibly missing u-turn edge) + intersection.reserve(node_based_graph.GetOutDegree(turn_node) + 1); const NodeID only_restriction_to_node = [&]() { // If only restrictions refer to invalid ways somewhere far away, we rather ignore the // restriction than to not route over the intersection at all. diff --git a/src/extractor/guidance/sliproad_handler.cpp b/src/extractor/guidance/sliproad_handler.cpp index 6de847851..174b26720 100644 --- a/src/extractor/guidance/sliproad_handler.cpp +++ b/src/extractor/guidance/sliproad_handler.cpp @@ -215,22 +215,21 @@ operator()(const NodeID, const EdgeID source_edge_id, Intersection intersection) const bool is_through_street = !target_intersection.empty() && link_data.name_id != EMPTY_NAMEID && target_intersection.end() != - std::find_if(target_intersection.begin() + 1, - target_intersection.end(), - [this, &link_data](const ConnectedRoad &road) { - const auto &road_edge_data = - node_based_graph.GetEdgeData(road.eid); + std::find_if( + target_intersection.begin() + 1, + target_intersection.end(), + [this, &link_data](const ConnectedRoad &road) { + const auto &road_edge_data = node_based_graph.GetEdgeData(road.eid); - const auto same_name = - road_edge_data.name_id != EMPTY_NAMEID && - !util::guidance::requiresNameAnnounced( - road_edge_data.name_id, - link_data.name_id, - name_table, - street_name_suffix_table); + const auto same_name = + road_edge_data.name_id != EMPTY_NAMEID && + !util::guidance::requiresNameAnnounced(road_edge_data.name_id, + link_data.name_id, + name_table, + street_name_suffix_table); - return same_name; - }); + return same_name; + }); // if the sliproad candidate is a through street, we cannot handle it as a sliproad if (is_through_street) diff --git a/src/extractor/guidance/turn_lane_data.cpp b/src/extractor/guidance/turn_lane_data.cpp index 11c287d1f..7695b589d 100644 --- a/src/extractor/guidance/turn_lane_data.cpp +++ b/src/extractor/guidance/turn_lane_data.cpp @@ -106,6 +106,7 @@ LaneDataVector laneDataFromDescription(TurnLaneDescription turn_lane_description // transform the map into the lane data vector LaneDataVector lane_data; + lane_data.reserve(lane_map.size()); for (const auto tag : lane_map) lane_data.push_back({tag.first, tag.second.first, tag.second.second, false}); diff --git a/src/extractor/guidance/turn_lane_handler.cpp b/src/extractor/guidance/turn_lane_handler.cpp index 0a6e9744b..e000c35bc 100644 --- a/src/extractor/guidance/turn_lane_handler.cpp +++ b/src/extractor/guidance/turn_lane_handler.cpp @@ -619,6 +619,8 @@ std::pair TurnLaneHandler::partitionLaneData( }; LaneDataVector first, second; + first.reserve(turn_lane_data.size()); + second.reserve(turn_lane_data.size()); for (std::size_t lane = 0; lane < turn_lane_data.size(); ++lane) { if (matched_at_second[lane]) diff --git a/src/util/guidance/bearing_class.cpp b/src/util/guidance/bearing_class.cpp index 7561f26fe..fb2dc1f05 100644 --- a/src/util/guidance/bearing_class.cpp +++ b/src/util/guidance/bearing_class.cpp @@ -11,6 +11,8 @@ namespace util namespace guidance { +BearingClass::BearingClass() { available_bearings.reserve(10); } + bool BearingClass::operator==(const BearingClass &other) const { BOOST_ASSERT(std::is_sorted(available_bearings.begin(), available_bearings.end())); diff --git a/unit_tests/engine/geometry_string.cpp b/unit_tests/engine/geometry_string.cpp index fffe56e7e..e522829bd 100644 --- a/unit_tests/engine/geometry_string.cpp +++ b/unit_tests/engine/geometry_string.cpp @@ -65,7 +65,6 @@ BOOST_AUTO_TEST_CASE(encode) BOOST_CHECK_EQUAL(encodedPolyline, polyline); } - BOOST_AUTO_TEST_CASE(encode6) { // Coordinates; these would be the coordinates we give the loc parameter,