什么是NetMQ API在自定义缓冲区中接收以避免在每个帧/消息上创建新的字节数组?
问题描述:
我有以下代码:什么是NetMQ API在自定义缓冲区中接收以避免在每个帧/消息上创建新的字节数组?
while (!isCancellationRequested)
{
byte[] frameBytes = socket.ReceiveFrameBytes();
MyData data = new MyData();
data.Deserialize(frameBytes);
// send the data somewhere somehow...
}
我想做的事情,但我不能在文档中发现的东西,将避免各socket.Receive
新的字节数组的创建:
byte[] frameBytes = new byte[123];
while (!isCancellationRequested)
{
socket.ReceiveFrameBytes(frameBytes);
MyData data = new MyData();
data.Deserialize(frameBytes);
// send the data somewhere somehow...
}
我该怎么做?
答
要使用NetMQ获得高性能和零分配,您需要启用BufferPool并直接使用Msg结构。
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...
}