[Mono-dev] BufferedStream.ReadByte and WriteByte are extremely inefficient
tom.philpot at logos.com
Sun Jan 10 10:35:36 EST 2010
It looks like the implementation of ReadByte and WriteByte in BufferedStream follow the default behavior for Stream by allocating a 1-byte array and then calling Read() or Write(). This is exactly what the MSDN docs for ReadByte and WriteByte ask implementers NOT to do:
Notes to Implementers:
The default implementation on Stream creates a new single-byte array and then calls Read. While this is formally correct, it is inefficient. Any stream with an internal buffer should override this method and provide a much more efficient version that reads the buffer directly, avoiding the extra array allocation on every call.
Granted, most people don't really ever read one byte at a time from a Stream, but in our case, we need to.
In my simple tests, reading 1.5 GB of data using ReadByte from BufferedStream versus FileStream yielded the following the following results (Late 2008 MacBook 13.3" 2.0 Ghz, 5400 rpm disk):
Using default BufferedStream ReadByte implementation: 763.814 seconds
Using FileStream ReadByte implementation: 43.53 seconds
Using default BufferedStream ReadByte implementation: 765.427 seconds
Using FileStream ReadByte implementation: 42.678 seconds
Obviously the alloc and GC cost of this one byte array is huge. I just thought I'd throw this out there in case one of the Mono devs (or someone else) wanted to work on a patch before I got a chance to submit one sometime on Monday.
More information about the Mono-devel-list