Skip to content

Fixed ArrayPoolBufferWriter<T> repeated new[] allocations #3524

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
8 commits merged into from
Nov 12, 2020

Conversation

Sergio0694
Copy link
Member

PR Type

What kind of change does this PR introduce?

  • Optimization

What is the current behavior?

The ArrayPoolBufferWriter<T> uses the given ArrayPool<T> instance to resize its internal buffer when needed, which only works as expected when the array itself is small. The issue is that the ArrayPool<T>.Shared instance has an internal threshold set to 1024 * 1024, over which it just allocates new arrays every time to avoid keeping very large arrays alive for a long time. That is perfectly fine, except for one little detail: once you get past that threshold, ArrayPool<T>.Shared stops rounding up the requested size. It instead returns an array with new[] of exactly the requested size, which absolutely kills the performance when used in a writer type like ArrayPoolBufferWriter<T>: this means that as soon as we get past tha threshold, we'll basically end up resizing the whole array for every single new write operation, no matter how large it is. That's super bad for performance and memory usage 🥺

What is the new behavior?

The solution for this is pretty simple, this PR includes a simple check for the requested size, and if that's over 1024 * 1024 it just rounds that up to the closest power of 2, so that the array size will effectively just keep being multiplied by 2 every time. This has a huge performance impact when eg. trying to use the ArrayPoolBufferWriter<T> class to write a 10MB buffer, 8KB at a time:

Method Mean Error StdDev Ratio Gen 0 Gen 1 Gen 2 Allocated
Before 1,380.236 ms 19.9806 ms 17.7122 ms 1.000 355000.0000 355000.0000 355000.0000 5754.44 MB
After 8.820 ms 0.1757 ms 0.4838 ms 0.006 640.6250 640.6250 640.6250 30 MB

In this simple benchmark alone, the updated version is 156x faster and uses 190x less memory 😄
Of course, results will vary a lot on the specific workload, but you can imagine the impact being even more dramatic when working with larger buffers, or with less items being written at any given time. With this change in general, users will not have to worry about the size of the data being written, and the class will automatically use the right approach in all cases.

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. Link:
  • 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 Oct 5, 2020
@Sergio0694 Sergio0694 added this to the 7.0 milestone Oct 5, 2020
@ghost
Copy link

ghost commented Oct 5, 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 October 5, 2020 20:42
@ghost
Copy link

ghost commented Nov 12, 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.

@ghost ghost merged commit d434f43 into CommunityToolkit:master Nov 12, 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.

3 participants