[Mono.Profiler.Log] Fix excessive byte array allocations.
[mono.git] / mcs / class / Mono.Profiler.Log / Mono.Profiler.Log / LogStream.cs
index 4fd0daf227d415e4cdfd69fa2f5e6a910a31a95b..b52402a91d43e23a359acfadb0fb14a61d7ffae2 100644 (file)
@@ -26,6 +26,8 @@ namespace Mono.Profiler.Log {
                        set => throw new NotSupportedException ();
                }
 
+               readonly byte[] _byteBuffer = new byte [1];
+
                public LogStream (Stream baseStream)
                {
                        if (baseStream == null)
@@ -48,6 +50,14 @@ namespace Mono.Profiler.Log {
                        throw new NotSupportedException ();
                }
 
+               public override int ReadByte ()
+               {
+                       // The base method on Stream is extremely inefficient in that it
+                       // allocates a 1-byte array for every call. Simply use a private
+                       // buffer instead.
+                       return Read (_byteBuffer, 0, sizeof (byte)) == 0 ? -1 : _byteBuffer [0];
+               }
+
                public override int Read (byte[] buffer, int offset, int count)
                {
                        return BaseStream.Read (buffer, offset, count);