Reduce GC in OioDatagramChannel.doRead/doFlush

This commit is contained in:
Trustin Lee 2012-05-25 14:12:43 -07:00
parent 2395bcd805
commit 01aa1647bb

View File

@ -46,9 +46,12 @@ public class OioDatagramChannel extends AbstractChannel
private static final InternalLogger logger = InternalLoggerFactory.getInstance(OioDatagramChannel.class); private static final InternalLogger logger = InternalLoggerFactory.getInstance(OioDatagramChannel.class);
private static final byte[] EMPTY_DATA = new byte[0];
private final MulticastSocket socket; private final MulticastSocket socket;
private final DatagramChannelConfig config; private final DatagramChannelConfig config;
private final ChannelBufferHolder<Object> out = ChannelBufferHolders.messageBuffer(); private final ChannelBufferHolder<Object> out = ChannelBufferHolders.messageBuffer();
private final java.net.DatagramPacket tmpPacket = new java.net.DatagramPacket(EMPTY_DATA, 0);
private static MulticastSocket newSocket() { private static MulticastSocket newSocket() {
try { try {
@ -194,14 +197,15 @@ public class OioDatagramChannel extends AbstractChannel
protected int doRead(Queue<Object> buf) throws Exception { protected int doRead(Queue<Object> buf) throws Exception {
int packetSize = config().getReceivePacketSize(); int packetSize = config().getReceivePacketSize();
byte[] data = new byte[packetSize]; byte[] data = new byte[packetSize];
java.net.DatagramPacket p = new java.net.DatagramPacket(data, packetSize); tmpPacket.setData(data);
try { try {
socket.receive(p); socket.receive(tmpPacket);
InetSocketAddress remoteAddr = (InetSocketAddress) p.getSocketAddress(); InetSocketAddress remoteAddr = (InetSocketAddress) tmpPacket.getSocketAddress();
if (remoteAddr == null) { if (remoteAddr == null) {
remoteAddr = remoteAddress(); remoteAddr = remoteAddress();
} }
buf.add(new DatagramPacket(ChannelBuffers.wrappedBuffer(data, p.getOffset(), p.getLength()), remoteAddr)); buf.add(new DatagramPacket(ChannelBuffers.wrappedBuffer(
data, tmpPacket.getOffset(), tmpPacket.getLength()), remoteAddr));
return 1; return 1;
} catch (SocketTimeoutException e) { } catch (SocketTimeoutException e) {
// Expected // Expected
@ -224,18 +228,16 @@ public class OioDatagramChannel extends AbstractChannel
DatagramPacket p = (DatagramPacket) buf.poll(); DatagramPacket p = (DatagramPacket) buf.poll();
ChannelBuffer data = p.data(); ChannelBuffer data = p.data();
int length = data.readableBytes(); int length = data.readableBytes();
SocketAddress remoteAddr = p.remoteAddress(); tmpPacket.setSocketAddress(p.remoteAddress());
java.net.DatagramPacket q;
if (data.hasArray()) { if (data.hasArray()) {
q = new java.net.DatagramPacket( tmpPacket.setData(data.array(), data.arrayOffset() + data.readerIndex(), length);
data.array(), data.arrayOffset() + data.readerIndex(), length, remoteAddr);
} else { } else {
byte[] tmp = new byte[length]; byte[] tmp = new byte[length];
data.getBytes(data.readerIndex(), tmp); data.getBytes(data.readerIndex(), tmp);
q = new java.net.DatagramPacket(tmp, length, remoteAddr); tmpPacket.setData(tmp);
} }
socket.send(q); socket.send(tmpPacket);
return 1; return 1;
} }