Repository navigation
perf(theming): Cut ~922 KB of boot allocation from theme dictionary materialization - #24539
Conversation
|
Claude finished @MartinZikmund's task in 3m 32s —— View job Review complete ✅
Verdict: fix-first (one finding, not blocking-severity, but worth addressing before merge)Findings by severity: 1 medium (test coverage / hygiene) — posted inline on What I verified as correct
Cross-cutting concern (not tied to one line)The perf win itself is real and well-reasoned, but it's currently unverifiable by CI: the sticky Note on validation: this was a code-review by inspection (diff read, static-flow tracing, cross-referencing call sites and value tables) — I did not build or run the test suite myself in this session; the PR's own reported test results (43/43 runtime tests, 4164 unit tests with only the known pre-existing |
There was a problem hiding this comment.
🔵 Needs a closer look
Clear the high-contrast state latch when no states remain and add coverage for the unmaterialized-resource early-return path.
Pull request overview
This PR reduces startup allocations by avoiding unnecessary high-contrast resource materialization and moving acrylic opacity values into Fluent XAML.
Changes:
- Defers high-contrast restoration until resource overrides exist.
- Removes eager acrylic brush patching.
- Defines
TintLuminosityOpacityvalues directly in theme resources.
File summaries
| File | Summary |
|---|---|
src/Uno.UI/UI/Xaml/ResourceResolver.cs |
Adds a high-contrast materialization guard; the latch should be cleared when the final resource state is removed, with regression coverage for the early-return path. |
src/Uno.UI.FluentTheme/XamlControlsResources.cs |
Removes constructor-time acrylic resource materialization. |
src/Uno.UI.FluentTheme/Resources/Priority02/AcrylicBrush_themeresources.xaml |
Defines acrylic luminosity values inline for theme dictionaries. |
Review details
Suppressed comments (2)
src/Uno.UI/UI/Xaml/ResourceResolver.cs:69
- This latch is never reset when the restore path removes the last
HighContrastResourceStateat line 1406. After one high-contrast on→off transition, every later non-high-contrast notification still walks the entire graph and probes all four theme dictionaries; newly loaded dictionaries can therefore be materialized even though there is no state to restore. Track live state entries (or otherwise clear the guard when the final state is removed) instead of permanently recording that a state existed.
private static bool _hasHighContrastResourceStates;
src/Uno.UI/UI/Xaml/ResourceResolver.cs:1290
- The allocation-saving branch itself has no regression test. The existing unit coverage calls
restoreDefaults: trueonly after an active high-contrast pass, so the latch is already set and this early return is not exercised; add coverage with an unmaterializedHighContrast*WeakResourceInitializerand assert its factory is not called by the first non-high-contrast restore.
if (restoreDefaults && !_hasHighContrastResourceStates)
{
return;
- Files reviewed: 3/3 changed files
- Comments generated: 0
- Review effort level: Lite
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-24539/wasm-skia-net9/index.html |
|
The build 234217 found UI Test snapshots differences: Details
|
|
|
UpdateSystemColorAndBrushResources is called from NotifyThemeChange with restoreDefaults: !HasHighContrastTheme(), so on a machine that is not in high contrast it walked the whole merged-dictionary graph, probing four HighContrast theme dictionaries per node. Those probes go through TryMaterializeLazy, so the dictionaries were built - and then nothing was written, because shouldUpdateResources requires either high contrast to be active or a previously recorded override, and an override can only ever be recorded from inside that same branch. Restoring defaults before any override exists therefore cannot change a value. Return early until the first override is recorded, which also keeps the HighContrast dictionaries lazy. ConditionalWeakTable exposes no count, so the flag is set in the GetValue factory. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012QzKQawubFcPPX9JSoZLkX
XamlControlsResources' constructor read ThemeDictionaries["Default"] and ["Light"] to assign 20 TintLuminosityOpacity doubles that our ported Fluent dictionaries omit. TryGetValue materializes lazy values, so a Light-themed app built the entire Dark theme dictionary before its first frame, and vice versa, although only one of the two can ever be the active theme. Set the values inline on the AcrylicBrush resources the way WinUI does. The ported MUX comment claiming nullable doubles cannot be set from XAML describes MUX's own compiler bug (it cites XAMLControlsResources.cpp); Uno's XAML generator unwraps nullable property types. Given_AcrylicBrush.When_TintLuminosityOpacity_Is_Set_By_ThemeResources covers the resolved values. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012QzKQawubFcPPX9JSoZLkX
Lets unit tests drop recorded high-contrast overrides so the one-way _hasHighContrastResourceStates latch does not leak between tests. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_019C454B8usXZRfvcjWT1BB4
Asserts that restoring defaults before any override leaves the HighContrast theme dictionary unmaterialized, and that the walk runs again once an override has been recorded. Resets high-contrast state after each test. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_019C454B8usXZRfvcjWT1BB4
c364497 to
823b3e3
Compare
|
Claude finished @MartinZikmund's task in 4m 9s —— View job Review complete ✅
Verdict: ship — no blocking findingsThis push ( What changed since the last review, and how it holds up:
Cross-cutting note (non-blocking)The Azure Pipelines run linked in the comments (build 234217, predates this push) reported ~2310/2408 changed Windows screenshots and a pipeline failure. That volume is plausible as an expected consequence of this fix — |
There was a problem hiding this comment.
🟡 Changes recommended
Clear _hasHighContrastResourceStates after restoration to preserve the allocation optimization.
Get a fresh assessment by requesting another Copilot review.
Review details
- Files reviewed: 4/4 changed files
- Comments generated: 1
- Review effort level: Lite
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-24539/wasm-skia-net9/index.html |
The high-contrast restore walk stayed on for the rest of the process after the first activation, because the state flag never reset. Count live override states instead and decrement when a restore removes one, so the walk is skipped again once nothing is left. Entries lost to GC only keep the count high, which just costs a no-op walk. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_019C454B8usXZRfvcjWT1BB4
Asserts that once every recorded high-contrast override has been restored, a later restore-defaults walk leaves HighContrast theme dictionaries unmaterialized. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_019C454B8usXZRfvcjWT1BB4
|
Claude finished @MartinZikmund's task in 1m 9s —— View job Review complete ✅
Verdict: ship — no blocking findingsThis push ( What changed and how it holds up:
No cross-cutting concerns beyond what's already tracked in the PR checklist (screenshot-compare results pending CI). |
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-24539/wasm-skia-net9/index.html |
|
The build 234511 found UI Test snapshots differences: Details
|
morning4coffe-dev
left a comment
There was a problem hiding this comment.
LGTM! Keeping HighContrast theme dictionaries lazy when no high-contrast override is active saves ~922 KB of boot allocations across all platforms. Great performance improvement with unit tests.
|
This pull request does not currently match the merge queue conditions, so it cannot be queued from here. The box comes back if it matches again. |
GitHub Issue: closes #24538
PR Type:
💬 Other... Performance — allocation reduction on the startup path. No API change, no behaviour change.
What changed? 🚀
Two startup paths built theme dictionaries the application can never observe. Together they cost about 922 KB of allocation before the first frame, on every launch, on every platform.
1.
ResourceResolver.UpdateSystemColorAndBrushResources— skip the high-contrast restore walk until there is something to restore.CoreServices.NotifyThemeChange()calls it withrestoreDefaults: !Theming.HasHighContrastTheme(), which istrueon any machine not in high contrast. It then walked the whole merged-dictionary graph, probing four theme dictionaries per node (HighContrast,HighContrastBlack,HighContrastWhite,HighContrastCustom). Those probes go throughTryGetThemeDictionary→TryGetValue→TryMaterializeLazy, so the HighContrast dictionaries of the entire Fluent graph were materialized — and then nothing was written, becauseshouldUpdateResourcesis!restoreDefaults || <an override was recorded>and no override can exist yet.An override is only ever recorded from inside that same branch, which requires high contrast to be active. So restoring defaults before any override has been recorded cannot change a value — it is a no-op computed the expensive way. The walk now returns early while no override is recorded, tracked by a
_highContrastResourceStateCountcounter: incremented in theGetValuefactory, decremented when a restore removes a state (ConditionalWeakTableexposes no count). So the skip also comes back after high contrast is turned off and every override has been restored. Entries lost to GC only keep the count above zero, which costs a no-op walk and never skips a real restore. The full walk still runs whenever high contrast is active, and whenever an override exists to restore.This path is new in 7.0 —
UpdateSystemColorAndBrushResourcesdoes not exist in6.7.103, where the call site is a// TODO Unocomment. It arrived with1afcec4b94 feat(skia): implement high contrast support.2.
XamlControlsResources— bakeTintLuminosityOpacityinto the Fluent XAML instead of patching it from the constructor.UpdateAcrylicBrushes()didThemeDictionaries.TryGetValue("Default", …)and…("Light", …)purely to assign 20 doubles our ported dictionaries omitted. SinceTryGetValuematerializes lazy values, a Light-themed app built the entire Dark dictionary before its first frame, and vice versa — one of the two can never be the active theme. The 20 values now sit inline on theAcrylicBrushresources the way WinUI sets them, and the constructor code path is gone.This restores WinUI parity — it does not invent an Uno mechanism. MUX sets these values inline in
XAML, not from code-behind:
controls/dev/Materials/Acrylic/AcrylicBrush_themeresources.xamlcarriesTintLuminosityOpacityon all 20 brushes, and the values in this PR were diffed against it — an exactmatch, Dark and Light. The only acrylic code in MUX's
XamlControlsResources.cppis a lookup, whoseown comment explains it: "a workaround to force the Nullable type to be registered as a known
type, which somehow doesn't otherwise get registered for some apps which need it." That is a WinRT
type-registration quirk, not a value assignment, and it looks up a single brush in the
Defaultdictionary only — it never materializes both themes.
Uno's
UpdateAcrylicBrusheswas therefore compensating, in code, for attributes dropped when the Fluentdictionaries were ported — as its own comment said: "Our ported Fluent dictionaries omit the
TintLuminosityOpacity that WinUI sets inline on these AcrylicBrush resources." This PR fills that gap the
way WinUI does. The "low level compiler bug ... nullable doubles" warning lives in
AcrylicBrush_19h1_themeresources.xaml, the legacy system-XAML dictionary; that text appears nowhere inthe MUX sources, and Uno's XAML generator unwraps nullable property types correctly (verified end to end
below). MUX's type-registration workaround is deliberately not ported: Uno has no equivalent WinRT type
activation, and porting it would re-materialize the
Defaultdictionary at constructor time.Measurements
Headless Skia boot harness, medians of interleaved A/B process launches. Allocation is deterministic on this path: byte-identical medians and minimums across sweeps, and stable to 0.02% between a quiet machine and one at 94% CPU.
OnLaunchednew XamlControlsResources()The rise in the window/activate phase is the active theme's materialization moving from constructor time to first use — the same work, later. The net ~922 KB is genuinely eliminated: the inactive theme dictionary and the four HighContrast dictionaries are never built at all.
This is an allocation result, not a startup-time claim. Total wall-clock to first idle moved about −1%, inside the noise floor: on the measuring machine the untouched, byte-identical phases drifted 6–8% between arms, so the timing numbers are not trustworthy at that resolution.
Validation
Given_AcrylicBrush+Given_HighContrastAdjustment+Given_Theme_Materialization): 43/43 passed, 1 skipped (aNativeWasm-only test, correctly excluded on this target).When_TintLuminosityOpacity_Is_Set_By_ThemeResourcespasses, asserting Light = 0.85 and Dark = 0.96 and non-null through normal{ThemeResource}resolution. This is a pre-existing test, not added here.HighContrastWhite/Black/Customvariant selection.Uno.UI.UnitTests): 4166 total, 12 failed — all 12 areWhen_Gregorian_FixedDate*, the known pre-existing failures on a non-UTC machine, unrelated to this change.Given_ResourceDictionary's high-contrast tests (which callUpdateSystemColorAndBrushResourcesdirectly, including withrestoreDefaults: true) pass, including two added here:When_HighContrast_Restore_Without_Override_Theme_Dictionaries_Stay_Lazy(the skip before any override) andWhen_HighContrast_Overrides_Restored_Theme_Dictionaries_Stay_Lazy(the skip after all overrides were restored; fails without the counter).TintLuminosityOpacityvalues were diffed against the deleted C# constants: exact match, 20/20.src/Uno.UI.FluentTheme/themeresources.xamlis a generated build artifact and is gitignored, so no merged file needed regenerating.PR Checklist ✅
Screenshots Compare Test Runresults. — to be checked once CI publishes them.🤖 Generated with Claude Code
https://claude.ai/code/session_012QzKQawubFcPPX9JSoZLkX