Description
In .NET 6 we have introduced new type called RandomAccess
that allows for reading and writing to specific file offset.
As of today, all it's Read*
and Write*
methods throw when given handle points to a non-seekable file like socket or pipe:
But it's not a problem for it's internal implementation (used by FileStream
):
And we use it's internal API surface to workaround this limitation is CoreLib
(mind the call to RandomAccess.WriteAtOffset (internal), not RandomAccess.Write (public):
We should relax the public requirements and make RandomAccess
work with non-seekable files.
This is going to require:
- a breaking change doc
- implementation change (stop throwing exception)
- test changes (this test shows how to easily create a handle that points to non-seekable pipe)
Everything motioned above should be a single PR. In the same or separate PR, the Unix implementation of overloads that accept multiple buffers should start using readv
and writev
sys-calls. This should be relatively easy (just search for preadv
and pwritev
and reuse the patterns)
But it's going to allow to:
- switch from heavy
FileStream
to a very lightweightRandomAccess
everywhere where we don't need buffering in BCL (and ofc outside of BCL). Sample perf gains: File.*AllText* optimizations #58167 (comment) - remove a lot of code duplication. Example: https://github.com/dotnet/runtime/blob/main/src/libraries/System.IO.Pipes/src/System/IO/Pipes/PipeStream.ValueTaskSource.cs