From 7e362277b93bbf4d59fd80485f3fe534a3ed8bba Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Tue, 22 Jul 2014 13:28:02 -0700 Subject: [PATCH] Reduce the default initial capacity of ChannelOutboundBuffer Motivation: ChannelOutboundBuffer is basically a circular array queue of its entry objects. Once an entry is created in the array, it is never nulled out to reduce the allocation cost. However, because it is a circular queue, the array almost always ends up with as many entry instances as the size of the array, regardless of the number of pending writes. At worst case, a channel might have only 1 pending writes at maximum while creating 32 entry objects, where 32 is the initial capacity of the array. Modifications: - Reduce the initial capacity of the circular array queue to 4. - Make the initial capacity of the circular array queue configurable Result: We spend 4 times less memory for entry objects under certain circumstances. --- .../java/io/netty/channel/ChannelOutboundBuffer.java | 10 +++++++++- .../socket/nio/NioSocketChannelOutboundBufferTest.java | 6 +++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/transport/src/main/java/io/netty/channel/ChannelOutboundBuffer.java b/transport/src/main/java/io/netty/channel/ChannelOutboundBuffer.java index 06cd3a3f50..6c1139cc6b 100644 --- a/transport/src/main/java/io/netty/channel/ChannelOutboundBuffer.java +++ b/transport/src/main/java/io/netty/channel/ChannelOutboundBuffer.java @@ -27,6 +27,7 @@ import io.netty.util.Recycler; import io.netty.util.Recycler.Handle; import io.netty.util.ReferenceCountUtil; import io.netty.util.internal.PlatformDependent; +import io.netty.util.internal.SystemPropertyUtil; import io.netty.util.internal.logging.InternalLogger; import io.netty.util.internal.logging.InternalLoggerFactory; @@ -42,7 +43,14 @@ public class ChannelOutboundBuffer { private static final InternalLogger logger = InternalLoggerFactory.getInstance(ChannelOutboundBuffer.class); - protected static final int INITIAL_CAPACITY = 32; + protected static final int INITIAL_CAPACITY = + SystemPropertyUtil.getInt("io.netty.outboundBufferInitialCapacity", 4); + + static { + if (logger.isDebugEnabled()) { + logger.debug("-Dio.netty.outboundBufferInitialCapacity: {}", INITIAL_CAPACITY); + } + } private static final Recycler RECYCLER = new Recycler() { @Override diff --git a/transport/src/test/java/io/netty/channel/socket/nio/NioSocketChannelOutboundBufferTest.java b/transport/src/test/java/io/netty/channel/socket/nio/NioSocketChannelOutboundBufferTest.java index 7f825d4b0e..d42100ceb6 100644 --- a/transport/src/test/java/io/netty/channel/socket/nio/NioSocketChannelOutboundBufferTest.java +++ b/transport/src/test/java/io/netty/channel/socket/nio/NioSocketChannelOutboundBufferTest.java @@ -36,7 +36,7 @@ public class NioSocketChannelOutboundBufferTest { NioSocketChannelOutboundBuffer buffer = NioSocketChannelOutboundBuffer.newInstance(channel); assertEquals(0, buffer.nioBufferCount()); ByteBuffer[] buffers = buffer.nioBuffers(); - assertEquals(32, buffers.length); + assertNotNull(buffers); for (ByteBuffer b: buffers) { assertNull(b); } @@ -50,7 +50,7 @@ public class NioSocketChannelOutboundBufferTest { NioSocketChannelOutboundBuffer buffer = NioSocketChannelOutboundBuffer.newInstance(channel); assertEquals(0, buffer.nioBufferCount()); ByteBuffer[] buffers = buffer.nioBuffers(); - assertEquals(32, buffers.length); + assertNotNull(buffers); for (ByteBuffer b: buffers) { assertNull(b); } @@ -66,7 +66,7 @@ public class NioSocketChannelOutboundBufferTest { } buffer.addFlush(); buffers = buffer.nioBuffers(); - assertEquals(32, buffers.length); + assertNotNull(buffers); assertEquals("Should still be 0 as not flushed yet", 1, buffer.nioBufferCount()); for (int i = 0; i < buffers.length; i++) { if (i == 0) {