mirror of
https://github.com/BepInEx/dotnet-runtime.git
synced 2026-11-24 16:43:13 -10:00
In an application that's using the Redis client library, a significant portion of the allocations are coming from calls to ReadByte on an SslStream. Since SslStream doesn't override ReadByte, the base Stream.ReadByte is used, and it ends up allocating a temporary array to use in a call to Read. This commit adds an override of ReadByte on SslStream that returns data from the stream's internal buffer if any is available, otherwise falling back to the old behavior. On a test that repeatedly writes 1000 bytes and then reads each of those with ReadByte, allocations are reduced by 10x and throughput is doubled. (Note that removing the Interlocked.Exchange in ReadByte yields another 20% improvement in throughput, but I've left it in so as not to impact guarantees of the type.) Commit migrated from https://github.com/dotnet/corefx/commit/b999431c9c519a797aff3eb472b614e0f15c6650