Skip to content

Performance: Eliminate atomic reference-counting overhead in EventsExecutor collection updates#3193

Closed
adi213-glitch wants to merge 1 commit into
ros2:jazzyfrom
adi213-glitch:jazzy
Closed

Performance: Eliminate atomic reference-counting overhead in EventsExecutor collection updates#3193
adi213-glitch wants to merge 1 commit into
ros2:jazzyfrom
adi213-glitch:jazzy

Conversation

@adi213-glitch

Copy link
Copy Markdown

Issue :
During a performance review of the EventsExecutor hot paths, it was observed that std::shared_ptr entities (timers, subscriptions, clients, services, waitables) were being passed by value into lambda callbacks during refresh_current_collection().

Impact :
Passing std::shared_ptr by value triggers an atomic increment and subsequent decrement of the control block's reference counter. In high-frequency spin loops, these atomic Read-Modify-Write operations force hardware-level cache synchronization barriers, resulting in avoidable instruction pressure and cache-line bouncing across CPU cores.

Fix :
Updated lambda signatures within refresh_current_collection() to capture collection entities via const auto & or const rclcpp::TimerBase::SharedPtr &. This does zero-copy, read-only references to the underlying resources already owned structurally by the collection container, completely bypassing unnecessary atomic operations.

Testing & Verification :
Compiled using colcon on Ubuntu 24.04 / ROS 2 Jazzy.

Profiled utilizing a dedicated Google Benchmark rig scaling the active entity footprint up to 1,000 entities to isolate the micro-architectural behavior from background kernel scheduling noise.

Results: The zero-copy optimization demonstrated a measurable CPU execution latency reduction, saving ~3.64 ms per processing cycle on the magnified payload (amounting to a ~1.9% throughput enhancement).

The patch operates strictly internal to events_executor.cpp and maintains absolute public API and ABI compatibility.
Screenshot 2026-07-14 235140

Copilot AI review requested due to automatic review settings July 14, 2026 18:33
@mergify

mergify Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

…llection updates

Signed-off-by: Aditya <theaditya213@gmail.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR optimizes the EventsExecutor::refresh_current_collection() hot path by avoiding unnecessary std::shared_ptr copies when ExecutorEntitiesCollection diffs are applied, reducing atomic reference-count churn during high-frequency spinning.

Changes:

  • Updated update() callbacks for subscriptions/clients/services/waitables to take entities as const auto & instead of by value.
  • Updated timer update() callbacks to take const rclcpp::TimerBase::SharedPtr & instead of by value.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@skyegalaxy

skyegalaxy commented Jul 14, 2026

Copy link
Copy Markdown
Member

hi, thanks for opening a pull request! a few comments:

  • your PR currently targets the jazzy branch. it should target rolling and be backported to jazzy unless it's a fix specific to the jazzy branch not applicable to other ROS distros.
  • we plan to deprecate the experimental events executor anyway in favor of the EventsCBGExecutor, which as of the last sync, has been backported to Jazzy. The implementation internals differ quite a bit from the experimental prototype and so I would recommend seeing if you still run into these performance issues with the new executor first, and targeting your fixes there if still applicable.

@adi213-glitch

Copy link
Copy Markdown
Author

Hi @skyegalaxy, thank you for the review and for pointing out the architectural roadmap!

That makes complete sense. I wasn't aware the experimental events executor was slated for deprecation in favor of EventsCBGExecutor. I will close this PR and pivot my focus to the rolling branch. I'm going to set up a benchmark rig for the new EventsCBGExecutor to audit its internal collection updates and see if any similar performance issues pop up there.

Appreciate the guidance on targeting rolling first. I'll make sure any future optimizations are directed there and tagged for backporting if applicable.

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.

3 participants