Use pool of datagram packets inside MultiplexingXXXSocketSupport (#168)

Use pool of datagram packets inside MultiplexingXXXSocketSupport to reduce clone() overhead.
This commit is contained in:
Yury Yaroshevich 2019-01-08 20:53:13 +03:00 committed by Paweł Domas
parent c56fcc3a3f
commit 4027cf429d

View File

@ -18,9 +18,9 @@
package org.ice4j.socket; package org.ice4j.socket;
import java.io.*; import java.io.*;
import java.lang.reflect.*;
import java.net.*; import java.net.*;
import java.util.*; import java.util.*;
import java.util.concurrent.*;
import java.util.logging.*; import java.util.logging.*;
/** /**
@ -188,6 +188,24 @@ abstract class MultiplexingXXXSocketSupport
} }
} }
/**
* The max number of {@link DatagramPacket}'s stored in the receive pool.
*/
private static final int RECEIVE_DATAGRAM_PACKETS_POOL_SIZE = 64;
/**
* The buffer size of pooled datagram packet used to read from
* underlying socket.
*/
private static final int POOLED_DATAGRAM_PACKET_SIZE = 1500;
/**
* A pool of {@link DatagramPacket}s used to receive data from unrelying
* socket.
*/
private final BlockingQueue<DatagramPacket> receiveDatagramPacketsPool
= new ArrayBlockingQueue<>(RECEIVE_DATAGRAM_PACKETS_POOL_SIZE);
/** /**
* The indicator which determines whether this <tt>DatagramSocket</tt> is * The indicator which determines whether this <tt>DatagramSocket</tt> is
* currently reading from the network using * currently reading from the network using
@ -543,7 +561,7 @@ abstract class MultiplexingXXXSocketSupport
throws IOException throws IOException
{ {
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
DatagramPacket r = null; DatagramPacket receivedPacket;
do do
{ {
@ -557,8 +575,8 @@ abstract class MultiplexingXXXSocketSupport
{ {
if (!received.isEmpty()) if (!received.isEmpty())
{ {
r = received.remove(0); receivedPacket = received.remove(0);
if (r != null) if (receivedPacket != null)
break; break;
} }
} }
@ -622,8 +640,18 @@ abstract class MultiplexingXXXSocketSupport
continue; continue;
} }
// The caller will receive from the network. // The pooled datagram packet instance will be used to receive
DatagramPacket c = clone(p, /* arraycopy */ false); // from the network.
DatagramPacket receivePacket
= receiveDatagramPacketsPool.poll();
if (receivePacket == null)
{
receivePacket = new DatagramPacket(
new byte[POOLED_DATAGRAM_PACKET_SIZE],
POOLED_DATAGRAM_PACKET_SIZE);
}
receivePacket.setLength(POOLED_DATAGRAM_PACKET_SIZE);
synchronized (receiveSyncRoot) synchronized (receiveSyncRoot)
{ {
@ -641,11 +669,11 @@ abstract class MultiplexingXXXSocketSupport
} }
} }
} }
doReceive(c); doReceive(receivePacket);
// The caller received from the network. Copy/add the packet to // The caller received from the network. Copy/add the packet to
// the receive list of the sockets which accept it. // the receive list of the sockets which accept it.
acceptBySocketsOrThis(c); acceptBySocketsOrThis(receivePacket);
} }
finally finally
{ {
@ -658,7 +686,12 @@ abstract class MultiplexingXXXSocketSupport
} }
while (true); while (true);
copy(r, p); copy(receivedPacket, p);
// receivePacket is a datagram packet retrieved from SocketReceiveBuffer
// SocketReceiveBuffers only store datagrams borrowed from pool,
// so return datagram back to pool.
receiveDatagramPacketsPool.offer(receivedPacket);
} }
/** /**