Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Rønne Petersen <alpeters@microsoft.com>2017-09-19 21:32:26 +0300
committerAlex Rønne Petersen <alpeters@microsoft.com>2017-09-20 15:17:26 +0300
commitdcbbe94d4e9c0cab8848946797a916f4ae0bdc1d (patch)
tree8a452be12813fabdd36c0e3d5c788f98e9d6dbfe /mcs/class/Mono.Profiler.Log
parentef2f4f2de28bc4db350ff4cff0d1488babaf2db2 (diff)
[Mono.Profiler.Log] Fix excessive byte array allocations.
The problem was that the base Stream.ReadByte () method allocates a temporary byte array and then calls Read (..., 1) on it. To solve this, we override ReadByte () in LogStream and use a private buffer to hold the result. It's a bit of a mystery to me why Stream.ReadByte () does it this way. The documentation for Stream explicitly says that instance methods aren't thread safe, so it would be perfectly fine for Stream.ReadByte () to do what we do here...
Diffstat (limited to 'mcs/class/Mono.Profiler.Log')
-rw-r--r--mcs/class/Mono.Profiler.Log/Mono.Profiler.Log/LogStream.cs10
1 files changed, 10 insertions, 0 deletions
diff --git a/mcs/class/Mono.Profiler.Log/Mono.Profiler.Log/LogStream.cs b/mcs/class/Mono.Profiler.Log/Mono.Profiler.Log/LogStream.cs
index 4fd0daf227d..b52402a91d4 100644
--- a/mcs/class/Mono.Profiler.Log/Mono.Profiler.Log/LogStream.cs
+++ b/mcs/class/Mono.Profiler.Log/Mono.Profiler.Log/LogStream.cs
@@ -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);