Skip to content

deprecate rclcpp::spin_some and rclcpp::spin_all #2848

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: rolling
Choose a base branch
from

Conversation

alsora
Copy link
Collaborator

@alsora alsora commented May 17, 2025

Description

Deprecate rclcpp::spin_some and rclcpp::spin_all.
These functions can lead to very poor performance for developers who are not aware that calling them will:

  • create an executor
  • add the node to the executor
  • do the spin
  • destroy the executor

If called in a loop, you are wasting a lot of CPU cycles repeating those operations every time.
This instead is a lot more efficient because only the spin operation is repeated as expected.

SingleThreadedExecutor executor;
executor.add_node(node);
while (should_spin)
{
    executor.spin_some();
}

Adding and removing nodes from executors are expensive operations that shouldn't be done at "steady state" of the system.

IMPORTANT
In this PR I'm also re-ordering the events-executor constructor arguments so that this becomes possible:

MyExecutorType executor(options);

Now all executors take the executor options as first argument.

Is this user-facing behavior change?

Yes, deprecating some functions and modifying a constructor (although it's in the experimental namespace)

Did you use Generative AI?

No

closes #2675

@alsora alsora force-pushed the asoragna/deprecate-global-spin-some-all branch from 4bf1cb8 to e8e8faf Compare May 17, 2025 08:20
alsora added 2 commits May 17, 2025 10:21
…match the other executors

Signed-off-by: Alberto Soragna <alberto.soragna@gmail.com>
Signed-off-by: Alberto Soragna <alberto.soragna@gmail.com>
@alsora alsora force-pushed the asoragna/deprecate-global-spin-some-all branch from e8e8faf to f1e7310 Compare May 17, 2025 08:21
#else // !defined(_WIN32)
# pragma warning(push)
# pragma warning(disable: 4996)
#endif
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a macro for this in cpputils

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#else // !defined(_WIN32)
# pragma warning(push)
# pragma warning(disable: 4996)
#endif
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


auto request = std::make_shared<test_msgs::srv::Empty::Request>();
auto future = empty_client->async_send_request(request);
state.ResumeTiming();
benchmark::DoNotOptimize(service);
benchmark::ClobberMemory();

rclcpp::spin_until_future_complete(node->get_node_base_interface(), future);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need deprecate this too?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Deprecate all spin_xxx functions in rclcpp namespace instead of spin.
4 participants