From 786c4647f7eefb0038f4a4534f77f7d1d36f06dd Mon Sep 17 00:00:00 2001 From: Matthijs van der Burgh Date: Thu, 23 May 2024 17:35:22 +0200 Subject: [PATCH] (NumberOfEntities) improve performance (#1285) * (NumberOfEntities) add __iadd__ * (NumberOfEntities) improve __add__ performance This does indeed not work child classes, but the __repr__ was also not working for child classes. And no currently there are no child classes, so performance is more important. See https://github.com/ros2/rclpy/issues/1223 Signed-off-by: Matthijs van der Burgh --- rclpy/rclpy/waitable.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/rclpy/rclpy/waitable.py b/rclpy/rclpy/waitable.py index fb70bb107..74ffff295 100644 --- a/rclpy/rclpy/waitable.py +++ b/rclpy/rclpy/waitable.py @@ -36,12 +36,23 @@ def __init__( def __add__(self, other): result = self.__class__() - for attr in result.__slots__: - left = getattr(self, attr) - right = getattr(other, attr) - setattr(result, attr, left + right) + result.num_subscriptions = self.num_subscriptions + other.num_subscriptions + result.num_guard_conditions = self.num_guard_conditions + other.num_guard_conditions + result.num_timers = self.num_timers + other.num_timers + result.num_clients = self.num_clients + other.num_clients + result.num_services = self.num_services + other.num_services + result.num_events = self.num_events + other.num_events return result + def __iadd__(self, other): + self.num_subscriptions += other.num_subscriptions + self.num_guard_conditions += other.num_guard_conditions + self.num_timers += other.num_timers + self.num_clients += other.num_clients + self.num_services += other.num_services + self.num_events += other.num_events + return self + def __repr__(self): return '<{0}({1}, {2}, {3}, {4}, {5}, {6})>'.format( self.__class__.__name__, self.num_subscriptions,