-
Notifications
You must be signed in to change notification settings - Fork 12
fix(perf): keep PerfStress responsive at 250/500 elements #321
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
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| using Microsoft.UI.Dispatching; | ||
|
|
||
| namespace Microsoft.UI.Reactor.Hosting; | ||
|
|
||
| /// <summary> | ||
| /// Decides the dispatcher-queue priority for the next render enqueue based on | ||
| /// recent render duration. | ||
| /// <para> | ||
| /// When a render is faster than a 60 Hz frame, the next render is enqueued at | ||
| /// <see cref="DispatcherQueuePriority.Normal"/> — the default, lowest latency. | ||
| /// When the previous render exceeded the budget, subsequent renders are demoted | ||
| /// to <see cref="DispatcherQueuePriority.Low"/> so that input, layout, and paint | ||
| /// messages on the same UI thread are interleaved between renders. Without this, | ||
| /// a high-frequency state-change source (animation, simulation, streaming data) | ||
| /// can fill the dispatcher with back-to-back renders that starve pointer/keyboard | ||
| /// input — the app feels frozen even though renders are still committing pixels. | ||
| /// </para> | ||
| /// </summary> | ||
| internal static class RenderPriorityPolicy | ||
| { | ||
| /// <summary> | ||
| /// Render-duration ceiling beyond which subsequent renders are demoted to | ||
| /// Low priority. 16 ms is one 60 Hz frame; a render past this point gives | ||
| /// up its Normal-priority slot so input/layout/paint catch up. | ||
|
codemonkeychris marked this conversation as resolved.
|
||
| /// </summary> | ||
| public const double DefaultFrameBudgetMs = 16.0; | ||
|
|
||
| /// <summary> | ||
| /// Decide the priority for the next render enqueue. | ||
| /// Returns <see cref="DispatcherQueuePriority.Low"/> when the last render | ||
| /// exceeded the budget, <see cref="DispatcherQueuePriority.Normal"/> | ||
| /// otherwise (including the cold-start case where no render has run yet | ||
| /// and <paramref name="lastRenderMs"/> is 0). | ||
| /// </summary> | ||
| public static DispatcherQueuePriority PickPriority( | ||
| double lastRenderMs, | ||
| double budgetMs = DefaultFrameBudgetMs) | ||
| => lastRenderMs > budgetMs | ||
| ? DispatcherQueuePriority.Low | ||
| : DispatcherQueuePriority.Normal; | ||
| } | ||
119 changes: 119 additions & 0 deletions
119
tests/Reactor.Tests/Hosting/RenderPriorityPolicyTests.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| using Microsoft.UI.Dispatching; | ||
| using Microsoft.UI.Reactor.Hosting; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.UI.Reactor.Tests.Hosting; | ||
|
|
||
| /// <summary> | ||
| /// The render-loop responsiveness fix for PerfStress at 250/500 elements: | ||
| /// when a render exceeds the 60 Hz frame budget, the host enqueues the next | ||
| /// render at <see cref="DispatcherQueuePriority.Low"/> so the message pump can | ||
| /// interleave input/layout/paint. Without this, back-to-back Normal-priority | ||
| /// renders triggered from <c>await Task.Delay</c> continuations starve UI | ||
| /// input and the app feels frozen until the work finishes. | ||
| /// | ||
| /// These tests pin the policy's decisions independent of the dispatcher so | ||
| /// regressions show up at unit-test time rather than as a UI freeze in the | ||
| /// PerfStress demo. | ||
| /// </summary> | ||
| public class RenderPriorityPolicyTests | ||
| { | ||
| [Fact] | ||
| public void ColdStart_UsesNormalPriority() | ||
| { | ||
| // Before any render has run, lastRenderMs == 0. The host MUST use | ||
| // Normal priority — Low-priority cold-start would queue behind every | ||
| // pending dispatcher item and delay first paint noticeably. | ||
| Assert.Equal( | ||
| DispatcherQueuePriority.Normal, | ||
| RenderPriorityPolicy.PickPriority(lastRenderMs: 0)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void FastRender_StaysAtNormalPriority() | ||
| { | ||
| // A render that fits inside one 60 Hz frame keeps Normal priority. | ||
| // Demoting fast renders would add latency for no benefit. | ||
| Assert.Equal( | ||
| DispatcherQueuePriority.Normal, | ||
| RenderPriorityPolicy.PickPriority(lastRenderMs: 8)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void RenderAtFrameBoundary_StaysAtNormalPriority() | ||
| { | ||
| // Exactly at the budget is treated as "fit" — only renders strictly | ||
| // longer than the budget demote. This avoids flip-flopping when a | ||
| // render lands right at the boundary. | ||
| Assert.Equal( | ||
| DispatcherQueuePriority.Normal, | ||
| RenderPriorityPolicy.PickPriority(lastRenderMs: RenderPriorityPolicy.DefaultFrameBudgetMs)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SlowRender_DemotesToLowPriority() | ||
| { | ||
| // This is the PerfStress fix: a render that exceeds one frame budget | ||
| // moves the next enqueue to Low priority. The PerfStress scenario | ||
| // tops out at ~100 ms/render at 500 elements — well past the budget. | ||
| Assert.Equal( | ||
| DispatcherQueuePriority.Low, | ||
| RenderPriorityPolicy.PickPriority(lastRenderMs: 100)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void JustOverBudget_DemotesToLowPriority() | ||
| { | ||
| // Even a small overrun (just past the 16 ms ceiling) demotes — | ||
| // the goal is to keep the dispatcher from monopolizing the UI thread. | ||
| Assert.Equal( | ||
| DispatcherQueuePriority.Low, | ||
| RenderPriorityPolicy.PickPriority(lastRenderMs: RenderPriorityPolicy.DefaultFrameBudgetMs + 0.5)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CustomBudget_DemotesPastCustomCeiling() | ||
| { | ||
| // A perf-sensitive host can raise or lower the ceiling. The policy | ||
| // honors the override. | ||
| Assert.Equal( | ||
| DispatcherQueuePriority.Low, | ||
| RenderPriorityPolicy.PickPriority(lastRenderMs: 12, budgetMs: 8)); | ||
|
|
||
| Assert.Equal( | ||
| DispatcherQueuePriority.Normal, | ||
| RenderPriorityPolicy.PickPriority(lastRenderMs: 12, budgetMs: 32)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void DefaultBudget_IsOneFrameAt60Hz() | ||
| { | ||
| // Pin the budget so a future "just bump it up" change is a conscious | ||
| // edit, not a silent regression. | ||
|
codemonkeychris marked this conversation as resolved.
|
||
| Assert.Equal(16.0, RenderPriorityPolicy.DefaultFrameBudgetMs); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ReactorHost_TracksLastRenderMs() | ||
| { | ||
| // The wiring contract: ReactorHost stores the most-recent render | ||
| // duration so RequestRender can consult RenderPriorityPolicy. | ||
| // Without this field, the policy can't see a slow render and the | ||
| // PerfStress regression returns. | ||
| var field = typeof(ReactorHost).GetField("_lastRenderMs", | ||
| global::System.Reflection.BindingFlags.NonPublic | global::System.Reflection.BindingFlags.Instance); | ||
| Assert.NotNull(field); | ||
| Assert.Equal(typeof(double), field!.FieldType); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ReactorHostControl_TracksLastRenderMs() | ||
| { | ||
| // Symmetric contract with ReactorHost — embedded hosts must also | ||
| // demote to Low priority on slow renders. | ||
| var field = typeof(ReactorHostControl).GetField("_lastRenderMs", | ||
| global::System.Reflection.BindingFlags.NonPublic | global::System.Reflection.BindingFlags.Instance); | ||
| Assert.NotNull(field); | ||
| Assert.Equal(typeof(double), field!.FieldType); | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.