[英]A simple rule of when I should use direct buffers with Java NIO for network I/O?
Can someone with the natural gift to explain complex things in an easy and straightforward way address this question? 有天赋的人能以简单明了的方式解释复杂的事情吗? To acquire the best performance when should I use direct ByteBuffers versus regular ByteBuffers when doing network I/O with Java NIO?
为了在使用Java NIO进行网络I / O时应该使用直接ByteBuffer而不是常规ByteBuffer来获得最佳性能?
For example: Should I read into a heap buffer and parse it from there, doing many get() (byte by byte) OR should I read it into a direct buffer and parse from the direct buffer? 例如:我应该读入堆缓冲区并从那里解析它,做很多get()(逐字节),还是应该读入直接缓冲区并从直接缓冲区进行解析?
To acquire the best performance when should I use direct ByteBuffers versus regular ByteBuffers when doing network I/O with Java NIO?
为了在使用Java NIO进行网络I / O时应该使用直接ByteBuffer而不是常规ByteBuffer来获得最佳性能?
Direct buffers have a number of advantages 直接缓冲区具有许多优点
Should I read into a heap buffer and parse it from there, doing many get() (byte by byte) OR should I read it into a direct buffer and parse from the direct buffer?
我应该读入堆缓冲区并从那里解析它,做很多get()(逐字节),还是应该读入直接缓冲区并从直接缓冲区进行解析?
If you are reading a byte at a time, you may not get much advantage. 如果您一次读取一个字节,则可能不会获得太多优势。 However, with a direct byte buffer you can read 2 or 4 bytes at a time and effectively parse multiple bytes at once.
但是,使用直接字节缓冲区,您一次可以读取2或4个字节,并且可以一次有效地解析多个字节。
[real time] [selectors]
[实时] [选择器]
If you are parsing real time data, I would avoid using selectors. 如果要解析实时数据,则应避免使用选择器。 I have found using blocking NIO or busy waiting NIO can give you the lowest latency performance (assuming you have a relatively small number of connections eg up to 20)
我发现使用阻塞NIO或忙于等待NIO可以为您提供最低的延迟性能(假设您的连接数量相对较少,例如最多20个)
A direct buffer is best when you are just copying the data, say from a socket to a file or vice versa, as the data doesn't have to traverse the JNI/Java boundary, it just stays in JNI land. 当您只是将数据复制(例如,从套接字复制到文件, 反之亦然)时,直接缓冲区是最好的选择,因为数据不必遍历JNI / Java边界,它只保留在JNI范围内。 If you are planning to look at the data yourself there's no point in a direct buffer.
如果您打算自己查看数据,则直接缓冲区没有意义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.