1
$\begingroup$

I hit a performance issue when trying to use a custom message type in ROS 1 Noetic in Python. My custom message was defined like this:

# Grasp.msg
Header header
geometry_msgs/Pose pose
float32 confidence
float32 width

And an array of these messages was defined like this:

# Grasps.msg
Header header
Grasp[] grasps

Creating ~10k of these messages was taking around 0.25 seconds.

When I went to profile my code, I realized that the majority of the time (>80%) was spent creating the empty message. If I write an equivalent class/dataclass to store my message data, the overhead is not nearly this high (~20% spent creating the class) and the speed is several times faster.

Other message types (non-custom) take similarly long to generate:

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
    23                                           def create_and_fill_msg():
    24     10001    1039578.0    103.9      4.4      for _ in range(10000):
    25     10000   12915914.0   1291.6     54.4          pose = Pose()
    26     10000    1573284.0    157.3      6.6          pose.position.x = 0
    27     10000    1304890.0    130.5      5.5          pose.position.y = 0
    28     10000    1533402.0    153.3      6.5          pose.position.z = 0
    29     10000    1412335.0    141.2      5.9          pose.orientation.x = 0
    30     10000    1374551.0    137.5      5.8          pose.orientation.y = 0
    31     10000    1363287.0    136.3      5.7          pose.orientation.z = 0
    32     10000    1222556.0    122.3      5.1          pose.orientation.w = 0

I'm curious why this is happening and if there is a way around the issue, either in how I define or how I fill my messages.

$\endgroup$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.