Slashdot Mirror


Java IO Faster Than NIO

rsk writes "Paul Tyma, the man behind Mailinator, has put together an excellent performance analysis comparing old-school synchronous programming (java.io.*) to Java's asynchronous programming (java.nio.*) — showing a consistent 25% performance deficiency with the asynchronous code. As it turns out, old-style blocking I/O with modern threading libraries like Linux NPTL and multi-core machines gives you idle-thread and non-contending thread management for an extremely low cost; less than it takes to switch-and-restore connection state constantly with a selector approach."

4 of 270 comments (clear)

  1. Re:Old news. by bill_kress · · Score: 4, Interesting

    I had a problem where the customer wanted to discover a class-b network in a reasonable amount of time.

    Aside from Java's lack of ping causing huge heartaches the limitation was that when using old Java IO it allocated a thread per connection while waiting for a response.

    This limited me to 2-4000 outstanding connection attempts at any time. Since most didn't connect, I needed at least 3 retries on each with progressive back-off times--the threads were absolutely the bottleneck.

    I reduced the time for this discovery process from days (or the machine just locked up) to 15 minutes. With nio I probably could have reduced it significantly more (although at some point packet collisions would have become problematic).

    NIO may not be defective, it just may be solving a problem you haven't conceived of.

  2. True for JAVA, but not generally true... by grmoc · · Score: 4, Interesting

    This may be true for Java.
    It isn't true for C/C++.

    With C/C++ and NPTL, the many-thread blocking IO style yields slightly lower latency at low IO rates, but offers significant latency variability and sharply decreased thruput at higher IO rates.
    It seems that the linux scheduler is much to blame for this-- the number of times that a thread is scheduled on a different CPU increases dramatically with more threads, and this trashes the caches.
    I've seen order-of-magnitude decreases in performance and order-of-magnitude increases in latency as a result of what appears to be the cache trashing.

    1. Re:True for JAVA, but not generally true... by grmoc · · Score: 4, Interesting

      Unfortunately, nothing I can publish without permission.
      I can say that I'm in charge of maintaining the software that terminates all HTTP traffic for Google. Draw your own conclusions.

  3. Re:Should be using Scatter/Gather +IOCP on windows by dr2chase · · Score: 4, Interesting

    I'm afraid I have to disagree. No fan of Microsoft, but I helped build a the-Java-Programming-Language-TM Virtual Machine on Windows, with M:N threads, back before Java 1.4, and IO Completion ports worked well, and we got good performance out of them. We rewrote the network IO to work behind the curtain with threads, with the result that the one-socket-per-thread model actually did the I/O completion port thing, with as many as 32k Java threads running in a grand total of about a dozen Windows threads (stacks were small, stacks grew on demand. Certain things were tricky.).

    The largest wins of doing it this way were:

    1) got to use the underlying OS's preferred way of doing async IO (on another OS, we might do it differently)
    2) lots of threads allowed
    3) because Java "context switches" were extremely lightweight, lots of "expensive" stuff got faster (e.g., lock contention).

    I also accidentally (really -- I had to choose one of two threads to go first, and chose the right one, on a whim) built-in an anti-convoying heuristic for contended locks, that was really useful when code contained a hot lock.

    But, the rest of the system was not especially Microsoft-y; all of us came form a Unix background, and when we were done, we did Unix again. IO Completion ports, at least one Windows, were the best choice (and I tried it 2 or 3 other ways, and they sucked).