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

Question: GetStream copy existing buffer use case #295

Closed
osexpert opened this issue Apr 23, 2023 · 1 comment
Closed

Question: GetStream copy existing buffer use case #295

osexpert opened this issue Apr 23, 2023 · 1 comment

Comments

@osexpert
Copy link

osexpert commented Apr 23, 2023

docs says:

You can also provide an existing buffer. It’s important to note that the data from this buffer will be copied into a buffer owned by the pool:

var stream = manager.GetStream("Program.Main", sourceBuffer, 
                                    0, sourceBuffer.Length);

But if using MemoryStream, no copy will be done?

var stream = new MemoryStream(sourceBuffer);

So who want to copy (and double the amount if memory used) vs. just wrap it, like MemoryStream does?

I made this mistake myself, before I read the docs I just changed all use of MemoryStream to RecyclableMemoryStream, but then I read that GetStream(byte[]) copies and changed all cases where MemoryStream was used for reading, back to MemoryStream.

I think this feature is weird (GetStream copy existing data) and could make more harm (people using as drop in replacement without reading\understanding the docs and GetStream(byte[]) increase memory use) than good? Or maybe it has a good use case, maybe an example would be nice.

Thanks.

@benmwatson
Copy link
Member

I would never recommend using this library without reading the documentation. There are lots of gotchas every time you take memory management on yourself, and this is a good example.

For cases where you just want to wrap an existing buffer in a stream, the existing MemoryStream is good.

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

2 participants