Re: NIO not so hot
On Sunday, June 1, 2014 11:15:01 AM UTC+1, Robert Klemme wrote:
Can you point me to writing about that internal byte buffer pool in the
JRE? I could not find anything.
Take a look here:
http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/sun/nio/ch/IOUtil.java#IOUtil.read%28java.io.FileDescriptor%2Cjava.nio.ByteBuffer%2Clong%2Csun.nio.ch.NativeDispatcher%2Cjava.lang.Object%29
Line 179.
You can see:
179 static int read(FileDescriptor fd, ByteBuffer dst, long position,
180 NativeDispatcher nd, Object lock)
181 throws IOException
182 {
183 if (dst.isReadOnly())
184 throw new IllegalArgumentException("Read-only buffer");
185 if (dst instanceof DirectBuffer)
186 return readIntoNativeBuffer(fd, dst, position, nd, lock);
187
188 // Substitute a native buffer
189 ByteBuffer bb = Util.getTemporaryDirectBuffer(dst.remaining());
190 try {
191 int n = readIntoNativeBuffer(fd, bb, position, nd, lock);
192 bb.flip();
193 if (n > 0)
194 dst.put(bb);
195 return n;
196 } finally {
197 Util.offerFirstTemporaryDirectBuffer(bb);
198 }
199 }
So when using a heap buffer, a temporary direct buffer is taken from a pool, read into, then the data is copied into the heap buffer.
Many benchmarks will do:
time this {
ByteBuffer.allocateDirect();
// Read some data into the buffer
}
time this {
ByteBuffer.allocate();
// Read some data into the buffer
}
And come to the conclusion that heap buffers are faster. But now we know that every heap buffer IO operation uses a direct buffer under the covers, how can heap buffer IO operations be faster?
If we do the pooling ourselves, we can find that direct buffers are faster.
Rupert