last week I got bitten by
System.IO.StreamReader - long story short, I found out that default
bufferSize is set to 1024. Byte array is always allocated when
StreamReader is instantiated. My StackOverflow post here.
I was immediately thinking about improving framework, but then realized it won’t be so easy. I will sum up my ideas below - didn’t want to start issue on GitHub before getting more feedback.
Also I think lowering buffer size seems to be enough. Is this even worth improving? Has this been discussed before?
1. Use ArrayPool
This could be the easiest way how to improve behavior of
StreamReader. Renting array from pool and releasing it on
ArrayPool<T> is also part of common code, so I believe there won’t be problem with referencing it (… but I’m not sure).
However when buffer size is small, using
ArrayPool<T> is not so effective (respective array pool behaves always the same no matter of how big chunk of memory you need, but it is slower than allocating small array directly).
2. Introduce another ctor overload
Allow user to supply
StreamReader with own buffer array (probably rented from pool) or buffer factory - in both cases leaving responsibility on user.
… but I can imagine this could lead to horrible things and more importantly, it would expose internal state of reader. Could this be considered as good design? I don’t think so.
3. Introducing new independent stream reader
Just introducing another stream reader class (which could use above mentioned
ArrayPool<T>) - and I’m absolutely not sure about its name. This seems to be the most clean solution from code perspective, in other hand it creates another way how to solve one thing, making it less intuitive to user.