Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #28633

Re: NIO not so hot

Newsgroups comp.lang.java.programmer
Date 2014-06-02 07:46 -0700
References <t24en9putph3mbnulkveghdjbrnisp105h@4ax.com> <0f77f8c8-374b-4c3a-a915-2beda6b3e74e@googlegroups.com> <bv0cp7FcakkU1@mid.individual.net>
Message-ID <6da74606-4802-4407-b160-078891edce83@googlegroups.com> (permalink)
Subject Re: NIO not so hot
From Rupert Smith <rupertlssmith@googlemail.com>

Show all headers | View raw


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

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

NIO not so hot Roedy Green <see_website@mindprod.com.invalid> - 2014-05-17 04:28 -0700
  Re: NIO not so hot Robert Klemme <shortcutter@googlemail.com> - 2014-05-18 13:38 +0200
    Re: NIO not so hot Robert Klemme <shortcutter@googlemail.com> - 2014-05-18 13:38 +0200
      Re: NIO not so hot Robert Klemme <shortcutter@googlemail.com> - 2014-05-18 14:12 +0200
        Re: NIO not so hot Robert Klemme <shortcutter@googlemail.com> - 2014-05-18 19:09 +0200
          Re: NIO not so hot Roedy Green <see_website@mindprod.com.invalid> - 2014-05-26 00:51 -0700
            Re: NIO not so hot Robert Klemme <shortcutter@googlemail.com> - 2014-05-26 20:14 +0200
    Re: NIO not so hot Roedy Green <see_website@mindprod.com.invalid> - 2014-05-26 01:15 -0700
    Re: NIO not so hot Roedy Green <see_website@mindprod.com.invalid> - 2014-05-26 01:24 -0700
      Re: NIO not so hot Robert Klemme <shortcutter@googlemail.com> - 2014-05-26 23:50 +0200
  Re: NIO not so hot Rupert Smith <rupertlssmith@googlemail.com> - 2014-05-31 02:29 -0700
    Re: NIO not so hot Robert Klemme <shortcutter@googlemail.com> - 2014-06-01 12:15 +0200
      Re: NIO not so hot Rupert Smith <rupertlssmith@googlemail.com> - 2014-06-02 07:37 -0700
        Re: NIO not so hot Robert Klemme <shortcutter@googlemail.com> - 2014-06-03 23:07 +0200
          Re: NIO not so hot Rupert Smith <rupertlssmith@googlemail.com> - 2014-06-06 01:32 -0700
      Re: NIO not so hot Rupert Smith <rupertlssmith@googlemail.com> - 2014-06-02 07:46 -0700

csiph-web