Skip to content
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

Optimizations/refactoring/tweaks to DispatcherQueueHelper #3498

Merged

Conversation

Sergio0694
Copy link
Member

Follow up for #3206

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Code style update (formatting)
  • Refactoring (no functional changes, no api changes)
  • Optimization

What is the new behavior?

This PR includes a number of optimizations, tweaks and refactorings to DispatcherQueueHelper:

  • Removed unnecessary null checks for function, and enabled nullability annotations. Those null checks were unnecessary since there were also other parameters that could also be null (ie. the actual DispatcherQueue) anyway, plus we'd still get an exception anyway when those instances are accessed. Instead, we now have nullability annotations to make the intent clearer.
  • Avoided the slowdown and memory allocations in case the thread already had access to the dispatcher queue, which was caused by the C# compiler (sadly) always preemptively instantiating the display class for the closure of the lambda expression used in the other branch (I made a sharplab repro here that illustrates the issue). Instead now the fallback path is moved to a separate method, so that the IL has the correct codegen.
  • Renamed the class to DispatcherQueueExtensions, since these are all extensions and not a helper class
  • Similarly, moved the class to the .Extensions namespace
  • Renamed the APIs to EnqueueAsync to mirror the TryEnqueue name of the actual API in the DispatcherQueue class. This better follows the naming convention of the class, and it's also clearer as in case users have multiple windows and different dispatching queues, there isn't really a single "UI thread" - the API is literally just enqueueing an operation on a given dispatcher queue.
  • Added a check in case TryEnqueue fails, so if that happens we now return a wrapped InvalidOperationException in the Task, whereas the previous behavior would've just caused that task to never be completed, leaving the caller just waiting forever.

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

@ghost
Copy link

ghost commented Sep 22, 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 22, 2020 23:14
@@ -0,0 +1,251 @@
// Licensed to the .NET Foundation under one or more agreements.
Copy link
Member

Choose a reason for hiding this comment

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

Did you git mv this file as I'm surprised it didn't mark it as a move...

Copy link
Member Author

Choose a reason for hiding this comment

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

I just moved it as usual through Visual Studio, not sure why it wasn't picked up 🤔
Maybe it's because I also renamed the file in the process?

Copy link
Contributor

Choose a reason for hiding this comment

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

You must move first [commit] and then rename. You can do both in one commit

@ghost
Copy link

ghost commented Sep 23, 2020

This PR has been marked as "needs attention 👋" and awaiting a response from the team.

@ghost
Copy link

ghost commented Sep 24, 2020

This PR has been marked as "needs attention 👋" and awaiting a response from the team.

@michael-hawker michael-hawker added auto merge ⚡ next preview ✈️ Label for marking what we want to include in the next preview release for developers to try. and removed needs attention 👋 labels Nov 10, 2020
@ghost
Copy link

ghost commented Nov 10, 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 Nov 10, 2020
@ghost ghost added the in progress 🚧 label Nov 10, 2020
@michael-hawker
Copy link
Member

@azchohfi think we're all good here now?

@ghost ghost merged commit 9d90b62 into CommunityToolkit:master Nov 10, 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 ⚡ extensions ⚡ improvements ✨ in progress 🚧 introduce breaking changes 💥 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 XamlIslands 🏝️
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants