From 45ec296ba0cdd98f79a7a47e05b7abaf07b01892 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Tue, 8 Feb 2005 05:08:47 +0000 Subject: [PATCH] Here's a giant performance improvement: the current Presents architecture flattens messages into a buffer and then passes that bucket to a SocketChannel.write() method which is part of the NIO business. If said buffer is a "direct" buffer, the write() method will in theory do high-performance shit like DMA the data right to the network card. If it's not a direct buffer, Sun apparently makes a temporary direct buffer, copies the data into it and passes that on to the underlying socket send() call. We weren't using direct buffers which means that we were copying everything one more time than needed (not a huge deal) and that we were allocating a direct buffer for every message (a much bigger deal). This should take a serious load off of the I/O thread and fortunately we can test it on Ice to make sure it doesn't do anything super crazy. All this said, this whole business is going to change when I rearchitect Presents to avoid the potential race conditions it suffers from now and we won't be able to use a single direct buffer to write all of our outgoing messages, but I believe we will be able to use a pool of direct buffers with one used by every message in the queue waiting to be written (hopefully that won't be too many at any given time) which we can keep around to avoid the expense of allocating and freeing direct buffers. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3336 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- src/java/com/threerings/io/FramingOutputStream.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/java/com/threerings/io/FramingOutputStream.java b/src/java/com/threerings/io/FramingOutputStream.java index b1d9c0ecd..a7e4c8119 100644 --- a/src/java/com/threerings/io/FramingOutputStream.java +++ b/src/java/com/threerings/io/FramingOutputStream.java @@ -44,7 +44,7 @@ public class FramingOutputStream extends OutputStream { public FramingOutputStream () { - _buffer = ByteBuffer.allocate(INITIAL_BUFFER_SIZE); + _buffer = ByteBuffer.allocateDirect(INITIAL_BUFFER_SIZE); _buffer.put(HEADER_PAD); } @@ -99,7 +99,7 @@ public class FramingOutputStream extends OutputStream if (ncapacity > ocapacity) { // increase the buffer size in large increments ncapacity = Math.max(ocapacity << 1, ncapacity); - ByteBuffer newbuf = ByteBuffer.allocate(ncapacity); + ByteBuffer newbuf = ByteBuffer.allocateDirect(ncapacity); newbuf.put((ByteBuffer)_buffer.flip()); _buffer = newbuf; }