Skip to content

Commit

Permalink
fix: recycling segments
Browse files Browse the repository at this point in the history
If I ask for a segment of size n, it should have a buffer of at least n
  • Loading branch information
paulpach committed Oct 18, 2020
1 parent e79d6c4 commit 9d12658
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 5 additions & 1 deletion Assets/Mirror/Runtime/Transport/Kcp/ByteBuffer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;

namespace Mirror.KCP
{
Expand Down Expand Up @@ -76,6 +75,11 @@ public void WriteBytes(byte[] bytes, int startIndex, int length)
writeIndex = total;
}

public void Resize(int length)
{
FixSizeAndReset(RawBuffer.Length, length);
}

public int ReaderIndex { get; private set; }

public int ReadableBytes => writeIndex - ReaderIndex;
Expand Down
4 changes: 3 additions & 1 deletion Assets/Mirror/Runtime/Transport/Kcp/Segment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ public static Segment Get(int size)
{
if (msSegmentPool.Count > 0)
{
return msSegmentPool.Pop();
Segment buffer = msSegmentPool.Pop();
buffer.data.Resize(size);
return buffer;
}
return new Segment(size);
}
Expand Down

0 comments on commit 9d12658

Please sign in to comment.