Skip to content

Achieving true zero-copy with Iceoryx (Shared Memory) #294

@fabiannagel

Description

@fabiannagel

Hi everyone, I've been trying to use this library with CycloneDDS and Iceoryx. I built both libraries from source and can verify everything works from examples & benchmarks.

However, I'm struggling to achieve true zero-copy for large messages (raw images; ~5 MiB/image, 24 Hz). This is my sample IDL:

module TestMessage {
    struct CameraImage {
        unsigned long width;
        unsigned long height;
        sequence<octet> image_data;
    };
};

Given a numpy array, I can use arr.to_bytes() to publish the message. However, it takes ~350 ms to do so:

msg_instance = CameraImage_type(
     width=arr.shape[1],
     height=arr.shape[0],
     image_data=arr.to_bytes()
)
self.writer.write(msg_instance)

Using np.ascontiguousarray() and memoryview, I can get it down to ~0.1 ms:

contiguous_arr = np.ascontiguousarray(image_np)
c_buffer = memoryview(contiguous_arr.data)

msg_instance = CameraImage_type(
    width=contiguous_arr.shape[1],
    height=contiguous_arr.shape[0],
    image_data=c_buffer
)
self.writer.write(msg_instance)

BUT it quickly fails (~30 calls) with this error:

Failed to encode member image_data, value is <memory at 0x72118cf35990>

I was guessing that Python garbage collection is not in tune with CycloneDDS & Iceoryx. So I tried keeping explicit references to the last 1000 references to both contiguous_arr and c_buffer in a collection.deque() - without success.

Is there another way of making this work?

Best
Fabian

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions