slow filling of uint8 data field from python
I have found this issue in both ros2 eloquent and foxy. When filling an Image message or a PointCloud2 message from a numpy.array, filling the data field is extremely slow.
For instance, with the point cloud message:
cloud_msg = PointCloud2()
cloud_msg.data = cloud_arr.tostring() # cloud_arr is my point cloud as numpy array
The tostring() itself is fast, but the assignment to cloud_msg.data is what takes time. I cannot get the publishing rate over 1 Hz because of how much time is spent in filling the message.
What should I do to publish point clouds in python at a decent rate?
EDIT: I managed to get it a bit faster by setting PYTHONOPTIMIZE to 1. I noticed that the point cloud filling was performing a lot of extra checks if __debug__ was set to True. This allows me to get the message filling down from 0.4 seconds to 0.002. Now the publish() part is the slowest, taking 0.04 seconds. Is there any chance of improving this as well, or is this as fast as it can get?