Reputation: 1067
I am working on a native application which uses the cppZmq binding. My messaging broker reads messages off a ZMQ_ROUTER and forwards them to another ZMQ_ROUTER after applying some load balancing logic. I use a message_t class to recv data (which internally just calls zmq_recv with a zmq_msg_t structure).
In my polling loop, can I use a message_t variable (stack allocated outside the loop) to receive a message, forward to the socket, and then use the same message_t variable to receive another message in the next iteration of the loop? Or, should I stack allocate the message_t variable INSIDE the polling loop; In other words, is it essential to zmq_close a message, zmq_msg_init it again before doing a zmq_recv with the same zmq_msg_t variable? This might not have measurable performance considerations, but the objective is to get things right to avoid bugs later on in the development cycle.
Also, can I take a zmq_msg_t structure from one socket and forward that to another socket without creating a new message structure, and doing zmq_msg_copy on it?
Upvotes: 0
Views: 835
Reputation: 1216
My strong advice is to get a working solution first before you try to optimise it. These days, memory allocators are pretty efficient; when you come to profile your application, memory allocation probably won't even register. However, if memory optimisation should be desirable you'll be able to post some working code which will then be much easier to comment on. [It is very difficult to visualise code from textual description of it.]
Upvotes: 1