-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: network writer pool to avoid expensive allocations
- Loading branch information
Showing
1 changed file
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace Mirror | ||
{ | ||
|
||
public static class NetworkWriterPool | ||
{ | ||
static readonly Stack<NetworkWriter> pool = new Stack<NetworkWriter>(); | ||
|
||
public static NetworkWriter GetWriter() | ||
{ | ||
if (pool.Count != 0) | ||
{ | ||
NetworkWriter writer = pool.Pop(); | ||
// reset cached writer length and position | ||
writer.SetLength(0); | ||
return writer; | ||
} | ||
|
||
return new NetworkWriter(); | ||
} | ||
|
||
public static void Recycle(NetworkWriter writer) | ||
{ | ||
pool.Push(writer); | ||
} | ||
} | ||
} |