Skip to content

Avoid unecessary creation of MultiThreadedExecutor#3090

Merged
ahcorde merged 2 commits intoros2:rollingfrom
solonovamax:bugfix/unecessary-executor
Mar 12, 2026
Merged

Avoid unecessary creation of MultiThreadedExecutor#3090
ahcorde merged 2 commits intoros2:rollingfrom
solonovamax:bugfix/unecessary-executor

Conversation

@solonovamax
Copy link
Contributor

Description

Avoids the unecessary creation of a MultiThreadedExecutor in the component_container_mt node.

The overhead of creating an unecessary MultiThreadedExecutor here is quite minimal, but there is some amount of overhead due to Executor's constructor.
I just noticed this and thought I'd contribute this, even though quite minor.

Also, I added const to node, as afaik there's no reason not to here.

Is this user-facing behavior change?

No.

Did you use Generative AI?

No. I don't use slop bots.

Signed-off-by: solonovamax <solonovamax@12oclockpoint.com>
auto exec = std::make_shared<rclcpp::executors::MultiThreadedExecutor>();
auto node = std::make_shared<rclcpp_components::ComponentManager>();
rclcpp::executors::MultiThreadedExecutor::SharedPtr exec;
const auto node = std::make_shared<rclcpp_components::ComponentManager>();
Copy link
Member

Choose a reason for hiding this comment

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

I don't believe that we can mark this as const, because the set_executor function mutates the node.

Did this compile for you?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't outright compile it, but my IDE (CLion) which uses clangd for its intentions said it was fine, which means that it should compile

the const applies to the shared_ptr, not the Node itself. const Node::SharedPtr (const shared_ptr<Node>) is different from Node::ConstSharedPtr (shared_ptr<const Node>)
you're thinking of the second one, but this is the first

node->set_executor(exec);
} else {
node->set_executor(exec);
exec = std::make_shared<rclcpp::executors::MultiThreadedExecutor>();
Copy link
Member

Choose a reason for hiding this comment

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

We could also consider using parameter_or and eliminating the branch entirely.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think get_parameter_or would eliminate the branch

instead you'd just end up with smth like

int thread_count;

node->get_parameter_or("thread_count", thread_count, 0);


if (thread_count > 0) {
    // ...
} else {
    // ...
}

so, you still have the branch, it just removes the two parameter calls & replaces them with a single one.
though ig at that point maybe you could do a ternary for exec? so like,

int thread_count;

node->get_parameter_or("thread_count", thread_count, 0);

auto exec = thread_count > 0
        ? /* ... */
        : /* ... */

(at least, I'm pretty sure this is what you'd get. I'm writing this on my phone and just looking at the docs for it)

Copy link
Collaborator

@fujitatomoya fujitatomoya left a comment

Choose a reason for hiding this comment

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

lgtm with green CI.

Signed-off-by: solonovamax <solonovamax@12oclockpoint.com>
@ahcorde
Copy link
Contributor

ahcorde commented Mar 12, 2026

Pulls: #3090
Gist: https://gist.githubusercontent.com/ahcorde/7b13d3fa8b7b121954f39d4b7502c616/raw/a8f147d45d5bba2e65d30c6eae95baac8ccb1f7f/ros2.repos
BUILD args: --packages-above-and-dependencies rclcpp_components
TEST args: --packages-above rclcpp_components
ROS Distro: rolling
Job: ci_launcher
ci_launcher ran: https://ci.ros2.org/job/ci_launcher/18447

  • Linux Build Status
  • Linux-aarch64 Build Status
  • Linux-rhel Build Status
  • Windows Build Status

@ahcorde ahcorde merged commit 8cd4d47 into ros2:rolling Mar 12, 2026
3 checks passed
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.

4 participants