Skip to content

Commit

Permalink
small unreliable optimization. Use arrays instead of queue. Swap arra…
Browse files Browse the repository at this point in the history
…ys for less lock times.
  • Loading branch information
RevenantX committed Aug 2, 2024
1 parent ed63101 commit 5124524
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
47 changes: 31 additions & 16 deletions LiteNetLib/NetPeer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Runtime.CompilerServices;
using System.Threading;
using LiteNetLib.Utils;

Expand Down Expand Up @@ -82,7 +83,11 @@ private set
}

//Channels
private readonly Queue<NetPacket> _unreliableChannel;
private NetPacket[] _unreliableSecondQueue;
private NetPacket[] _unreliableChannel;
private int _unreliablePendingCount;
private readonly object _unreliableChannelLock = new object();

private readonly ConcurrentQueue<BaseChannel> _channelSendQueue;
private readonly BaseChannel[] _channels;

Expand Down Expand Up @@ -242,7 +247,8 @@ internal NetPeer(NetManager netManager, IPEndPoint remoteEndPoint, int id) : bas
_pongPacket = new NetPacket(PacketProperty.Pong, 0);
_pingPacket = new NetPacket(PacketProperty.Ping, 0) {Sequence = 1};

_unreliableChannel = new Queue<NetPacket>();
_unreliableSecondQueue = new NetPacket[8];
_unreliableChannel = new NetPacket[8];
_holdedFragments = new Dictionary<ushort, IncomingFragments>();
_deliveredFragments = new Dictionary<ushort, ushort>();

Expand Down Expand Up @@ -354,9 +360,17 @@ public void SendPooledPacket(PooledPacket packet, int userDataSize)
CreateChannel(packet._channelNumber).AddToQueue(packet._packet);
}
else
EnqueueUnreliable(packet._packet);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void EnqueueUnreliable(NetPacket packet)
{
lock (_unreliableChannelLock)
{
lock(_unreliableChannel)
_unreliableChannel.Enqueue(packet._packet);
if (_unreliablePendingCount == _unreliableChannel.Length)
Array.Resize(ref _unreliableChannel, _unreliablePendingCount*2);
_unreliableChannel[_unreliablePendingCount++] = packet;
}
}

Expand Down Expand Up @@ -696,8 +710,7 @@ private void SendInternal(

if (channel == null) //unreliable
{
lock(_unreliableChannel)
_unreliableChannel.Enqueue(packet);
EnqueueUnreliable(packet);
}
else
{
Expand Down Expand Up @@ -828,8 +841,7 @@ private void SendInternal(

if (channel == null) //unreliable
{
lock(_unreliableChannel)
_unreliableChannel.Enqueue(packet);
EnqueueUnreliable(packet);
}
else
{
Expand Down Expand Up @@ -1395,15 +1407,18 @@ internal void Update(float deltaTime)
}
}

lock (_unreliableChannel)
int unreliableCount;
lock (_unreliableChannelLock)
{
int unreliableCount = _unreliableChannel.Count;
for (int i = 0; i < unreliableCount; i++)
{
var packet = _unreliableChannel.Dequeue();
SendUserData(packet);
NetManager.PoolRecycle(packet);
}
(_unreliableChannel, _unreliableSecondQueue) = (_unreliableSecondQueue, _unreliableChannel);
unreliableCount = _unreliablePendingCount;
_unreliablePendingCount = 0;
}
for (int i = 0; i < unreliableCount; i++)
{
var packet = _unreliableSecondQueue[i];
SendUserData(packet);
NetManager.PoolRecycle(packet);
}

SendMerged();
Expand Down
Binary file modified LiteNetLibSampleUnity/Assets/LiteNetLib.dll
Binary file not shown.
10 changes: 8 additions & 2 deletions LiteNetLibSampleUnity/Assets/LiteNetLib.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5124524

Please sign in to comment.