Skip to content

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

Conversation

Sergio0694
Copy link
Member

@Sergio0694 Sergio0694 commented Sep 15, 2020

PR type:

What kind of change does this PR introduce?

  • Performance improvement

What is the current behavior?

The MemoryStream type wraps a Memory<byte> instance, so every time a read/write operation needs to be performed, it will need to use Memory<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 of IMemorySource types specifically supporting byte[] and MemoryManager<byte> sources with additional optimizations wherever possible. Also, just using this already removes all the overhead of dynamically creating a Span<T> from the wrapped Memory<T> instance.

Here's a benchmark I put together to show the before/after:

image

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:

  • Tested code with current supported SDKs
  • Pull Request has been submitted to the documentation repository instructions.
  • Sample in sample app has been added / updated (for bug fixes / features)
  • Tests for the changes have been added (for bug fixes / features) (if applicable)
  • Header has been added to all new source files (run build/UpdateHeaders.bat)
  • Contains NO breaking changes

@Sergio0694 Sergio0694 added high-performance 🚂 Issues/PRs for the Microsoft.Toolkit.HighPerformance package optimization ☄ Performance or memory usage improvements .NET Components which are .NET based (non UWP specific) labels Sep 15, 2020
@ghost
Copy link

ghost commented Sep 15, 2020

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 🙌

@ghost ghost requested review from michael-hawker, azchohfi and Kyaa-dost September 15, 2020 23:18
@Sergio0694 Sergio0694 force-pushed the performance/generic-memory-stream branch from 42ed136 to 6efa784 Compare September 16, 2020 00:43
@ghost
Copy link

ghost commented Oct 6, 2020

Hello @michael-hawker!

Because this pull request has the auto merge label, I will be glad to assist with helping to merge this pull request once all check-in policies pass.

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 (@msftbot) and give me an instruction to get started! Learn more here.

@michael-hawker michael-hawker added this to the 7.0 milestone Oct 6, 2020
@ghost ghost merged commit 5fce180 into CommunityToolkit:master 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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants