-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Proposal] Passing custom buffer to FileStream #52321
Comments
Tagging subscribers to this area: @carlossanlop Issue DetailsBackground and Motivation
Proposed APIpublic class FileStream
{
public FileStream(string path, FileMode mode, FileAccess access, FileShare share, Memory<byte> buffer, FileOptions options)
} Usage Examplesvar buffer = ArrayPool<byte>.Shared.Rent(4096);
using var fs = new FileStream("dummy.txt", FileMode.OpenOrCreate, FileAccess.Wrtie, FileShare.None, buffer.AsMemory(), FileOptions.None); Alternative DesignsTo mitigate risk of sharing the same buffer between different instances of file stream we could offer passing of memory pool: public class FileStream
{
public FileStream(string path, FileMode mode, FileAccess access, FileShare share, ArrayPool<byte> buffer, FileOptions options);
public FileStream(string path, FileMode mode, FileAccess access, FileShare share, MemoryPool<byte> buffer, FileOptions options);
} Risks
|
Pass a buffer size of 1 a d you'll disable the FileStream's internal buffer. This shouldn't be needed |
Not sure that this works on Linux/MacOS/FreeBSD. In .NET 6, |
That strategy should work on all platforms (or it's a bug). You pass a buffer to FileStream anyways. Why would you want to have it use the array pool rather than do that yourself? It's much better and you can pre-pin buffers to avoid fragmentation. I don't think we should add new overloads. |
That's why overload with |
Btw, the current implementation in .NET 6 doesn't use pinned arrays. |
pre-pinned arrays are placed into Gen2 so we must be careful to only use them when we expect the array to last a while. |
They're placed in the POH actually which is different than Gen2 and this would be a bad usage of the POH. |
It is different than Gen2 internally in the GC. From the outside, you cannot tell the difference. POH allocations put pressure on Gen2 GCs with the current implementation. #51363 (comment) |
Yea, I was being pedantic 😄 |
I've created a new proposal related to this feature request: #52446 |
OK, we can move any further discussion to one place. Closing issue. |
Background and Motivation
FileStream
has its own buffer for I/O. But it cannot be passed from outside. If the application requires a lot ofFileStream
objects with short lifetime there is not way to reuse the buffer between them. For instance, logging to files.Rotation of log files leads to dispose of old streams and create new ones. Also, it's reasonable to use memory pool (e.g.ArrayPool<T>
orMemoryPool<T>
) as a source of such buffers.Proposed API
Usage Examples
Alternative Designs
To mitigate risk of sharing the same buffer between different instances of file stream we could offer passing of memory pool:
Risks
FileStream
still in useThe text was updated successfully, but these errors were encountered: