[zeromq-dev] Zero-copy message API

Pieter Hintjens ph at imatix.com
Sun Apr 3 17:50:51 CEST 2011


On Sun, Apr 3, 2011 at 5:34 PM, Martin Sustrik <sustrik at 250bpm.com> wrote:

>> zmq_msg_t msg;
>> zmq_msg_init (&msg, 0);  // instead of zmq_msg_init (&msg);
>> zmq_recvmsg (s,&msg, 0);
>>
>> It does make receiving a message slightly weird, but it gives a single
>> 'normal' init method, which is worth a lot IMO.
>
> I am not against. The less functions the better. Let's see what others say.

If we make zero-copy an exceptional case, we can simplify the API
further, using a style like this:

zmq_msg_t *msg = zmq_msg_new ();
zmq_msg_set (msg, body, size);
zmq_sendmsg (&msg);  // Destroys message

and

zmq_msg_t *msg = zmq_recvmsg (socket, 0);
...
zmq_msg_destroy (&msg);

This API style has worked well for C code elsewhere (like libzapi). It
works since we don't need to hold messages on the stack, only pointers
to messages. It's nicer to pass pointers than addresses to structures.
To do zero-copy we can then make it look more like the old API:

zmq_msg_t msg;
// some method that means 'initialize for zero-copy and asynchronous
destruction'
zmq_msg_blargh (&msg, body, size, freefn);
zmq_sendmsg (&msg);
...

-Pieter



More information about the zeromq-dev mailing list