-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New IBufferWriter<byte>.AsStream() extension #3522
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
New IBufferWriter<byte>.AsStream() extension #3522
Conversation
Thanks Sergio0694 for opening a Pull Request! The reviewers will test the PR and highlight if there is any conflict or changes required. If the PR is approved we will proceed to merge the pull request 🙌 |
Removed "do not merge" tag as #3477 is now merged 🚀 |
Microsoft.Toolkit.HighPerformance/Streams/IBufferWriterStream{TWriter}.Memory.cs
Show resolved
Hide resolved
Hello @michael-hawker! Because this pull request has the p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (
|
@Sergio0694 the build seems to be failing could you try updating the branch one more time? |
@Kyaa-dost Ah, thanks for the ping! That error was caused by #3510 being merged, which bumped the |
PR Type
What kind of change does this PR introduce?
What is the current behavior?
There is currently no way to interoperate between the
IBufferWriter<T>
interface and theStream
class. Many APIs in the BCL and in 3rd party libraries useStream
as the standard way to accept an instance that can be written to or read from, and there is no built-in way to have a memory stream that is also using memory pooling, because none of the types in the BCL and in theHighPerformance
package currently support both features at the same time. This PR fixes that 😄🚀Consider this example that I saw from a user in the C# Discord server:
You can see how the code is very memory inefficient: the
MemoryStream
type will justnew
-up arrays as it goes, and at the endToArray()
is used too, which will duplicate the arrays too. Even by removing that, the main issue withinMemoryStream
remains. With the new extension introduced in this PR, these two APIs can be rewritten much more efficiently, like this:Which heavily leverages all the various APIs and helpers in the
HighPerformance
package, and gives us the following results:This benchmark compresses and decompresses a 1MB buffer, using the two methods detailed above.
You can see the vastly reduced memory allocations using the pooled writer backed stream 🚀
What is the new behavior?
This PR introduces this new extension:
Which helps to interoperate between the
IBufferWriter<T>
interface and theStream
class. In particular, since theHighPerformance
package includes theArrayPoolBufferWriter<T>
type, this extension allows users to use that as aStream
, and then keep working with the resultingReadOnlyMemory<T>
produced by that type, as shown above.PR Checklist
Please check if your PR fulfills the following requirements:
Pull Request has been submitted to the documentation repository instructions. Link:Sample in sample app has been added / updated (for bug fixes / features)Icon has been created (if new sample) following the Thumbnail Style Guide and templates