3

I have the following code:

while (!isCancellationRequested)
{
    byte[] frameBytes = socket.ReceiveFrameBytes();
    MyData data = new MyData();
    data.Deserialize(frameBytes);
    // send the data somewhere somehow...
}

What I want to do, but I can't find in the documentation is something that will avoid the creation of new byte array on each socket.Receive:

byte[] frameBytes = new byte[123];
while (!isCancellationRequested)
{
    socket.ReceiveFrameBytes(frameBytes);
    MyData data = new MyData();
    data.Deserialize(frameBytes);
    // send the data somewhere somehow...
}

How can I do that?

Ceco
  • 1,586
  • 3
  • 16
  • 23

1 Answers1

5

To get high performance and zero allocation with NetMQ you need to both enable the BufferPool and use the Msg structure directly.

BufferPool.SetBufferManagerBufferPool(1024 * 1024, 1024);    

while (!isCancellationRequested)
{
    Msg msg = new Msg();
    msg.InitEmpty();
    socket.Receive(ref msg);
    MyData data = new MyData();
    data.Deserialize(msg.Data, msg.Size);
    msg.Close();
    // send the data somewhere somehow...
}
somdoron
  • 4,653
  • 2
  • 16
  • 24