* Made the preallocated buffer capacity configurable

* Added an integer getter for SystemPropertyUtil
This commit is contained in:
Trustin Lee 2010-02-18 02:12:15 +00:00
parent f650a8bcee
commit a58c7c4364
3 changed files with 50 additions and 6 deletions

View File

@ -18,6 +18,9 @@ package org.jboss.netty.channel.socket.nio;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.logging.InternalLogger;
import org.jboss.netty.logging.InternalLoggerFactory;
import org.jboss.netty.util.internal.SystemPropertyUtil;
/** /**
* @author <a href="http://www.jboss.org/netty/">The Netty Project</a> * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
@ -26,7 +29,25 @@ import org.jboss.netty.buffer.ChannelBuffer;
*/ */
final class DirectBufferPool { final class DirectBufferPool {
private static final int preallocatedBufferCapacity = 128 * 1024; private static final InternalLogger logger =
InternalLoggerFactory.getInstance(DirectBufferPool.class);
private static final int CAPACITY;
static {
int val = SystemPropertyUtil.get(
"org.jboss.netty.channel.socket.nio.preallocatedBufferCapacity",
0);
if (val <= 0) {
val = 128 * 1024;
} else {
logger.debug(
"Using the specified preallocated buffer capacity: " + val);
}
CAPACITY = val;
}
private ByteBuffer preallocatedBuffer; private ByteBuffer preallocatedBuffer;
@ -45,7 +66,7 @@ final class DirectBufferPool {
final ByteBuffer acquire(int size) { final ByteBuffer acquire(int size) {
ByteBuffer preallocatedBuffer = this.preallocatedBuffer; ByteBuffer preallocatedBuffer = this.preallocatedBuffer;
if (preallocatedBuffer == null) { if (preallocatedBuffer == null) {
if (size < preallocatedBufferCapacity) { if (size < CAPACITY) {
return preallocateAndAcquire(size); return preallocateAndAcquire(size);
} else { } else {
return ByteBuffer.allocateDirect(size); return ByteBuffer.allocateDirect(size);
@ -53,7 +74,7 @@ final class DirectBufferPool {
} }
if (preallocatedBuffer.remaining() < size) { if (preallocatedBuffer.remaining() < size) {
if (size > preallocatedBufferCapacity) { if (size > CAPACITY) {
return ByteBuffer.allocateDirect(size); return ByteBuffer.allocateDirect(size);
} else { } else {
return preallocateAndAcquire(size); return preallocateAndAcquire(size);
@ -69,7 +90,7 @@ final class DirectBufferPool {
private final ByteBuffer preallocateAndAcquire(int size) { private final ByteBuffer preallocateAndAcquire(int size) {
ByteBuffer preallocatedBuffer = this.preallocatedBuffer = ByteBuffer preallocatedBuffer = this.preallocatedBuffer =
ByteBuffer.allocateDirect(preallocatedBufferCapacity); ByteBuffer.allocateDirect(CAPACITY);
ByteBuffer x = preallocatedBuffer.duplicate(); ByteBuffer x = preallocatedBuffer.duplicate();
x.limit(size); x.limit(size);
preallocatedBuffer.position(size); preallocatedBuffer.position(size);

View File

@ -62,8 +62,7 @@ class NioProviderMetadata {
// Use the system property if possible. // Use the system property if possible.
try { try {
String value = SystemPropertyUtil.get(CONSTRAINT_LEVEL_PROPERTY, "-1"); constraintLevel = SystemPropertyUtil.get(CONSTRAINT_LEVEL_PROPERTY, -1);
constraintLevel = Integer.parseInt(value);
if (constraintLevel < 0 || constraintLevel > 2) { if (constraintLevel < 0 || constraintLevel > 2) {
constraintLevel = -1; constraintLevel = -1;
} else { } else {

View File

@ -15,6 +15,8 @@
*/ */
package org.jboss.netty.util.internal; package org.jboss.netty.util.internal;
import java.util.regex.Pattern;
/** /**
* Accesses the system property swallowing a {@link SecurityException}. * Accesses the system property swallowing a {@link SecurityException}.
* *
@ -59,6 +61,28 @@ public class SystemPropertyUtil {
return value; return value;
} }
/**
* Returns the value of the Java system property with the specified
* {@code key}, while falling back to the specified default value if
* the property access fails.
*
* @return the property value.
* {@code def} if there's no such property or if an access to the
* specified property is not allowed.
*/
public static int get(String key, int def) {
String value = get(key);
if (value == null) {
return def;
}
if (Pattern.matches("-?[0-9]+", value)) {
return Integer.parseInt(value);
} else {
return def;
}
}
private SystemPropertyUtil() { private SystemPropertyUtil() {
// Unused // Unused
} }