Skip to content

Commit ebc4458

Browse files
Override ReadByte in PacketReader.
Turns out, the normal BinaryReader was calling NetworkStream.ReadByte, which didn't override ReadByte. As such, every single call to ReadByte would wastefully allocate a 1 element byte array, which ended up being responsible for a significant portion of allocations when running server for extended periods of time.
1 parent 215878e commit ebc4458

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

fCraft/Network/PacketReader.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,14 @@ public PacketReader( [NotNull] Stream stream ) :
1313
public OpCode ReadOpCode() {
1414
return (OpCode)ReadByte();
1515
}
16-
16+
17+
byte[] oneBuffer = new byte[1];
18+
public override byte ReadByte() {
19+
int count = BaseStream.Read(oneBuffer, 0, 1);
20+
if (count == -1) throw new EndOfStreamException("End of file");
21+
return oneBuffer[0];
22+
}
23+
1724
public override short ReadInt16() {
1825
return IPAddress.NetworkToHostOrder( base.ReadInt16() );
1926
}

0 commit comments

Comments
 (0)