Skip to content
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

Use memcpy instead of std::copy when bridging images (backport #565) #585

Merged
merged 1 commit into from
Aug 1, 2024

Conversation

mergify[bot]
Copy link
Contributor

@mergify mergify bot commented Aug 1, 2024

🦟 Optimization

Summary

While testing ros <-> gz communication using the bridge I noticed that the bridge was talking quite a bit of time copying images from Gazebo to ROS. I found that the std::copy operation that we're doing is substantially slower than the memcpy alternative. I think that in principle this shouldn't happen but the numbers are quite clear. Perhaps std::copy is doing something that doesn't use cache effectively...

How to test it?

First, modify this code to see some stats:

auto beg = std::chrono::high_resolution_clock::now();
// Option 1: Using memcpy
// memcpy(ros_msg.data.data(), gz_msg.data().c_str(), gz_msg.data().size());

// Option 2: Using std::copy
auto count = ros_msg.step * ros_msg.height;
std::copy(
  gz_msg.data().begin(),
  gz_msg.data().begin() + count,
  ros_msg.data.begin());

auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - beg);
std::cerr << "Elapsed Time: " << duration.count() << " us" << std::endl; 

Recompile and launch one of our examples that publish 320x240 images:

ros2 launch ros_gz_sim_demos gpu_lidar_bridge.launch.py

The default code shows:

[parameter_bridge-2] Elapsed Time: 548 us

Enable the memcpy, comment the old code and relaunch the example again. The new code shows:

[parameter_bridge-2] Elapsed Time: 11 us

Checklist

  • Signed all commits for DCO
  • Added tests
  • Updated documentation (as needed)
  • Updated migration guide (as needed)
  • Consider updating Python bindings (if the library has them)
  • codecheck passed (See contributing)
  • All tests passed (See test coverage)
  • While waiting for a review on your PR, please help review another open pull request to support the maintainers

Note to maintainers: Remember to use Squash-Merge and edit the commit message to match the pull request summary while retaining Signed-off-by messages.


This is an automatic backport of pull request #565 done by Mergify.

While testing ros <-> gz communication using the bridge I noticed that the bridge was talking quite a bit of time copying images from Gazebo to ROS. I found that the std::copy operation that we're doing is substantially slower than the memcpy alternative. I think that in principle this shouldn't happen but the numbers are quite clear. Perhaps std::copy is doing something that doesn't use cache effectively
---------

Signed-off-by: Carlos Agüero <[email protected]>
Signed-off-by: Carlos Agüero <[email protected]>
Co-authored-by: Jose Luis Rivero <[email protected]>
(cherry picked from commit a781b78)
@mergify mergify bot requested a review from ahcorde as a code owner August 1, 2024 09:57
@ahcorde ahcorde merged commit 7314ab4 into jazzy Aug 1, 2024
5 checks passed
@ahcorde ahcorde deleted the mergify/bp/jazzy/pr-565 branch August 1, 2024 13:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants