Skip to content

Commit

Permalink
Use memcpy instead of std::copy when bridging images (#565) (#585)
Browse files Browse the repository at this point in the history
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)

Co-authored-by: Carlos Agüero <[email protected]>
  • Loading branch information
mergify[bot] and caguero authored Aug 1, 2024
1 parent c8e5742 commit 7314ab4
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions ros_gz_bridge/src/convert/sensor_msgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,11 @@ convert_gz_to_ros(

ros_msg.is_bigendian = false;
ros_msg.step = ros_msg.width * num_channels * octets_per_channel;

auto count = ros_msg.step * ros_msg.height;
ros_msg.data.resize(ros_msg.step * ros_msg.height);
std::copy(
gz_msg.data().begin(),
gz_msg.data().begin() + count,
ros_msg.data.begin());

// Prefer memcpy over std::copy for performance reasons,
// see https://github.com/gazebosim/ros_gz/pull/565
memcpy(ros_msg.data.data(), gz_msg.data().c_str(), gz_msg.data().size());
}

template<>
Expand Down

0 comments on commit 7314ab4

Please sign in to comment.