Skip to content

Commit cc04804

Browse files
committed
Lockless double serialization/deserialization
1 parent 62b7a1a commit cc04804

File tree

2 files changed

+26
-30
lines changed

2 files changed

+26
-30
lines changed

MLAPI.Tests/NetworkingManagerComponents/Binary/BitStreamTest.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,21 @@ public void TestWriteDouble()
330330

331331
}
332332

333+
[Test]
334+
public void TestWriteDoublePacked()
335+
{
336+
double somenumber = Math.PI;
337+
BitStream outStream = new BitStream();
338+
339+
outStream.WriteDoublePacked(somenumber);
340+
byte[] buffer = outStream.GetBuffer();
341+
342+
BitStream inStream = new BitStream(buffer);
343+
344+
Assert.That(inStream.ReadDoublePacked(), Is.EqualTo(somenumber));
345+
346+
}
347+
333348
[Test]
334349
public void TestWriteMisaligned()
335350
{

MLAPI/NetworkingManagerComponents/Binary/BitStream.cs

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using System.Security;
34
using UnityEngine;
45
using static MLAPI.NetworkingManagerComponents.Binary.Arithmetic;
56

@@ -300,13 +301,9 @@ public void WriteSingle(float value)
300301
/// <param name="value">Value to write</param>
301302
public void WriteDouble(double value)
302303
{
303-
lock (holder_d)
304-
lock (holder_l)
305-
{
306-
holder_d[0] = value;
307-
Buffer.BlockCopy(holder_d, 0, holder_l, 0, 8);
308-
WriteUInt64(holder_l[0]);
309-
}
304+
long binary = BitConverter.DoubleToInt64Bits(value);
305+
WriteInt64(binary);
306+
310307
}
311308

312309
/// <summary>
@@ -330,13 +327,8 @@ public void WriteSinglePacked(float value)
330327
/// <param name="value">Value to write</param>
331328
public void WriteDoublePacked(double value)
332329
{
333-
lock (holder_d)
334-
lock (holder_l)
335-
{
336-
holder_d[0] = value;
337-
Buffer.BlockCopy(holder_d, 0, holder_l, 0, 8);
338-
WriteUInt64Packed(BinaryHelpers.SwapEndian(holder_l[0]));
339-
}
330+
long binary = BitConverter.DoubleToInt64Bits(value);
331+
WriteInt64Packed(binary);
340332
}
341333

342334
/// <summary>
@@ -532,14 +524,9 @@ public float ReadSingle()
532524
/// <returns>The read value</returns>
533525
public double ReadDouble()
534526
{
535-
ulong read = ReadUInt64();
536-
lock (holder_d)
537-
lock (holder_l)
538-
{
539-
holder_l[0] = read;
540-
Buffer.BlockCopy(holder_l, 0, holder_d, 0, 8);
541-
return holder_d[0];
542-
}
527+
528+
long read = ReadInt64();
529+
return BitConverter.Int64BitsToDouble(read);
543530
}
544531

545532
/// <summary>
@@ -564,14 +551,8 @@ public float ReadSinglePacked()
564551
/// <returns>The read value</returns>
565552
public double ReadDoublePacked()
566553
{
567-
ulong read = ReadUInt64Packed();
568-
lock (holder_d)
569-
lock (holder_l)
570-
{
571-
holder_l[0] = BinaryHelpers.SwapEndian(read);
572-
Buffer.BlockCopy(holder_l, 0, holder_d, 0, 8);
573-
return holder_d[0];
574-
}
554+
long read = ReadInt64Packed();
555+
return BitConverter.Int64BitsToDouble(read);
575556
}
576557

577558
/// <summary>

0 commit comments

Comments
 (0)