You've already forked lidgren-network-gen3
mirror of
https://github.com/lidgren/lidgren-network-gen3.git
synced 2026-05-16 15:16:33 +09:00
NetOutgoingMessage.Write(float) optimized to avoid memory allocation
This commit is contained in:
@@ -21,9 +21,21 @@ using System.Collections.Generic;
|
|||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
namespace Lidgren.Network
|
namespace Lidgren.Network
|
||||||
{
|
{
|
||||||
|
[StructLayout(LayoutKind.Explicit)]
|
||||||
|
public struct SingleUIntUnion
|
||||||
|
{
|
||||||
|
[FieldOffset(0)]
|
||||||
|
public float SingleValue;
|
||||||
|
|
||||||
|
[FieldOffset(0)]
|
||||||
|
[CLSCompliant(false)]
|
||||||
|
public uint UIntValue;
|
||||||
|
}
|
||||||
|
|
||||||
public sealed partial class NetOutgoingMessage
|
public sealed partial class NetOutgoingMessage
|
||||||
{
|
{
|
||||||
private const int c_overAllocateAmount = 4;
|
private const int c_overAllocateAmount = 4;
|
||||||
@@ -386,17 +398,16 @@ namespace Lidgren.Network
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public void Write(float source)
|
public void Write(float source)
|
||||||
{
|
{
|
||||||
byte[] val = BitConverter.GetBytes(source);
|
// Use union to avoid BitConverter.GetBytes() which allocates memory on the heap
|
||||||
|
SingleUIntUnion su;
|
||||||
|
su.UIntValue = 0; // must initialize every member of the union to avoid warning
|
||||||
|
su.SingleValue = source;
|
||||||
|
|
||||||
#if BIGENDIAN
|
#if BIGENDIAN
|
||||||
// swap byte order
|
// swap byte order
|
||||||
byte tmp = val[3];
|
su.UIntValue = NetUtility.SwapByteOrder(su.UIntValue);
|
||||||
val[3] = val[0];
|
|
||||||
val[0] = tmp;
|
|
||||||
tmp = val[2];
|
|
||||||
val[2] = val[1];
|
|
||||||
val[1] = tmp;
|
|
||||||
#endif
|
#endif
|
||||||
Write(val);
|
Write(su.UIntValue);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ namespace UnitTests
|
|||||||
|
|
||||||
int bcnt = 0;
|
int bcnt = 0;
|
||||||
|
|
||||||
msg.Write(45.0f);
|
msg.Write(567845.0f);
|
||||||
msg.WriteVariableInt32(2115998022);
|
msg.WriteVariableInt32(2115998022);
|
||||||
msg.Write(46.0);
|
msg.Write(46.0);
|
||||||
bcnt += msg.WriteVariableInt32(-47);
|
bcnt += msg.WriteVariableInt32(-47);
|
||||||
@@ -94,7 +94,7 @@ namespace UnitTests
|
|||||||
bdr.Append(inc.ReadVariableUInt32());
|
bdr.Append(inc.ReadVariableUInt32());
|
||||||
bdr.Append(inc.ReadVariableInt64());
|
bdr.Append(inc.ReadVariableInt64());
|
||||||
|
|
||||||
if (bdr.ToString().Equals("False-342duke of earl4344True45211599802246-4747000048-49"))
|
if (bdr.ToString().Equals("False-342duke of earl4344True567845211599802246-4747000048-49"))
|
||||||
Console.WriteLine("Read/write tests OK");
|
Console.WriteLine("Read/write tests OK");
|
||||||
else
|
else
|
||||||
throw new NetException("Read/write tests FAILED!");
|
throw new NetException("Read/write tests FAILED!");
|
||||||
|
|||||||
Reference in New Issue
Block a user