Skip to content

Commit b0817e9

Browse files
committed
perf(bitstream): Fixed BitStream overriding WriteByte
This should reduce allocations significantly
1 parent 16b7fc7 commit b0817e9

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

MLAPI/NetworkingManagerComponents/Binary/BitStream.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,27 @@ public override void Write(byte[] buffer, int offset, int count)
292292
if (BitPosition > BitLength) BitLength = BitPosition;
293293
}
294294

295+
/// <summary>
296+
/// Write byte value to the internal stream buffer.
297+
/// </summary>
298+
/// <param name="value">The byte value to write.</param>
299+
public override void WriteByte(byte value)
300+
{
301+
// Check bit alignment. If misaligned, each byte written has to be misaligned
302+
if (BitAligned)
303+
{
304+
if (Position + 1 >= target.Length) Grow(1);
305+
target[Position] = value;
306+
Position += 1;
307+
}
308+
else
309+
{
310+
if (Position + 1 + 1 >= target.Length) Grow(1);
311+
_WriteMisaligned(value);
312+
}
313+
if (BitPosition > BitLength) BitLength = BitPosition;
314+
}
315+
295316
/// <summary>
296317
/// Write a misaligned byte. NOTE: Using this when the bit position isn't byte-misaligned may cause an IndexOutOfBoundsException! This does not update the current Length of the stream.
297318
/// </summary>

0 commit comments

Comments
 (0)