The fields in this class and its base are not encapsulated properly for performance reasons. For example, Buffer.this along with Index should be replaced with a method like GetBufferedByte()
. However, doing so costs more than 30% of the throughput in dependent classes when run on the windows phone emulator. This most likely stems form the fact that the CF JIT is only able to inline very simple methods. Apparently, even a method with the one-liner return this.buffer[this.index++];
is not eligible for inlining.
A frequent use case for this class is when a not previously known number of bytes need to be read from a stream one by one. In this case the following code tends to be much faster than calling Stream.ReadByte for each byte: void ReadFromStream(Stream stream) { var readBuffer = new ReadBuffer(stream.Read, 1024); while ((readBuffer.Index < readBuffer.Count) || readBuffer.Read()) { var theByte = readBuffer[readBuffer.Index++]; // Do something with the byte. } }