From ff9b4920413583104e2a2e9c8567f4bf1e178242 Mon Sep 17 00:00:00 2001 From: Jackie Kay Date: Wed, 18 Nov 2015 18:16:18 -0800 Subject: [PATCH] remove redundant vector instantiation, replace with erase-remove idiom --- rclcpp/include/rclcpp/memory_strategy.hpp | 2 +- .../strategies/allocator_memory_strategy.hpp | 43 +++++++------------ rclcpp/src/rclcpp/executor.cpp | 2 +- 3 files changed, 17 insertions(+), 30 deletions(-) diff --git a/rclcpp/include/rclcpp/memory_strategy.hpp b/rclcpp/include/rclcpp/memory_strategy.hpp index a7ec910..e363914 100644 --- a/rclcpp/include/rclcpp/memory_strategy.hpp +++ b/rclcpp/include/rclcpp/memory_strategy.hpp @@ -52,7 +52,7 @@ public: virtual void clear_active_entities() = 0; virtual void clear_handles() = 0; - virtual void revalidate_handles() = 0; + virtual void remove_null_handles() = 0; virtual bool collect_entities(const WeakNodeVector & weak_nodes) = 0; /// Provide a newly initialized AnyExecutable object. diff --git a/rclcpp/include/rclcpp/strategies/allocator_memory_strategy.hpp b/rclcpp/include/rclcpp/strategies/allocator_memory_strategy.hpp index 3824bf6..6d14aca 100644 --- a/rclcpp/include/rclcpp/strategies/allocator_memory_strategy.hpp +++ b/rclcpp/include/rclcpp/strategies/allocator_memory_strategy.hpp @@ -108,35 +108,22 @@ public: client_handles_.clear(); } - void revalidate_handles() + void remove_null_handles() { - { - VectorRebind temp; - for (auto & subscriber_handle : subscriber_handles_) { - if (subscriber_handle) { - temp.push_back(subscriber_handle); - } - } - subscriber_handles_.swap(temp); - } - { - VectorRebind temp; - for (auto & service_handle : service_handles_) { - if (service_handle) { - temp.push_back(service_handle); - } - } - service_handles_.swap(temp); - } - { - VectorRebind temp; - for (auto & client_handle : client_handles_) { - if (client_handle) { - temp.push_back(client_handle); - } - } - client_handles_.swap(temp); - } + subscriber_handles_.erase( + std::remove(subscriber_handles_.begin(), subscriber_handles_.end(), nullptr), + subscriber_handles_.end() + ); + + service_handles_.erase( + std::remove(service_handles_.begin(), service_handles_.end(), nullptr), + service_handles_.end() + ); + + client_handles_.erase( + std::remove(client_handles_.begin(), client_handles_.end(), nullptr), + client_handles_.end() + ); } bool collect_entities(const WeakNodeVector & weak_nodes) diff --git a/rclcpp/src/rclcpp/executor.cpp b/rclcpp/src/rclcpp/executor.cpp index 981934c..7a39c0b 100644 --- a/rclcpp/src/rclcpp/executor.cpp +++ b/rclcpp/src/rclcpp/executor.cpp @@ -379,7 +379,7 @@ Executor::wait_for_work(std::chrono::nanoseconds timeout) return; } - memory_strategy_->revalidate_handles(); + memory_strategy_->remove_null_handles(); } rclcpp::node::Node::SharedPtr