Skip to content

Commit 2179b41

Browse files
committed
Moved temporary buffer allocations to a weak reference
In cases where multiple large messages come in in rapid succession. There is no need to throw the temp buffer away. Instead we store it as a weak reference
1 parent 780997c commit 2179b41

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

MLAPI/Data/Transports/UNET/UnetTransport.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ namespace MLAPI.Transports.UNET
88
[Serializable]
99
public class UnetTransport : IUDPTransport
1010
{
11+
private WeakReference temporaryBufferReference;
12+
1113
public ChannelType InternalChannel => ChannelType.ReliableFragmentedSequenced;
1214
public uint ServerClientId => new NetId(0, 0, true).GetClientId();
1315
public int serverConnectionId;
@@ -71,7 +73,17 @@ public NetEventType PollReceive(out uint clientId, out int channelId, ref byte[]
7173

7274
if (errorType == NetworkError.MessageToLong)
7375
{
74-
byte[] tempBuffer = new byte[receivedSize];
76+
byte[] tempBuffer;
77+
if (temporaryBufferReference != null && temporaryBufferReference.IsAlive && ((byte[]) temporaryBufferReference.Target).Length >= receivedSize)
78+
{
79+
tempBuffer = (byte[])temporaryBufferReference.Target;
80+
}
81+
else
82+
{
83+
tempBuffer = new byte[receivedSize];
84+
temporaryBufferReference = new WeakReference(tempBuffer);
85+
}
86+
7587
eventType = NetworkTransport.Receive(out hostId, out connectionId, out channelId, tempBuffer, tempBuffer.Length, out receivedSize, out err);
7688
data = tempBuffer;
7789
}

0 commit comments

Comments
 (0)