You've already forked lidgren-network-gen3
mirror of
https://github.com/lidgren/lidgren-network-gen3.git
synced 2026-05-17 15:46:33 +09:00
Merge pull request #2 from forestrf/master
Less GC when reading floats and doubles thanks to a pool of byte[] + Removed a unnecessary array creation and destruction
This commit is contained in:
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
#if !__NOIPENDPOINT__
|
#if !__NOIPENDPOINT__
|
||||||
using NetEndPoint = System.Net.IPEndPoint;
|
using NetEndPoint = System.Net.IPEndPoint;
|
||||||
@@ -16,6 +17,8 @@ namespace Lidgren.Network
|
|||||||
public partial class NetBuffer
|
public partial class NetBuffer
|
||||||
{
|
{
|
||||||
private const string c_readOverflowError = "Trying to read past the buffer size - likely caused by mismatching Write/Reads, different size or order.";
|
private const string c_readOverflowError = "Trying to read past the buffer size - likely caused by mismatching Write/Reads, different size or order.";
|
||||||
|
private const int c_bufferSize = 64; // Min 8 to hold anything but strings. Increase it if readed strings usally don't fit inside the buffer
|
||||||
|
private static object s_buffer;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Reads a boolean value (stored as a single bit) written using Write(bool)
|
/// Reads a boolean value (stored as a single bit) written using Write(bool)
|
||||||
@@ -352,8 +355,11 @@ namespace Lidgren.Network
|
|||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
byte[] bytes = ReadBytes(4);
|
byte[] bytes = (byte[]) Interlocked.Exchange(ref s_buffer, null) ?? new byte[c_bufferSize];
|
||||||
return BitConverter.ToSingle(bytes, 0);
|
ReadBytes(bytes, 0, 4);
|
||||||
|
float res = BitConverter.ToSingle(bytes, 0);
|
||||||
|
s_buffer = bytes;
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -374,8 +380,10 @@ namespace Lidgren.Network
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
byte[] bytes = ReadBytes(4);
|
byte[] bytes = (byte[]) Interlocked.Exchange(ref s_buffer, null) ?? new byte[c_bufferSize];
|
||||||
|
ReadBytes(bytes, 0, 4);
|
||||||
result = BitConverter.ToSingle(bytes, 0);
|
result = BitConverter.ToSingle(bytes, 0);
|
||||||
|
s_buffer = bytes;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -394,8 +402,11 @@ namespace Lidgren.Network
|
|||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
byte[] bytes = ReadBytes(8);
|
byte[] bytes = (byte[]) Interlocked.Exchange(ref s_buffer, null) ?? new byte[c_bufferSize];
|
||||||
return BitConverter.ToDouble(bytes, 0);
|
ReadBytes(bytes, 0, 8);
|
||||||
|
double res = BitConverter.ToDouble(bytes, 0);
|
||||||
|
s_buffer = bytes;
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
@@ -593,8 +604,16 @@ namespace Lidgren.Network
|
|||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
byte[] bytes = ReadBytes(byteLen);
|
if (byteLen <= c_bufferSize) {
|
||||||
return System.Text.Encoding.UTF8.GetString(bytes, 0, bytes.Length);
|
byte[] buffer = (byte[]) Interlocked.Exchange(ref s_buffer, null) ?? new byte[c_bufferSize];
|
||||||
|
ReadBytes(buffer, 0, byteLen);
|
||||||
|
string retval = Encoding.UTF8.GetString(buffer, 0, byteLen);
|
||||||
|
s_buffer = buffer;
|
||||||
|
return retval;
|
||||||
|
} else {
|
||||||
|
byte[] bytes = ReadBytes(byteLen);
|
||||||
|
return Encoding.UTF8.GetString(bytes, 0, bytes.Length);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -24,7 +24,8 @@ namespace Lidgren.Network
|
|||||||
/// <param name="method">How to deliver the message</param>
|
/// <param name="method">How to deliver the message</param>
|
||||||
public void SendToAll(NetOutgoingMessage msg, NetDeliveryMethod method)
|
public void SendToAll(NetOutgoingMessage msg, NetDeliveryMethod method)
|
||||||
{
|
{
|
||||||
var all = this.Connections;
|
// Modifying m_connections will modify the list of the connections of the NetPeer. Do only reads here
|
||||||
|
var all = m_connections;
|
||||||
if (all.Count <= 0) {
|
if (all.Count <= 0) {
|
||||||
if (msg.m_isSent == false)
|
if (msg.m_isSent == false)
|
||||||
Recycle(msg);
|
Recycle(msg);
|
||||||
@@ -43,7 +44,8 @@ namespace Lidgren.Network
|
|||||||
/// <param name="sequenceChannel">Which sequence channel to use for the message</param>
|
/// <param name="sequenceChannel">Which sequence channel to use for the message</param>
|
||||||
public void SendToAll(NetOutgoingMessage msg, NetConnection except, NetDeliveryMethod method, int sequenceChannel)
|
public void SendToAll(NetOutgoingMessage msg, NetConnection except, NetDeliveryMethod method, int sequenceChannel)
|
||||||
{
|
{
|
||||||
var all = this.Connections;
|
// Modifying m_connections will modify the list of the connections of the NetPeer. Do only reads here
|
||||||
|
var all = m_connections;
|
||||||
if (all.Count <= 0) {
|
if (all.Count <= 0) {
|
||||||
if (msg.m_isSent == false)
|
if (msg.m_isSent == false)
|
||||||
Recycle(msg);
|
Recycle(msg);
|
||||||
|
|||||||
Reference in New Issue
Block a user