Skip to content

Commit

Permalink
perf PR 8461
Browse files Browse the repository at this point in the history
  • Loading branch information
technolojin committed Oct 21, 2024
1 parent 3f18f0d commit 36b0cb6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <rclcpp/rclcpp.hpp>
#include <tier4_autoware_utils/ros/transform_listener.hpp>
#include <tier4_autoware_utils/system/lru_cache.hpp>
#include <tier4_autoware_utils/system/stop_watch.hpp>

#include <autoware_auto_mapping_msgs/msg/had_map_bin.hpp>
Expand All @@ -33,6 +34,7 @@
#include <lanelet2_core/Forward.h>
#include <lanelet2_core/LaneletMap.h>
#include <lanelet2_routing/Forward.h>
#include <lanelet2_routing/LaneletPath.h>
#include <lanelet2_traffic_rules/TrafficRules.h>

#include <deque>
Expand All @@ -42,6 +44,28 @@
#include <utility>
#include <vector>

namespace std
{
template <>
struct hash<lanelet::routing::LaneletPaths>
{
// 0x9e3779b9 is a magic number. See
// https://stackoverflow.com/questions/4948780/magic-number-in-boosthash-combine
size_t operator()(const lanelet::routing::LaneletPaths & paths) const
{
size_t seed = 0;
for (const auto & path : paths) {
for (const auto & lanelet : path) {
seed ^= hash<int64_t>{}(lanelet.id()) + 0x9e3779b9 + (seed << 6U) + (seed >> 2U);
}
// Add a separator between paths
seed ^= hash<int>{}(0) + 0x9e3779b9 + (seed << 6U) + (seed >> 2U);
}
return seed;
}
};
} // namespace std

namespace map_based_prediction
{
struct LateralKinematicsToLanelet
Expand Down Expand Up @@ -223,6 +247,10 @@ class MapBasedPredictionNode : public rclcpp::Node
const Maneuver & maneuver, std::vector<PredictedRefPath> & reference_paths);
std::vector<PosePath> convertPathType(const lanelet::routing::LaneletPaths & paths);

mutable tier4_autoware_utils::LRUCache<lanelet::routing::LaneletPaths, std::vector<PosePath>>
lru_cache_of_convert_path_type_{1000};
std::vector<PosePath> convertPathType(const lanelet::routing::LaneletPaths & paths) const;

void updateFuturePossibleLanelets(
const TrackedObject & object, const lanelet::routing::LaneletPaths & paths);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,7 @@ void MapBasedPredictionNode::mapCallback(const HADMapBin::ConstSharedPtr msg)
lanelet_map_ptr_ = std::make_shared<lanelet::LaneletMap>();
lanelet::utils::conversion::fromBinMsg(
*msg, lanelet_map_ptr_, &traffic_rules_ptr_, &routing_graph_ptr_);
lru_cache_of_convert_path_type_.clear(); // clear cache
RCLCPP_INFO(get_logger(), "[Map Based Prediction]: Map is loaded");

const auto all_lanelets = lanelet::utils::query::laneletLayer(lanelet_map_ptr_);
Expand Down Expand Up @@ -1994,6 +1995,10 @@ ManeuverProbability MapBasedPredictionNode::calculateManeuverProbability(
std::vector<PosePath> MapBasedPredictionNode::convertPathType(
const lanelet::routing::LaneletPaths & paths)
{
if (lru_cache_of_convert_path_type_.contains(paths)) {
return *lru_cache_of_convert_path_type_.get(paths);
}

std::vector<PosePath> converted_paths;
for (const auto & path : paths) {
PosePath converted_path;
Expand Down Expand Up @@ -2077,6 +2082,7 @@ std::vector<PosePath> MapBasedPredictionNode::convertPathType(
converted_paths.push_back(resampled_converted_path);
}

lru_cache_of_convert_path_type_.put(paths, converted_paths);
return converted_paths;
}

Expand Down

0 comments on commit 36b0cb6

Please sign in to comment.