-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Performance improvement in MemoryStream #3477
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
Merged
13 commits merged into
CommunityToolkit:master
from
Sergio0694:performance/generic-memory-stream
Oct 6, 2020
Merged
Performance improvement in MemoryStream #3477
13 commits merged into
CommunityToolkit:master
from
Sergio0694:performance/generic-memory-stream
Oct 6, 2020
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 🙌 |
42ed136
to
6efa784
Compare
vgromfeld
approved these changes
Sep 21, 2020
7 tasks
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 (
|
michael-hawker
approved these changes
Oct 6, 2020
This pull request was closed.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
auto merge ⚡
high-performance 🚂
Issues/PRs for the Microsoft.Toolkit.HighPerformance package
.NET
Components which are .NET based (non UWP specific)
optimization ☄
Performance or memory usage improvements
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR type:
What kind of change does this PR introduce?
What is the current behavior?
The
MemoryStream
type wraps aMemory<byte>
instance, so every time a read/write operation needs to be performed, it will need to useMemory<T>.Span
, which is relatively expensive to invoke, as it needs to handle multiple possible sources of data. We can use generic types and the value delegate trick (constrained generic struct + interface) to optimize this away.What is the new behavior?
We now use a
MemoryStream<TSource>
type (which is internal anyway) and have a bunch ofIMemorySource
types specifically supportingbyte[]
andMemoryManager<byte>
sources with additional optimizations wherever possible. Also, just using this already removes all the overhead of dynamically creating aSpan<T>
from the wrappedMemory<T>
instance.Here's a benchmark I put together to show the before/after:
The benchmark consists of writing 1024 times a 1024 bytes buffer to a target
Stream
instance.The new
MemoryStream<TSource>
version is almost 25% faster just thanks to this change 🚀PR Checklist
Please check if your PR fulfills the following requirements:
Pull Request has been submitted to the documentation repository instructions.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