Skip to content

__debug__ flag slows down image message creation #79

Closed
@mlix11

Description

@mlix11

Bug report

Required Info:

  • Operating System:
    • Ubuntu 18.04
  • Installation type:
    • Debian package
  • Version or commit hash:
    • dashing
  • DDS implementation:
    • default
  • Client library (if applicable):
    • rclpy

Steps to reproduce issue

from time import time
import numpy as np
from cv_bridge import CvBridge

# initialize cv_bridge
bridge = CvBridge()
# generate fake image with numpy
fake_image = np.random.randint(254, size=(1080, 1920, 3), dtype=np.uint8)
print(f"Fake image size: {fake_image.nbytes / 1048576}MB")

for i in range(10):
    n = time()
    bridge.cv2_to_imgmsg(fake_image)
    print(f"{time()-n}s")

Expected behavior

Output:
Fake image size: 5.9326171875MB
0.04487729072570801s
0.007574319839477539s
0.0013623237609863281s
0.001272439956665039s
0.0012216567993164062s
0.0011925697326660156s
0.0012187957763671875s
0.0012259483337402344s
0.0011894702911376953s
0.0010957717895507812s

Actual behavior

Output:
Fake image size: 5.9326171875MB
1.4624004364013672s
1.4479649066925049s
1.45872163772583s
1.4582252502441406s
1.4557397365570068s
1.4485929012298584s
1.4805355072021484s
1.5637807846069336s
1.5311927795410156s
1.6195440292358398s

Additional information

I experienced a slow performance during a cv2_to_imgmsg call. The interesting part is, that the call in C++ is fast. Probably it has to do something with the python message wrapper. Did someone experienced also a slow down with cv2_to_imgmsg calls?
Right now a 1080x1920x3 image takes up 1.4s to convert to a ROS2 image message.

The slow down is happening at this line: https://github.com/ros-perception/vision_opencv/blob/ros2/cv_bridge/python/cv_bridge/core.py#L275
and the root cause is at this line: https://github.com/ros2/rosidl_python/blob/master/rosidl_generator_py/resource/_msg.py.em#L481

Feature request

Deactivate the per-element comparison for images or use another approach then the
__debug__ flag from python. This flag is always on and can be only set to false with PYTHONOPTIMIZE={1|2} (for ROS2) or for python (-O) (-OO). Did not found a documentation for this behavior in the ROS2 documentation. With this flag the __debug__ field is going to be false and everything is running smoothly.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions