Zero-Copy
ÃMQâs message API lets you send and receive messages directly to and from application buffers without copying data. We call it zero-copy, and it can improve performance in some applications. Like all optimizations, use this when you know it helps, and measure before and after. Zero-copy makes your code more complex.
To do zero-copy, you use zmq_msg_init_data()
to create a message that
refers to a block of data already allocated on the heap with malloc()
, and then you pass that to zmq_msg_send()
. When you create the message, you
also pass a function that ÃMQ will call to free the block of data, when it
has finished sending the message. This is the simplest example, assuming
âbufferâ is a block of 1,000 bytes allocated on the heap:
void
my_free
(
void
*
data
,
void
*
hint
)
{
free
(
data
);
}
// Send message from buffer, which we allocate and 0MQ will free for us
zmq_msg_t
message
;
zmq_msg_init_data
(
&
message
,
buffer
,
1000
,
my_free
,
NULL
);
zmq_msg_send
(
socket
,
&
message
,
0
);
There is no way to do zero-copy on receive: ÃMQ delivers you a buffer that you can store as long as you wish, but it will not write data directly into application buffers.
On writing, ÃMQâs multipart messages work nicely together with zero-copy. In traditional messaging, you need to marshal different buffers together into one buffer that you can send. That means copying data. With ÃMQ, you can send multiple buffers coming from different sources as individual message frames. Send each field as a length-delimited ...
Get ZeroMQ now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.