问题描述
在将对象写入网络时,Netty是否提供任何方法来避免缓冲区复制?根据我的理解,在其Channel.write(Object)
中,使该对象通过编码器(该编码器返回ChannelBuffer副本).基本上,这意味着我最终为每条消息创建一个缓冲区副本,最终创建了大量的短期缓冲区对象
Does Netty provide any way for me to avoid buffer copying when I am writing objects to network ? In its Channel.write(Object)
, in my understanding, this object is made to pass through encoders ( which return ChannelBuffer copies ). This basically means for each message I end up creating a buffer copy , eventually creating large number of short lived buffer objects
Netty可以为我提供一个可重复使用的ChannelBuffer,我用字节填充它并将这些字节直接写到网络上吗?
Can Netty provide me with a reusable ChannelBuffer that I fill up with bytes and it writes these bytes to network directly ?
推荐答案
Netty已经回收了缓冲区,您无需担心.
Netty already recycles buffers, you don't need to care about this.
此外,编码器/解码器只是您自己编写的编码器,Netty仅能处理 ChannelBuffer 对象,如果您将ChannelBuffer写入您的通道中,那么它周围就没有魔法了,它的内容是直接写到输出中.
Also, the encoders/decoders are only the ones you wrote yourself, Netty can only handle ChannelBuffer objects, if you are writing a ChannelBuffer to your channel there is no magic around it, it's contents are written directly to the output.
您可以(并且应该)做的事情是创建与缓冲区大小完全相同的缓冲区,以免在向缓冲区加载数据时分配动态缓冲区.
What you can (and should) do is to create buffers of the exact size you need to avoid allocation of dynamic buffers when you are loading the buffer with data.
这篇关于使用Netty进行书写的有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!