1
0
mirror of https://github.com/lidgren/lidgren-network-gen3.git synced 2026-05-15 22:56:30 +09:00

NetOutgoingMessage.Write(float) optimized to avoid memory allocation

This commit is contained in:
lidgren
2012-01-20 09:52:19 +00:00
parent 61ec7e3add
commit e07f7c75c9
2 changed files with 21 additions and 10 deletions

View File

@@ -21,9 +21,21 @@ using System.Collections.Generic;
using System.Net;
using System.Reflection;
using System.Text;
using System.Runtime.InteropServices;
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
{
private const int c_overAllocateAmount = 4;
@@ -386,17 +398,16 @@ namespace Lidgren.Network
/// </summary>
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
// swap byte order
byte tmp = val[3];
val[3] = val[0];
val[0] = tmp;
tmp = val[2];
val[2] = val[1];
val[1] = tmp;
su.UIntValue = NetUtility.SwapByteOrder(su.UIntValue);
#endif
Write(val);
Write(su.UIntValue);
}
#endif