You've already forked lidgren-network-gen3
mirror of
https://github.com/lidgren/lidgren-network-gen3.git
synced 2026-05-19 00:26:30 +09:00
Improved performance (very slightly) when batch recycling incoming messages
This commit is contained in:
@@ -17,6 +17,8 @@ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR TH
|
|||||||
USE OR OTHER DEALINGS IN THE SOFTWARE.
|
USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
//#define USE_RELEASE_STATISTICS
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
@@ -228,6 +230,9 @@ namespace Lidgren.Network
|
|||||||
//
|
//
|
||||||
internal void SendPacket(int numBytes, IPEndPoint target, int numMessages, out bool connectionReset)
|
internal void SendPacket(int numBytes, IPEndPoint target, int numMessages, out bool connectionReset)
|
||||||
{
|
{
|
||||||
|
#if USE_RELEASE_STATISTICS
|
||||||
|
m_statistics.PacketSent(numBytes, numMessages);
|
||||||
|
#endif
|
||||||
connectionReset = false;
|
connectionReset = false;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -55,19 +55,18 @@ namespace Lidgren.Network
|
|||||||
if (m_storagePool == null)
|
if (m_storagePool == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
int len = storage.Length;
|
|
||||||
lock (m_storagePool)
|
lock (m_storagePool)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < m_storagePool.Count; i++)
|
m_storagePoolBytes += storage.Length;
|
||||||
|
int cnt = m_storagePool.Count;
|
||||||
|
for (int i = 0; i < cnt; i++)
|
||||||
{
|
{
|
||||||
if (m_storagePool[i] == null)
|
if (m_storagePool[i] == null)
|
||||||
{
|
{
|
||||||
m_storagePoolBytes += storage.Length;
|
|
||||||
m_storagePool[i] = storage;
|
m_storagePool[i] = storage;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
m_storagePoolBytes += storage.Length;
|
|
||||||
m_storagePool.Add(storage);
|
m_storagePool.Add(storage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -156,18 +155,33 @@ namespace Lidgren.Network
|
|||||||
if (m_incomingMessagesPool == null)
|
if (m_incomingMessagesPool == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
foreach (var msg in toRecycle)
|
// first recycle the storage of each message
|
||||||
|
if (m_storagePool != null)
|
||||||
{
|
{
|
||||||
#if DEBUG
|
lock (m_storagePool)
|
||||||
if (m_incomingMessagesPool.Contains(msg))
|
{
|
||||||
throw new NetException("Recyling already recycled message! Thread race?");
|
foreach (var msg in toRecycle)
|
||||||
#endif
|
{
|
||||||
byte[] storage = msg.m_data;
|
var storage = msg.m_data;
|
||||||
msg.m_data = null;
|
msg.m_data = null;
|
||||||
Recycle(storage);
|
m_storagePoolBytes += storage.Length;
|
||||||
msg.Reset();
|
int cnt = m_storagePool.Count;
|
||||||
m_incomingMessagesPool.Enqueue(msg);
|
for (int i = 0; i < cnt; i++)
|
||||||
|
{
|
||||||
|
if (m_storagePool[i] == null)
|
||||||
|
{
|
||||||
|
m_storagePool[i] = storage;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
msg.Reset();
|
||||||
|
m_storagePool.Add(storage);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// then recycle the message objects
|
||||||
|
m_incomingMessagesPool.Enqueue(toRecycle);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void Recycle(NetOutgoingMessage msg)
|
internal void Recycle(NetOutgoingMessage msg)
|
||||||
|
|||||||
@@ -146,8 +146,13 @@ namespace Lidgren.Network
|
|||||||
{
|
{
|
||||||
StringBuilder bdr = new StringBuilder();
|
StringBuilder bdr = new StringBuilder();
|
||||||
bdr.AppendLine(m_peer.ConnectionsCount.ToString() + " connections");
|
bdr.AppendLine(m_peer.ConnectionsCount.ToString() + " connections");
|
||||||
|
#if DEBUG || USE_RELEASE_STATISTICS
|
||||||
bdr.AppendLine("Sent " + m_sentBytes + " bytes in " + m_sentMessages + " messages in " + m_sentPackets + " packets");
|
bdr.AppendLine("Sent " + m_sentBytes + " bytes in " + m_sentMessages + " messages in " + m_sentPackets + " packets");
|
||||||
bdr.AppendLine("Received " + m_receivedBytes + " bytes in " + m_receivedMessages + " messages in " + m_receivedPackets + " packets");
|
bdr.AppendLine("Received " + m_receivedBytes + " bytes in " + m_receivedMessages + " messages in " + m_receivedPackets + " packets");
|
||||||
|
#else
|
||||||
|
bdr.AppendLine("Sent (n/a) bytes in (n/a) messages in (n/a) packets");
|
||||||
|
bdr.AppendLine("Received (n/a) bytes in (n/a) messages in (n/a) packets");
|
||||||
|
#endif
|
||||||
bdr.AppendLine("Storage allocated " + m_bytesAllocated + " bytes");
|
bdr.AppendLine("Storage allocated " + m_bytesAllocated + " bytes");
|
||||||
bdr.AppendLine("Recycled pool " + m_peer.m_storagePoolBytes + " bytes");
|
bdr.AppendLine("Recycled pool " + m_peer.m_storagePoolBytes + " bytes");
|
||||||
return bdr.ToString();
|
return bdr.ToString();
|
||||||
|
|||||||
@@ -21,6 +21,8 @@ using System;
|
|||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
// @TODO: examine performance characteristics of using SpinLock when using .Net 4.0
|
||||||
|
|
||||||
namespace Lidgren.Network
|
namespace Lidgren.Network
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -83,6 +85,25 @@ namespace Lidgren.Network
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds an item last/tail of the queue
|
||||||
|
/// </summary>
|
||||||
|
public void Enqueue(IEnumerable<T> items)
|
||||||
|
{
|
||||||
|
lock (m_lock)
|
||||||
|
{
|
||||||
|
foreach (var item in items)
|
||||||
|
{
|
||||||
|
if (m_size == m_items.Length)
|
||||||
|
SetCapacity(m_items.Length + 8); // @TODO move this out of loop
|
||||||
|
|
||||||
|
int slot = (m_head + m_size) % m_items.Length;
|
||||||
|
m_items[slot] = item;
|
||||||
|
m_size++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Places an item first, at the head of the queue
|
/// Places an item first, at the head of the queue
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -80,7 +80,7 @@ namespace ManyClients
|
|||||||
private void button1_Click(object sender, EventArgs e)
|
private void button1_Click(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
var om = Net.CreateMessage();
|
var om = Net.CreateMessage();
|
||||||
om.Write("Hi!");
|
om.Write("Manual hi!");
|
||||||
|
|
||||||
Net.SendMessage(om, NetDeliveryMethod.ReliableOrdered);
|
Net.SendMessage(om, NetDeliveryMethod.ReliableOrdered);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user