Skip to content

WeakReferenceMessenger automatic cleanup #4050

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 May 27, 2021

PR Type

What kind of change does this PR introduce?

  • Improvements
  • Optimizations

What is the current behavior?

The WeakReferenceMessenger requires users to manually call Cleanup() to perform trimming of internal structures. This is inconvenient for consumers (When should they call this method? Why should they have to worry about this?), or at least, it would be more convenient if the whole behavior was just automatic.

What is the new behavior?

This PR introduces automatic trimming, by leveraging a customized version of the Gen2GcCallback class to register a cleanup callback that will take care of trimming the messenger data structures if needed. Additionally this PR contains some improvements to nullability annotations, as well as one more minor performance improvements still within WeakReferenceMessenger.

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)
  • New major technical changes in the toolkit have or will be added to the Wiki e.g. build changes, source generators, testing infrastructure, sample creation changes, etc...
  • 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

Other information

@Sergio0694 Sergio0694 added improvements ✨ optimization ☄ Performance or memory usage improvements .NET Components which are .NET based (non UWP specific) mvvm-toolkit 🧰 Issues/PRs for the Microsoft.Toolkit.Mvvm package labels May 27, 2021
@ghost
Copy link

ghost commented May 27, 2021

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, Kyaa-dost and Rosuavio May 27, 2021 15:52
@Sergio0694 Sergio0694 changed the title Feature/weak messenger callback WeakReferenceMessenger automatic cleanup May 27, 2021
@Sergio0694 Sergio0694 force-pushed the feature/weak-messenger-callback branch 2 times, most recently from 277b7ea to 1254cb7 Compare May 29, 2021 22:01
@Sergio0694 Sergio0694 marked this pull request as ready for review May 31, 2021 11:14
@Sergio0694
Copy link
Member Author

Marking this as ready for review - even if we managed to get dotnet/runtime#53296 approved, that'd still be a separate improvement for WeakReferenceMessenger that we can do in another PR to add .NET 6 support as well (and branched off from #3998). The new auto-cleanup callback looks good on its own in the meantime, so worth taking this in already 🙂

@Sergio0694 Sergio0694 force-pushed the feature/weak-messenger-callback branch from 1254cb7 to 6c9fbd7 Compare June 1, 2021 20:40
Copy link
Member

@michael-hawker michael-hawker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming all the internal changes are covered by the existing unit tests.

Assume that devs still calling Cleanup are also still fine, this change is also now that it's OK if they don't?

{
/// <summary>
/// Schedules a callback roughly every gen 2 GC (you may see a Gen 0 an Gen 1 but only once).
/// Ported from https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Gen2GcCallback.cs.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wondering if we want to make this a permalink or pin to a certain release for historical reference/comparison in the future?

Otherwise since it's just .NET Foundation code as well, we should be fine with just a link from a license perspective. Though wonder if we want a separate thing to call out other .NET Foundation code we've been using somewhere... 🤔

@clairernovotny do other projects do this somehow that we can refer to? Since we're all the same organization under the same license I don't think there's a legal attribution we need to follow, but was just thinking it could be nice from an evangelism perspective.

@Sergio0694
Copy link
Member Author

"Assuming all the internal changes are covered by the existing unit tests."

Yup, though I've also just added one more test specifically for the automatic cleanup in d326912 just to be extra careful 😄

"Assume that devs still calling Cleanup are also still fine, this change is also now that it's OK if they don't?"

Exactly - it's not a breaking change and calling Cleanup() manually will still work just as fine, but with this PR it'll just no longer be needed and the messenger will take care of automatically trimming all its internal data whenever there's memory pressure 👍

@ghost
Copy link

ghost commented Jun 9, 2021

Hello @michael-hawker!

Because this pull request has the auto merge :zap: 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.1 milestone Jun 29, 2021
@michael-hawker
Copy link
Member

@Sergio0694 there's a conflict here now. You want to resolve?

@Sergio0694 Sergio0694 force-pushed the feature/weak-messenger-callback branch from 69f5b75 to e0a8335 Compare July 14, 2021 00:07
@Sergio0694
Copy link
Member Author

@michael-hawker Done, should be good to review! 😄

@michael-hawker michael-hawker added the next preview ✈️ Label for marking what we want to include in the next preview release for developers to try. label Jul 15, 2021
@michael-hawker michael-hawker merged commit ae07326 into CommunityToolkit:main Jul 22, 2021
@Sergio0694 Sergio0694 deleted the feature/weak-messenger-callback branch July 22, 2021 20:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto merge ⚡ improvements ✨ mvvm-toolkit 🧰 Issues/PRs for the Microsoft.Toolkit.Mvvm package .NET Components which are .NET based (non UWP specific) next preview ✈️ Label for marking what we want to include in the next preview release for developers to try. optimization ☄ Performance or memory usage improvements priority 🚩
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants