Reverted back the changes about direct buffer pool - it doesn't seem to improve the performance as much as additional memory consumption and the latest one often led to OOME during testing

This commit is contained in:
Trustin Lee 2010-02-18 13:00:31 +00:00
parent 263e1ed601
commit a9c90d0e4a
3 changed files with 2 additions and 112 deletions

View File

@ -1,99 +0,0 @@
/*
* Copyright 2010 Red Hat, Inc.
*
* Red Hat licenses this file to you under the Apache License, version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package org.jboss.netty.channel.socket.nio;
import java.nio.ByteBuffer;
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://gleamynode.net/">Trustin Lee</a>
* @version $Rev$, $Date$
*/
final class DirectBufferPool {
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 = 1048576;
} else {
logger.debug(
"Setting the preallocated buffer capacity to: " + val);
}
CAPACITY = val;
}
private ByteBuffer preallocatedBuffer;
DirectBufferPool() {
super();
}
final ByteBuffer acquire(ChannelBuffer src) {
ByteBuffer dst = acquire(src.readableBytes());
dst.mark();
src.getBytes(src.readerIndex(), dst);
dst.reset();
return dst;
}
final ByteBuffer acquire(int size) {
ByteBuffer preallocatedBuffer = this.preallocatedBuffer;
if (preallocatedBuffer == null) {
if (size < CAPACITY) {
return preallocateAndAcquire(size);
} else {
return ByteBuffer.allocateDirect(size);
}
}
if (preallocatedBuffer.remaining() < size) {
if (size > CAPACITY) {
return ByteBuffer.allocateDirect(size);
} else {
return preallocateAndAcquire(size);
}
} else {
int nextPos = preallocatedBuffer.position() + size;
ByteBuffer x = preallocatedBuffer.duplicate();
preallocatedBuffer.position(nextPos);
x.limit(nextPos);
return x;
}
}
private final ByteBuffer preallocateAndAcquire(int size) {
ByteBuffer preallocatedBuffer = this.preallocatedBuffer =
ByteBuffer.allocateDirect(CAPACITY);
ByteBuffer x = preallocatedBuffer.duplicate();
x.limit(size);
preallocatedBuffer.position(size);
return x;
}
}

View File

@ -127,8 +127,6 @@ class NioDatagramWorker implements Runnable {
private volatile int cancelledKeys; // should use AtomicInteger but we just need approximation
private final DirectBufferPool directBufferPool = new DirectBufferPool();
/**
* Sole constructor.
*
@ -519,11 +517,7 @@ class NioDatagramWorker implements Runnable {
}
ChannelBuffer origBuf = (ChannelBuffer) evt.getMessage();
if (origBuf.isDirect()) {
channel.currentWriteBuffer = buf = origBuf.toByteBuffer();
} else {
channel.currentWriteBuffer = buf = directBufferPool.acquire(origBuf);
}
channel.currentWriteBuffer = buf = origBuf.toByteBuffer();
} else {
buf = channel.currentWriteBuffer;
}

View File

@ -78,7 +78,6 @@ class NioWorker implements Runnable {
private final Queue<Runnable> registerTaskQueue = new LinkedTransferQueue<Runnable>();
private final Queue<Runnable> writeTaskQueue = new LinkedTransferQueue<Runnable>();
private volatile int cancelledKeys; // should use AtomicInteger but we just need approximation
private final DirectBufferPool directBufferPool = new DirectBufferPool();
NioWorker(int bossId, int id, Executor executor) {
this.bossId = bossId;
@ -444,11 +443,7 @@ class NioWorker implements Runnable {
}
ChannelBuffer origBuf = (ChannelBuffer) evt.getMessage();
if (origBuf.isDirect()) {
channel.currentWriteBuffer = buf = origBuf.toByteBuffer();
} else {
channel.currentWriteBuffer = buf = directBufferPool.acquire(origBuf);
}
channel.currentWriteBuffer = buf = origBuf.toByteBuffer();
} else {
buf = channel.currentWriteBuffer;
}