Behavior optimizations#692
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR optimizes CPU usage by caching repeated computations, standardizing TF buffer initialization, switching to a lighter weight executor, and replacing costly TF transforms with direct numpy operations.
- Standardized
tf_bufferinitialization across nodes with the newBuffersignature - Swapped
MultiThreadedExecutorforEventsExecutorand added cache clearing - Introduced a
@cached_capsule_functiondecorator and applied it to expensive capsule methods
Reviewed Changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| bitbots_world_model/bitbots_robot_filter/bitbots_robot_filter/filter.py | Reordered Buffer arguments for tf_buffer |
| bitbots_world_model/bitbots_ball_filter/bitbots_ball_filter/ball_filter.py | Reordered Buffer arguments for tf_buffer |
| bitbots_team_communication/bitbots_team_communication/bitbots_team_communication/bitbots_team_communication.py | Changed tf_buffer to use named argument |
| bitbots_navigation/bitbots_path_planning/bitbots_path_planning/path_planning.py | Reordered Buffer arguments for tf_buffer |
| bitbots_behavior/bitbots_body_behavior/bitbots_body_behavior/bitbots_body_behavior.py | Replaced MultiThreadedExecutor with EventsExecutor |
| bitbots_behavior/bitbots_blackboard/bitbots_blackboard/capsules/world_model_capsule.py | Added caching decorator and replaced TF-based transform with numpy |
| bitbots_behavior/bitbots_blackboard/bitbots_blackboard/capsules/team_data_capsule.py | Applied @cached_capsule_function to team data methods |
| bitbots_behavior/bitbots_blackboard/bitbots_blackboard/capsules/init.py | Defined cached_capsule_function and added per-capsule cache |
| bitbots_behavior/bitbots_blackboard/bitbots_blackboard/body_blackboard.py | Added clear_cache to flush all capsule caches |
Comments suppressed due to low confidence (2)
bitbots_behavior/bitbots_blackboard/bitbots_blackboard/capsules/init.py:11
- The new caching decorator introduces stateful behavior; consider adding unit tests to verify that caching and
clear_cachecorrectly store and invalidate results.
def cached_capsule_function(method):
bitbots_behavior/bitbots_blackboard/bitbots_blackboard/capsules/team_data_capsule.py:72
- [nitpick] The method name
timemay be ambiguous or collide with modules; consider renaming it tocurrent_timeornowfor clarity.
def time(self) -> Time:
| self.set_state_defaults() | ||
|
|
||
| self.tf_buffer = Buffer(self.node) | ||
| self.tf_buffer = Buffer(node=self.node) |
There was a problem hiding this comment.
Other TF buffers in this project specify a cache duration; relying on the default may lead to inconsistency. Consider passing the intended Duration explicitly.
| super().__init__("bitbots_robot_filter") | ||
|
|
||
| self.tf_buffer = Buffer(self, Duration(seconds=10.0)) | ||
| self.tf_buffer = Buffer(Duration(seconds=10.0), self) |
There was a problem hiding this comment.
[nitpick] Using positional parameters for the new Buffer signature can be less clear. Consider switching to named arguments (e.g., Buffer(cache_time=Duration(...), node=self)) for readability.
| self.tf_buffer = Buffer(Duration(seconds=10.0), self) | |
| self.tf_buffer = Buffer(cache_time=Duration(seconds=10.0), node=self) |
Summary
Bring peak behavior CPU load from 99% to ~40% with an avg. of ~25%
Proposed changes
Checklist
colcon build