From be0424bd93d509d933dc30b24181c36f1632135b Mon Sep 17 00:00:00 2001 From: Guillaume Autran Date: Fri, 29 Dec 2017 15:43:46 -0500 Subject: [PATCH] Small performance improvements Address small performance issues to ROS2 impacting the `rmw_count_publishers` and `rmw_count_subscribers` method when called with a significante number of topics to count. The root of the performance issues come from the duplication of `std::string` type of objects. These duplication are honerous with strings and cause a significante amount of CPU cycle to be wasted duplicating things. --- rmw_fastrtps_cpp/src/demangle.cpp | 6 +----- rmw_fastrtps_cpp/src/namespace_prefix.cpp | 16 ++++++++++++++-- rmw_fastrtps_cpp/src/namespace_prefix.hpp | 3 +++ rmw_fastrtps_cpp/src/rmw_count.cpp | 8 ++++---- 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/rmw_fastrtps_cpp/src/demangle.cpp b/rmw_fastrtps_cpp/src/demangle.cpp index dd96d8a1f..2c1c3a57f 100644 --- a/rmw_fastrtps_cpp/src/demangle.cpp +++ b/rmw_fastrtps_cpp/src/demangle.cpp @@ -25,11 +25,7 @@ std::string _demangle_if_ros_topic(const std::string & topic_name) { - std::string prefix = _get_ros_prefix_if_exists(topic_name); - if (prefix.length()) { - return topic_name.substr(strlen(ros_topic_prefix)); - } - return topic_name; + return _strip_ros_prefix_if_exists(topic_name); } /// Return the demangled ROS type or the original if not a ROS type. diff --git a/rmw_fastrtps_cpp/src/namespace_prefix.cpp b/rmw_fastrtps_cpp/src/namespace_prefix.cpp index 16600fd44..569054daf 100644 --- a/rmw_fastrtps_cpp/src/namespace_prefix.cpp +++ b/rmw_fastrtps_cpp/src/namespace_prefix.cpp @@ -32,10 +32,22 @@ const std::vector _ros_prefixes = std::string _get_ros_prefix_if_exists(const std::string & topic_name) { - for (auto prefix : _ros_prefixes) { - if (topic_name.rfind(std::string(prefix) + "/", 0) == 0) { + for (const auto & prefix : _ros_prefixes) { + if (topic_name.rfind(prefix, 0) == 0 && topic_name.at(prefix.length()) == '/') { return prefix; } } return ""; } + +/// Strip the ROS specific prefix if it exists from the topic name. +std::string +_strip_ros_prefix_if_exists(const std::string & topic_name) +{ + for (const auto & prefix : _ros_prefixes) { + if (topic_name.rfind(prefix, 0) == 0 && topic_name.at(prefix.length()) == '/') { + return topic_name.substr(prefix.length()); + } + } + return topic_name; +} diff --git a/rmw_fastrtps_cpp/src/namespace_prefix.hpp b/rmw_fastrtps_cpp/src/namespace_prefix.hpp index 8f8172f70..191a35a89 100644 --- a/rmw_fastrtps_cpp/src/namespace_prefix.hpp +++ b/rmw_fastrtps_cpp/src/namespace_prefix.hpp @@ -31,4 +31,7 @@ extern const std::vector _ros_prefixes; std::string _get_ros_prefix_if_exists(const std::string & topic_name); +/// Returns the topic name stripped of and ROS specific prefix if exists. +std::string +_strip_ros_prefix_if_exists(const std::string & topic_name); #endif // NAMESPACE_PREFIX_HPP_ diff --git a/rmw_fastrtps_cpp/src/rmw_count.cpp b/rmw_fastrtps_cpp/src/rmw_count.cpp index 207472f8a..0ed66c4e6 100644 --- a/rmw_fastrtps_cpp/src/rmw_count.cpp +++ b/rmw_fastrtps_cpp/src/rmw_count.cpp @@ -51,8 +51,8 @@ rmw_count_publishers( WriterInfo * slave_target = impl->secondaryPubListener; slave_target->mapmutex.lock(); *count = 0; - for (auto it : slave_target->topicNtypes) { - auto topic_fqdn = _demangle_if_ros_topic(it.first); + for (const auto & it : slave_target->topicNtypes) { + const auto topic_fqdn = _demangle_if_ros_topic(it.first); if (topic_fqdn == topic_name) { *count += it.second.size(); } @@ -90,8 +90,8 @@ rmw_count_subscribers( ReaderInfo * slave_target = impl->secondarySubListener; *count = 0; slave_target->mapmutex.lock(); - for (auto it : slave_target->topicNtypes) { - auto topic_fqdn = _demangle_if_ros_topic(it.first); + for (const auto & it : slave_target->topicNtypes) { + const auto topic_fqdn = _demangle_if_ros_topic(it.first); if (topic_fqdn == topic_name) { *count += it.second.size(); }