-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Allow to supply an external buffer to FileStream #1429
Conversation
… won't be allocating its own buffers. That allow to use common / pool buffer when using large number of FileStream
|
Hi @ayende, I'm your friendly neighborhood .NET Foundation Pull Request Bot (You can call me DNFBOT). Thanks for your contribution! TTYL, DNFBOT; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In your example, the method is called SetExternalBuffer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, slip of the keyboard while writing the comment. I think that the name SetBuffer is better, with the externalBuffer name indicating what is going on.
|
@ayende, Thanks for signing the contribution license agreement so quickly! Actual humans will now validate the agreement and then evaluate the PR. |
|
@ayende Thank you for suggesting this improvement. Could you please follow the API review process for this one since it is a public API addition? From Contributing to .NET Core: DON'T add API additions without filing an issue and discussing with us first. See API Review Process. |
|
A few thoughts:
|
|
I closed the PR and opened: https://github.com/dotnet/corefx/issues/2961 |
The idea is to avoid 4KB allocation for buffer whenever we need to work with large number of files.
Consider the following code:
The problem is that each instance of FileStream will allocate an independent buffer. If we are reading 10,000 files, that will result in 40MB(!) being allocated, even if we are very careful about allocations in general.
This PR adds a method, SetExternalBuffer(byte[]) which allows the caller to send an external buffer, which they can manage on their own, to the FileStream.
This gives callers the chance to pool the buffer or share it between multiple FileStream instances (obviously with only one stream using the buffer at at time.
Sample usage:
And now we have no more allocations. The cost of calling this code went from 40MB to 4KB.