Skip to content
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

Use BufferManager for byte array creation #65

Closed
imanushin opened this issue Oct 26, 2018 · 2 comments
Closed

Use BufferManager for byte array creation #65

imanushin opened this issue Oct 26, 2018 · 2 comments

Comments

@imanushin
Copy link

Summary: for better memory re-usage I suggest using BufferManager type (which is available from .Net 3.0).

New byte arrays are created here: https://github.com/Microsoft/Microsoft.IO.RecyclableMemoryStream/blob/master/src/RecyclableMemoryStreamManager.cs#L289

Pros:

  • BufferManager is system type, so it can be tuned per each .Net framework implementation.

Cons:

@buybackoff
Copy link

BufferManager is not the best option, even it's namespace suggests it is intended for particular purpose.

There is shared memory pool optimized for thread local and detecting memory pressure, but it could return size larger than requested, that breaks RMS in the current design and requires a check for length. If the goal is to reduce overall app memory usage and share pools then using the shared pool with the check should be OK, or RMS should be redesigned to use ArraySegments/Memory etc.

There is also a ConfigurableArrayPool but the relevant variable to get exact size MaxBucketsToTry is not configurable. It should be set to zero and != condition in the while loop replaced with <=.

RMS could just extract bucket implementation from ConfigurableArrayPool, it is c.3X faster than ConcurrentStack that is used now. Replacing SpinLock with hand-crafted lock makes it even faster and is appropriate for this case where the work being done inside the lock is literally a couple of instructions.

@imanushin
Copy link
Author

@buybackoff , thank you for detailed investigation.

RMS should be redesigned to use ArraySegments/Memory etc.

I think this is the best option, because we will re-use common solution (which is optimized for now and will be optimized in future).

And I fully agree with "BufferManager is not the best option" according to your arguments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants