Further reduces the average time it takes to clone a DatagramPacket.

This commit is contained in:
lubomir.marinov 2014-09-05 15:47:37 +00:00
parent a34e108b2d
commit eccf1346c0
3 changed files with 25 additions and 5 deletions

View File

@ -362,7 +362,6 @@ public class GoogleRelayedCandidateDelegate
else else
{ {
packetsToSend.add(MultiplexingDatagramSocket.clone(p)); packetsToSend.add(MultiplexingDatagramSocket.clone(p));
if (sendThread == null) if (sendThread == null)
createSendThread(); createSendThread();
else else

View File

@ -50,6 +50,25 @@ public class MultiplexingDatagramSocket
* specified <tt>DatagramPacket</tt> * specified <tt>DatagramPacket</tt>
*/ */
public static DatagramPacket clone(DatagramPacket p) public static DatagramPacket clone(DatagramPacket p)
{
return clone(p, /* arraycopy */ true);
}
/**
* Initializes a new <tt>DatagramPacket</tt> instance which is a clone of a
* specific <tt>DatagramPacket</tt> i.e. the properties of the clone
* <tt>DatagramPacket</tt> are clones of the specified
* <tt>DatagramPacket</tt>.
*
* @param p the <tt>DatagramPacket</tt> to clone
* @param arraycopy <tt>true</tt> if the actual bytes of the data of
* <tt>p</tt> are to be copied into the clone or <tt>false</tt> if only the
* capacity of the data of <tt>p</tt> is to be cloned without copying the
* actual bytes of the data of <tt>p</tt>
* @return a new <tt>DatagramPacket</tt> instance which is a clone of the
* specified <tt>DatagramPacket</tt>
*/
public static DatagramPacket clone(DatagramPacket p, boolean arraycopy)
{ {
byte[] data; byte[] data;
int off; int off;
@ -71,7 +90,7 @@ public class MultiplexingDatagramSocket
// However, only copy the range of data starting with off and // However, only copy the range of data starting with off and
// spanning len number of bytes. Of course, preserve off and len // spanning len number of bytes. Of course, preserve off and len
// in addition to the capacity. // in addition to the capacity.
if (len > 0) if (arraycopy && (len > 0))
{ {
int arraycopyOff, arraycopyLen; int arraycopyOff, arraycopyLen;
@ -582,7 +601,7 @@ public class MultiplexingDatagramSocket
} }
// The caller will receive from the network. // The caller will receive from the network.
DatagramPacket c = clone(p); DatagramPacket c = clone(p, /* arraycopy */ false);
synchronized (receiveSyncRoot) synchronized (receiveSyncRoot)
{ {
@ -614,7 +633,10 @@ public class MultiplexingDatagramSocket
{ {
synchronized (socket.received) synchronized (socket.received)
{ {
socket.received.add(accepted ? clone(c) : c); socket.received.add(
accepted
? clone(c, /* arraycopy */ true)
: c);
socket.received.notifyAll(); socket.received.notifyAll();
} }
accepted = true; accepted = true;

View File

@ -1063,7 +1063,6 @@ public class RelayedCandidateDatagramSocket
else else
{ {
packetsToSend.add(MultiplexingDatagramSocket.clone(p)); packetsToSend.add(MultiplexingDatagramSocket.clone(p));
if (sendThread == null) if (sendThread == null)
createSendThread(); createSendThread();
else else