Repository navigation
Description
What would you like to be added? π‘
TextBlock.MeasureOverride re-shapes its text unconditionally on every measure β full ICU boundary analysis, HarfBuzz shaping and line breaking β even when nothing about the text or the width has changed. In an allocation profile of a dirty layout pass it is the single largest allocator in the framework.
WinUI does not do this. It keeps a content dirty flag on the text layout node that is separate from the element's own measure-dirty flag (m_pPageNode->IsMeasureDirty(), set via CTextBlock::InvalidateContentMeasure()), and BlockNode::CanBypassMeasure reuses the existing layout when that flag is clear and the constraint still permits it. A TextBlock re-measured for an unrelated reason therefore keeps its layout.
The proposal is to give Uno Platform's TextBlock the same separation: set a content-dirty flag in TextBlock.InvalidateTextBlock() (the funnel ~17 property-change handlers already call), clear it in ParseText, and in MeasureOverride skip the re-parse when the flag is clear and the constraint check passes.
The constraint check already exists in the codebase: TextBlock.NeedsReparseForArrange, added for the arrange-side half of this optimisation, implements WinUI's rule β the width and adjusted alignment must match, and the height must match only when the previous layout was actually truncated by it, otherwise it is enough that the text still fits. MeasureOverride should use that same predicate, not a width-only check, because availableSize.Height genuinely drives isEarlyLastLine.
Why is this needed? π€
The arrange-side half of this has already been done, and on a 3,109-element tree (nested Grid/StackPanel/Border with TextBlock leaves) a full dirty layout pass dropped from 32.4 ms to 19.0 ms and from 8.19 MB to 4.14 MB of allocations, purely by not shaping the same text twice per pass. The measure side is the remaining half, and text remains the top allocator afterwards.
β οΈ Prerequisites β this is not safe to implement as-is
Every input of the UnicodeText constructor was audited against the invalidation funnel. Five sources bypass TextBlock.InvalidateTextBlock(), and each would become a silent rendering bug once a cache exists. They should be closed first, each with a test:
| # | Source | What breaks if cached |
|---|---|---|
| 1 | TextBlock.cs β UnicodeText.IFontCacheUpdateListener.Invalidate() is => InvalidateMeasure() |
Permanent wrong font. The async fallback-font path removes its listener before firing, and only re-registers inside the UnicodeText constructor β so a skipped re-parse never recovers. Normal path for emoji, CJK, Arabic, Devanagari and symbol glyphs. |
| 2 | FrameworkElement.FlowDirectionProperty is AffectsMeasure | Inherits with no changed callback |
FlowDirection is a direct UnicodeText constructor argument driving BiDi levels, per-line visual order and the trimming-ellipsis side. Flipping it on the element or any ancestor would leave stale LTR/RTL layout. |
| 3 | CoreServices text-scale walk calls bare element.InvalidateMeasure() on TextBlock/RichTextBlock |
OS text scaling stops taking effect β accessibility-critical. |
| 4 | TextBlock.IsSpellCheckEnabled is a plain property with no invalidation at all |
Spell-check underlines never refresh. This is already a live bug today and is filed separately. |
| 5 | TextBlock.IsTextScaleFactorEnabledProperty has no changed callback |
Only matters for an empty TextBlock; with β₯1 inline, inheritance rescues it via Inline.OnIsTextScaleFactorEnabledChanged. |
Two further inputs invalidate nothing today either, so they are pre-existing rather than regressions β but a cache makes them harder to recover from, because today any layout pass repairs them: CultureInfo.CurrentUICulture (it opens the ICU line- and word-break iterators, so it changes wrapping for Thai / Japanese / Khmer), and the FeatureConfiguration.Font statics.
Test coverage is thin. Only gaps 1 and 3 have existing tests that would go red (Given_TextBlock.Check_FontFallback_Shaping2 and Given_TextBlock_TextScaling.When_TextScaleFactorChanges_Nested_Inlines_Are_Invalidated). Gaps 2, 4 and 5 have none β every current FlowDirection test assigns the value before load. Tests worth writing first: mutate TextBlock.FlowDirection after load; the same via an ancestor (inherited path); toggle TextBox.IsSpellCheckEnabled after load; measure at a generous height then a small one and assert truncation kicks in; toggle IsTextScaleFactorEnabled on an empty TextBlock after load.
One existing test will need re-checking: Given_TextBlock.When_FontFamily_Changed calls a bare SUT.InvalidateMeasure() in its retry loop, which becomes a no-op for re-parsing once the flag exists.
Renderer π¨
- Skia
For which platforms π±π»π₯οΈ
All platforms π (the Skia text stack is shared)
Anything else we need to know? π¬
Worth calibrating the expected win before building it: UIElement.DoMeasure already short-circuits when the element is not measure-dirty and the available size is unchanged, so MeasureOverride never runs in that case. The gain comes specifically from text blocks made measure-dirty for a non-content reason, plus available-size changes that do not affect the text layout.
On the cached path the implementation must still take the desired size from the recorded parse output (which already carries the caret-thickness adjustment for TextBox-owned blocks) and still run the layout-rounding block.
Activity