Repository navigation
perf(wasm): preload only common font variants - #24080
MartinZikmund wants to merge 3 commits into
Conversation
|
Claude finished @MartinZikmund's task in 4m 26s —— View job PR Review: perf(wasm): preload only common font variants
SummaryThe design is clean and the approach is sound: moving from "preload everything" to a filtered, configurable set of variants is a well-reasoned startup-perf win, and the selection logic is pure and side-effect-free. The public API surface ( 4 findings — 1 medium, 3 low:
No async/cancellation issues, no cross-platform leaks, no hot-path allocations beyond the startup path, and the |
There was a problem hiding this comment.
Pull request overview
This PR reduces startup font preloading work (especially on WebAssembly) by filtering which font-manifest variants are preloaded, controlled via a new FeatureConfiguration.Font.PreloadedVariants flags setting.
Changes:
- Add
FontPreloadVariants[Flags]enum and a newFeatureConfiguration.Font.PreloadedVariantsknob (defaulting to Normal/SemiBold/Bold). - Introduce variant-selection logic in
FontFamilyHelperand routeApplication.PreloadFontsthrough it. - Add unit tests validating the variant-selection behavior.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/Uno.UI/UI/Xaml/FontFamilyHelper.cs | Adds variant-filtered manifest preloading and selection helpers. |
| src/Uno.UI/UI/Xaml/Application.cs | Uses FeatureConfiguration.Font.PreloadedVariants for manifest preloading. |
| src/Uno.UI/FontPreloadVariants.cs | New flags enum defining preloadable weight/style/stretch variants. |
| src/Uno.UI/FeatureConfiguration.cs | Adds Font.PreloadedVariants configuration property and documentation. |
| src/Uno.UI.UnitTests/Windows_UI_Xaml_Media/Given_FontPreloadVariants.cs | New unit tests covering selection rules and edge cases. |
Suppressed comments (2)
src/Uno.UI/UI/Xaml/FontFamilyHelper.cs:96
- Avoid
ContinueWithinside anasyncmethod here; it capturesTaskScheduler.Currentand can reintroduce context/exception-handling quirks. Since this is alreadyasync, awaitingTask.WhenAlland aggregating the results is simpler and more predictable.
var tasks = fonts
.Select(fontInfo => PreloadAsync(fontInfo.FamilyName, new FontWeight(fontInfo.FontWeight), fontInfo.FontStretch, fontInfo.FontStyle));
return await Task.WhenAll(tasks).ContinueWith(combinedTask => combinedTask.Result.All(t => t));
src/Uno.UI/UI/Xaml/FontFamilyHelper.cs:109
FontPreloadVariants.Condensedcurrently includes any non-Normalstretch (includingExpanded/UltraExpanded), but the docs say it should widen selection to condensed widths only. This will preload unintended variants when a manifest contains expanded stretches.
if (font.FontStretch != FontStretch.Normal && !variants.HasFlag(FontPreloadVariants.Condensed))
{
return false;
}
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
|
|
||
| namespace Uno.UI.Tests.Windows_UI_Xaml_Media; | ||
|
|
||
| [TestClass] |
There was a problem hiding this comment.
Per .claude/rules/unit-tests.md, tests that guard or reproduce a specific GitHub issue should carry [GitHubWorkItem] for traceability. Since this PR closes #24079, consider adding it at class level:
| [TestClass] | |
| [TestClass] | |
| [GitHubWorkItem("https://github.com/unoplatform/uno/issues/24079")] | |
| public class Given_FontPreloadVariants |
There was a problem hiding this comment.
Not applying this one — GitHubWorkItemAttribute isn't available in Uno.UI.UnitTests, so the suggestion wouldn't compile.
Evidence:
git grep GitHubWorkItemAttributefinds no declaration anywhere in the repo; the type comes from outsidesrc/.- All 70 files using
[GitHubWorkItem]are inUno.UI.RuntimeTests; zero are inUno.UI.UnitTests. Uno.UI.UnitTests.csprojreferences neitherUno.UI.RuntimeTestsnor the MUX test infrastructure that supplies it, andUnitTestsImport.propsdoesn't pull this file into the RuntimeTests compilation either.
I couldn't compile-check it locally to be certain — Uno.UI.UnitTests targets $(NetUnitTests) = net11.0 on feature/breakingchanges and no .NET 11 SDK is installed here (NETSDK1045) — so I left it out rather than risk turning CI red on a traceability annotation. Happy to add it if the attribute is made visible to the unit-test project.
The issue link is on the PR body (closes #24079), so traceability isn't lost.
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-24080/wasm-skia-net9/index.html |
|
The build 227682 found UI Test snapshots differences: Details
|
458735c to
35fc40b
Compare
|
Claude finished @MartinZikmund's task in 3m 19s —— View job PR Review: perf(wasm): preload only common font variants
SummaryThis PR synchronization was triggered by a base-branch update — the PR branch itself has a single unchanged commit ( Overall design assessment: Clean and well-structured. The approach of a configurable Open findings (4 total — 1 medium, 3 low):
Additional minor observation (not previously flagged): |
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-24080/wasm-skia-net9/index.html |
|
The build 227793 found UI Test snapshots differences: Details
|
|
Claude finished @MartinZikmund's task in 2m 35s —— View job PR Review: perf(wasm): preload only common font variants
SummaryGood progress since the last review run — both medium/correctness issues from the two previous passes have been resolved in
2 low findings remain (inline comments posted):
No issues with async/cancellation discipline beyond finding #1, no cross-platform leaks, no hot-path allocations, and the |
| var tasks = SelectVariants(manifest.Fonts, variants) | ||
| .Select(fontInfo => PreloadAsync(fontInfo.FamilyName, new FontWeight(fontInfo.FontWeight), fontInfo.FontStretch, fontInfo.FontStyle)); | ||
|
|
||
| return await Task.WhenAll(tasks).ContinueWith(combinedTask => combinedTask.Result.All(t => t)); |
There was a problem hiding this comment.
Low — async idiom: ContinueWith inside an async method is the wrong pattern here. If Task.WhenAll faults (any preload task throws), the continuation lambda accesses .Result and rethrows as AggregateException, which is then wrapped again by the outer await. The await-then-aggregate idiom handles exceptions cleanly and uses no continuation scheduler ambiguity:
| return await Task.WhenAll(tasks).ContinueWith(combinedTask => combinedTask.Result.All(t => t)); | |
| var results = await Task.WhenAll(tasks); | |
| return results.All(t => t); |
| namespace Uno.UI.Tests.Windows_UI_Xaml_Media; | ||
|
|
||
| [TestClass] | ||
| public class Given_FontPreloadVariants |
There was a problem hiding this comment.
Low — missing [GitHubWorkItem]: Per .claude/rules/unit-tests.md, tests that guard a specific GitHub issue must carry this attribute for traceability (flagged in both previous review passes — still outstanding).
| public class Given_FontPreloadVariants | |
| [TestClass] | |
| [GitHubWorkItem("https://github.com/unoplatform/uno/issues/24079")] | |
| public class Given_FontPreloadVariants |
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-24080/wasm-skia-net9/index.html |
|
The build 227959 found UI Test snapshots differences: Details
|
|
|
| /// The weight flags, i.e. the ones that can select a face on their own. <see cref="FontPreloadVariants.Italic"/> | ||
| /// and <see cref="FontPreloadVariants.Condensed"/> only widen an existing weight selection. | ||
| /// </summary> | ||
| private const FontPreloadVariants AnyWeight = |
There was a problem hiding this comment.
this should be an combined flag value inside that enum
it will be easier to keep that updated, should things change
1e2de4e to
b02cfd1
Compare
|
Claude finished @MartinZikmund's task in 1m 52s —— View job PR Review: perf(wasm): preload only common font variants
SummaryAll prior medium/correctness findings remain resolved. The design is clean: the 2 findings — both Low:
One previously-noted observation (not re-posted as blocking):
|
| var tasks = SelectVariants(manifest.Fonts, variants) | ||
| .Select(fontInfo => PreloadAsync(fontInfo.FamilyName, new FontWeight(fontInfo.FontWeight), fontInfo.FontStretch, fontInfo.FontStyle)); | ||
|
|
||
| return await Task.WhenAll(tasks).ContinueWith(combinedTask => combinedTask.Result.All(t => t)); |
There was a problem hiding this comment.
Low — async idiom (third pass, still open): ContinueWith inside an async method is the wrong pattern here. When any preload task faults, combinedTask.Result inside the lambda re-throws as AggregateException, which is then wrapped again by the outer await instead of being unwrapped to the inner exception. ContinueWith also captures TaskScheduler.Current, which can route the continuation back to the UI thread in the wrong context.
The equivalent await-first form is simpler and has correct exception semantics:
| return await Task.WhenAll(tasks).ContinueWith(combinedTask => combinedTask.Result.All(t => t)); | |
| var results = await Task.WhenAll(tasks); | |
| return results.All(t => t); |
| private const FontPreloadVariants AnyWeight = | ||
| FontPreloadVariants.Thin | FontPreloadVariants.ExtraLight | FontPreloadVariants.Light | | ||
| FontPreloadVariants.Normal | FontPreloadVariants.Medium | FontPreloadVariants.SemiBold | | ||
| FontPreloadVariants.Bold | FontPreloadVariants.ExtraBold | FontPreloadVariants.Black; |
There was a problem hiding this comment.
Low — maintainability (endorsing Xiaoy312's review): AnyWeight is a private constant that mirrors every weight member of FontPreloadVariants. If a new weight flag (e.g. ExtraThin) is ever added to the enum, this private constant must also be updated or SelectsAnyFace silently excludes the new flag. The natural home for this mask is the enum itself:
// in FontPreloadVariants.cs
/// <summary>All weight flags; does not include <see cref="Italic"/> or <see cref="Condensed"/>.</summary>
AnyWeight = Thin | ExtraLight | Light | Normal | Medium | SemiBold | Bold | ExtraBold | Black,FontFamilyHelper.AnyWeight can then be removed and SelectsAnyFace updated to (variants & FontPreloadVariants.AnyWeight) != 0.
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-24080/wasm-skia-net9/index.html |
|
The build 228880 found UI Test snapshots differences: Details
|
b02cfd1 to
a35c62a
Compare
|
Claude finished @MartinZikmund's task in 2m 29s —— View job PR Review: perf(wasm): preload only common font variants
SummaryAll correctness findings from the previous five review passes remain resolved. The selection logic, 2 low findings remain open (both carried from prior passes):
Previously closed:
The snapshot diffs (73–178 changed screenshots across builds) are consistent across all runs on this branch and appear to originate from the |
| var tasks = SelectVariants(manifest.Fonts, variants) | ||
| .Select(fontInfo => PreloadAsync(fontInfo.FamilyName, new FontWeight(fontInfo.FontWeight), fontInfo.FontStretch, fontInfo.FontStyle)); | ||
|
|
||
| return await Task.WhenAll(tasks).ContinueWith(combinedTask => combinedTask.Result.All(t => t)); |
There was a problem hiding this comment.
Low — async idiom (fifth pass, still open): ContinueWith inside an async method is the wrong pattern. When any preload task faults, Task.WhenAll returns a faulted task; the continuation lambda accesses .Result, re-throwing as AggregateException rather than letting await unwrap to the original exception. ContinueWith also captures TaskScheduler.Current, which can route the continuation back to the UI thread.
| return await Task.WhenAll(tasks).ContinueWith(combinedTask => combinedTask.Result.All(t => t)); | |
| var results = await Task.WhenAll(tasks); | |
| return results.All(t => t); |
| /// Whether <paramref name="variants"/> can match a face at all. Covers <see cref="FontPreloadVariants.None"/> | ||
| /// as well as a width/style-only selection, which would otherwise match nothing and trip the fallback below. | ||
| /// </summary> | ||
| internal static bool SelectsAnyFace(FontPreloadVariants variants) => (variants & AnyWeight) != 0; |
There was a problem hiding this comment.
Low — maintainability (third pass, endorsing Xiaoy312): AnyWeight is a private constant that manually enumerates every weight member of FontPreloadVariants. A new weight flag added to the enum (e.g. ExtraThin) that isn't also added here silently breaks SelectsAnyFace — the face would never match and would either be excluded or trigger the whole-manifest fallback.
The natural home for this mask is the enum itself, so both definitions stay in sync:
// in FontPreloadVariants.cs, after Black:
/// <summary>All weight flags; does not include <see cref="Italic"/> or <see cref="Condensed"/>.</summary>
AnyWeight = Thin | ExtraLight | Light | Normal | Medium | SemiBold | Bold | ExtraBold | Black,Then the private constant here can be removed, and this line updated to:
| internal static bool SelectsAnyFace(FontPreloadVariants variants) => (variants & AnyWeight) != 0; | |
| internal static bool SelectsAnyFace(FontPreloadVariants variants) => (variants & FontPreloadVariants.AnyWeight) != 0; |
|
The build 229432 found UI Test snapshots differences: Details
|
|
|
|
|
|
|
e5454bf to
945831f
Compare
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-24080/wasm-skia-net9/index.html |
|
Claude encountered an error after 0s —— View job I'll analyze this and get back to you. |
|
The build 230998 found UI Test snapshots differences: Details
|
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-24080/wasm-skia-net9/index.html |
|
The build 231686 found UI Test snapshots differences: Details
|
morning4coffe-dev
left a comment
There was a problem hiding this comment.
The All compatibility path, None/no-weight no-fetch path and no-matching-face fallback are consistent. One remaining contract detail: FontFamilyHelper.IsVariantSelected excludes only FontStyle.Italic, so an Oblique normal-weight face passes the default mask despite the documented upright-only selection. Please explicitly decide how Oblique maps to the style flag and cover it, rather than silently preloading it as upright. This is Uno/browser startup policy, not a direct port of WinUI font-loading behavior.
Application.PreloadFonts awaited every entry in the default font's manifest before the first frame. A full family declares dozens of weight/width/style combinations, and on WebAssembly each one is an individual HTTP request on the startup path: a blank Skia WASM app fetched 37 font files (2.6 MB brotli) before the splash screen came down. Preloading is now driven by FeatureConfiguration.Font.PreloadedVariants, a [Flags] enum defaulting to the upright Normal, SemiBold and Bold faces. The remaining variants load on first use. FontPreloadVariants.All restores the previous behaviour, None disables preloading entirely, and Italic/Condensed extend the selection to those forms of the selected weights. PreloadAllFontsInManifest keeps its existing semantics and now delegates to the new overload with FontPreloadVariants.All. If a manifest declares none of the selected variants the full set is preloaded, so a family with no upright regular face is not left with nothing preloaded. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01KPSfNUJzuqnH64X7YuX7VK
A selection with no weight flag (e.g. Italic alone) matched no face, which tripped the "manifest declares none of the selected variants" fallback and preloaded the whole family — the opposite of what was asked for. Such a selection now preloads nothing and skips the manifest fetch entirely. A manifest omitting font_stretch deserializes to FontStretch.Undefined, which the width filter treated as non-normal and excluded. Undefined now counts as normal width, so a manifest without explicit widths is filtered by weight instead of falling through to a full preload. The selection is extracted into FontFamilyHelper.SelectVariants so the fallback and the empty-selection guard are covered by unit tests rather than only by the I/O-bound PreloadFontsInManifest. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01GP2BeVCQzY3sQB7QBv173u
PreloadFonts runs from InvokeOnLaunched before OnLaunched, so assigning PreloadedVariants inside OnLaunched has no effect. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01GP2BeVCQzY3sQB7QBv173u
945831f to
1ffa603
Compare
|
Claude finished @MartinZikmund's task in 1m 52s —— View job PR Review: perf(wasm): preload only common font variants
SummaryRe-verified the current state of all five changed files against the six prior review passes. The design remains sound: a pure, side-effect-free All correctness-level findings from passes 1–6 are confirmed fixed and stay fixed: 1 new finding — Low: 2 findings remain open from passes 1–6 (not re-posted inline — already on the thread, unchanged since 2026-08-22, no action taken):
Verdict: no blocking issues. The two long-open lows are minor and one predates this PR; the new Oblique finding is also low-severity and narrow in practice. Safe to merge as-is, or address the three lows together in a quick follow-up commit. |
|
|
||
| internal static bool IsVariantSelected(FontInfo font, FontPreloadVariants variants) | ||
| { | ||
| if (font.FontStyle == FontStyle.Italic && !variants.HasFlag(FontPreloadVariants.Italic)) |
There was a problem hiding this comment.
Low — FontStyle.Oblique silently treated as upright: FontStyle has three values (Normal, Oblique, Italic — src/Uno.WinRT/UI/Text/FontStyle.cs), but this check only excludes FontStyle.Italic when Italic isn't requested. A manifest face with FontStyle.Oblique falls through this guard and is preloaded by the default weight mask alone, even though the enum's remarks say weight flags should select only "upright, normal-width faces" (FontPreloadVariants.cs:10).
In practice no shipped Uno Fonts/Open Sans manifest declares oblique faces, so this doesn't change today's preload count, but it's an unhandled case in otherwise-exhaustive logic (raised by @morning4coffe-dev on 2026-09-08, not yet addressed). Suggest either matching FontStyle.Normal explicitly instead of != Italic, or documenting that Italic also gates Oblique:
| if (font.FontStyle == FontStyle.Italic && !variants.HasFlag(FontPreloadVariants.Italic)) | |
| if (font.FontStyle != FontStyle.Normal && !variants.HasFlag(FontPreloadVariants.Italic)) |
| using Microsoft.UI.Xaml; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using Uno.UI; | ||
| using Uno.UI.Xaml.Media; |
|
|
||
| internal static bool IsVariantSelected(FontInfo font, FontPreloadVariants variants) | ||
| { | ||
| if (font.FontStyle == FontStyle.Italic && !variants.HasFlag(FontPreloadVariants.Italic)) |
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-24080/wasm-skia-net9/index.html |
|
The build 237149 found UI Test snapshots differences: Details
|
|
Tick the box to add this pull request to the merge queue (same as
|


GitHub Issue: closes #24079
PR Type:
✨ Feature
What changed? 🚀
Current behavior.
Application.PreloadFontsawaits every entry in the default font's manifest before the first frame.FontFamilyHelper.PreloadAllFontsInManifestapplies no weight/width/style filter, so a family such asUno.Fonts.OpenSans(32 faces) is fetched in full. On WebAssembly each face is an individual HTTP request on the startup path, and becauseWebAssemblyWindowWrapper.ShowCorechainsRemoveSplashScreen()offFontPreloadTask, those requests gate perceived startup.Measured on a blank Skia WASM app (Release publish, brotli, cold context): 37 font requests, 2.62 MB brotli. First GL draw at ~870 ms, last font at 1 026–1 215 ms, splash cleared at 1 141–1 306 ms.
This PR. Preloading is now driven by
FeatureConfiguration.Font.PreloadedVariants, a new[Flags] FontPreloadVariantsenum:Weight flags select upright, normal-width faces;
ItalicandCondensedwiden the selection to those forms of the selected weights. Non-preloaded faces load on first use exactly as before.PreloadAllFontsInManifestkeeps its existing semantics and delegates to the new overload withFontPreloadVariants.All.If a manifest declares none of the selected variants, the full set is preloaded, so a family without an upright regular face is not left with nothing preloaded.
This addresses the preload half of the cost. The 32 faces are still deployed; trimming what
Uno.Fonts.OpenSansships by default is separate follow-up work.Context and full measurements:
specs/056-wasm-skia-startup-performance/spec.md(R3), on branchdev/mazi/wasm-startup-perf.Validation
All, preserving semantics.dotnet build src/Uno.UI/Uno.UI.csproj -p:UnoTargetFrameworkOverride=net10.0— succeeded.dotnet test --project src/Uno.UI.UnitTests/Uno.UI.UnitTests.csproj --filter "FullyQualifiedName~Windows_UI_Xaml_Media"— 63/63 passed, comprising 8 newGiven_FontPreloadVariantstests plus the 28 pre-existingGiven_FontManifesttests and the rest of the namespace, confirming no regression.Not yet measured end-to-end in a published WASM app — the expected effect is that the 37 startup font requests drop to the small number of faces actually selected.
PR Checklist ✅
Screenshots Compare Test Runresults.