mirror of
https://github.com/MirrorNetworking/Mirror.git
synced 2024-11-18 11:00:32 +00:00
feature: NetworkWriter.WriteBytes(byte*) unsafe version to prepare for BitTree delta compression
This commit is contained in:
parent
243e142203
commit
9afc57bc78
@ -197,6 +197,26 @@ public void WriteBytes(byte[] array, int offset, int count)
|
|||||||
Array.ConstrainedCopy(array, offset, this.buffer, Position, count);
|
Array.ConstrainedCopy(array, offset, this.buffer, Position, count);
|
||||||
Position += count;
|
Position += count;
|
||||||
}
|
}
|
||||||
|
// write an unsafe byte* array.
|
||||||
|
// useful for bit tree compression, etc.
|
||||||
|
public unsafe bool WriteBytes(byte* ptr, int offset, int size)
|
||||||
|
{
|
||||||
|
EnsureCapacity(Position + size);
|
||||||
|
|
||||||
|
fixed (byte* destination = &buffer[Position])
|
||||||
|
{
|
||||||
|
// write 'size' bytes at position
|
||||||
|
// 10 mio writes: 868ms
|
||||||
|
// Array.Copy(value.Array, value.Offset, buffer, Position, value.Count);
|
||||||
|
// 10 mio writes: 775ms
|
||||||
|
// Buffer.BlockCopy(value.Array, value.Offset, buffer, Position, value.Count);
|
||||||
|
// 10 mio writes: 637ms
|
||||||
|
UnsafeUtility.MemCpy(destination, ptr + offset, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
Position += size;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>Writes any type that mirror supports. Uses weaver populated Writer(T).write.</summary>
|
/// <summary>Writes any type that mirror supports. Uses weaver populated Writer(T).write.</summary>
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
@ -133,6 +133,22 @@ public void TestWritingBytesSegment()
|
|||||||
Assert.That(deserialized.Array[deserialized.Offset + i], Is.EqualTo(data[i]));
|
Assert.That(deserialized.Array[deserialized.Offset + i], Is.EqualTo(data[i]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public unsafe void WriteBytes_Ptr()
|
||||||
|
{
|
||||||
|
NetworkWriter writer = new NetworkWriter();
|
||||||
|
|
||||||
|
// make sure this respects position & offset
|
||||||
|
writer.WriteByte(0xFF);
|
||||||
|
|
||||||
|
byte[] bytes = {0x01, 0x02, 0x03, 0x04};
|
||||||
|
fixed (byte* ptr = bytes)
|
||||||
|
{
|
||||||
|
Assert.True(writer.WriteBytes(ptr, 1, 2));
|
||||||
|
Assert.True(writer.ToArraySegment().SequenceEqual(new byte[] {0xFF, 0x02, 0x03}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// write byte[], read segment
|
// write byte[], read segment
|
||||||
[Test]
|
[Test]
|
||||||
public void TestWritingBytesAndReadingSegment()
|
public void TestWritingBytesAndReadingSegment()
|
||||||
|
Loading…
Reference in New Issue
Block a user