Skip to content

perf(textblock): Keep a single line's layout under a new constraint - #25411

Open
MartinZikmund wants to merge 4 commits into
masterfrom
dev/mazi/textblock-measure-reuse
Open

MartinZikmund wants to merge 4 commits into
masterfrom
dev/mazi/textblock-measure-reuse

Conversation

@MartinZikmund

Copy link
Copy Markdown
Member

GitHub Issue: closes #25410

PR Type:

✨ Feature (performance)

What changed? 🚀

A TextBlock measured again only because its parent offers a different constraint now keeps its text layout when the layout can't depend on that constraint: a single unwrapped, untrimmed, unjustified line with no paragraph layout. Measure returns the existing size and arrange positions the line, as it already did. A TextBlock whose own measure was invalidated since its last layout is re-parsed as before.

  • UnicodeText.IsSizeValidFor is the size counterpart of IsLayoutValidFor. Alignment and right-to-left only offset the line, so they don't count as width dependencies.
  • UIElement.OnMeasureInvalidated (private protected) is a new hook. It is called on every InvalidateMeasure, including ones ignored because the element is measuring itself or invalidations are intercepted, and when Enter dirties measure. TextBlock uses it to know whether a new measure is a pure constraint change.

Motivating case: #25389. Its ListViewItemPresenter multi-select check glyph gets a different constraint on every selection toggle and was re-shaped twice per toggle. This also applies to TextBlocks in Grid star columns and to any other multi-pass layout.

Numbers. GridView multi-select, toggling 100 items per op (Win32 Skia, 3 rounds plus a null copy, stacked on #25389 and the other open perf PRs):

Before After Master
Allocations per op 2.10 MB 1.67 MB 1.54 MB
Synchronous allocations 1.95 MB 1.51 MB 1.50 MB
Synchronous time 7.8 ms 7.2 ms 3.9 ms

Validation. Runtime, Skia desktop:

  • New Given_TextBlock tests:
    • Left and centered single lines keep their layout and size under a new constraint (fail before this change).
    • Under a new constraint, a content change, a font change and wrapped text are still laid out again.
  • Suites run: Given_TextBlock, Given_TextBlock_TextScaling, Given_RichTextBlock, Given_TextBox, Given_Button, Given_ComboBox, Given_Grid, Given_StackPanel, Given_ToolTip, Given_ContentPresenter, Given_ScrollViewer, Given_FrameworkElement*. 1,314 of 1,323 pass.
    • 6 fail, all pre-existing on master: When_ComboBox_MinWidth, When_IsTextSelectionEnabled_CRLF, and EVP_When_ConstrainedInNonScrollableSV ×4 (150% display scale).

PR Checklist ✅

🤖 Generated with Claude Code

https://claude.ai/code/session_01Vi68uWCYVXxhcX9m6JEwSC

MartinZikmund and others added 4 commits October 13, 2026 22:11
A TextBlock measured again only because its parent offered another size
re-parsed and re-shaped its text every time. A single unwrapped,
untrimmed line has the same lines and size under any constraint, so
measure now keeps the current layout and arrange positions it, as it
already did. A TextBlock invalidated since its last layout is laid out
as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Vi68uWCYVXxhcX9m6JEwSC
A text change made while the block measures itself, or while
invalidations are intercepted, now also keeps the next measure from
reusing the layout.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Vi68uWCYVXxhcX9m6JEwSC
@github-actions

github-actions Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @MartinZikmund's task in 19s —— View job


Review summary

Verdict: ship, with one nit.

  • Blocking: 0
  • Nits: 1 (inline, field placement in TextBlock.cs)

What I checked (code review only, no build or runtime run):

  • IsSizeValidFor leaves out alignment and RTL, and it still counts wrap, trimming, justify and paragraph layouts as width dependencies. That matches the intent.
  • OnMeasureInvalidated is called before the early returns in InvalidateMeasure, so ignored invalidations still mark the text dirty. It is also called from the Enter-dirty path in UIElement.mux.cs. _isMeasureInvalidated starts as true, so the first measure always parses.
  • CustomTextLayout and non-UnicodeText parsed text fall back to a full re-parse.
  • The new tests cover both the reuse path and the invalidation cases (content, font, wrap).

Cross-cutting: reuse depends on every text-affecting change reaching InvalidateMeasure. Changes that skip it would now leave stale layout, where before the next measure would have re-parsed. Examples are a text-scale or culture change, or a font fallback or resource change that doesn't go through a property. The Given_TextBlock_TextScaling run in the PR description covers part of this. A test for a theme or resource-driven font change would back it up.

// Whether the text was invalidated since it was last laid out, as opposed to only being measured under a new constraint.
private bool _isMeasureInvalidated = true;

private protected override void OnMeasureInvalidated() => _isMeasureInvalidated = true;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: the new flag and override are wedged between _textLayoutVersion and _textLayoutWidth, splitting the layout-state fields. Move _textLayoutWidth above them, or put the override with the other methods, so the fields stay grouped.

@unodevops

Copy link
Copy Markdown
Contributor

@nventive-devops

Copy link
Copy Markdown
Contributor

The build 239328 found UI Test snapshots differences: skia-linux-screenshots: 118, skia-windows-screenshots: 2458

Details
  • skia-linux-screenshots: 118 changed over 2458

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • ButtonClippingTestsControl.png-dark
    • ButtonClippingTestsControl.png
    • CalendarView_Theming.png-dark
    • CalendarView_Theming.png
    • CompositionEffectBrush.png-dark
    • CompositionEffectBrush.png
    • CompositionNineGridBrush_Source_Changes.png-dark
    • CompositionNineGridBrush_Source_Changes.png
    • Buttons.png-dark
    • Buttons.png
    • ClipboardTests.png-dark
    • DynamicFont.png-dark
    • DynamicFont.png
    • Focus_FocusVisual_Properties.png-dark
    • Focus_FocusVisual_Properties.png
    • DropDownButtonPage.png-dark
    • Examples.png
    • ImageIconPage.png-dark
    • ImageIconPage.png
    • DoubleImageBrushInList.png-dark
  • skia-windows-screenshots: 2458 changed over 2488

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • AnimatedBackVisualSourceSamplePage.png-dark
    • AppBarToggleButtonTest.png-dark
    • ApplicationViewSizing.png
    • AppWindowPresenters.png
    • Arrange_Performance01.png
    • Attributed_text_Supserscript.png
    • AutoBorderStretchwithrightmargin.png-dark
    • AutomationProperties_Landmark.png
    • AutoSizedPathCentered.png-dark
    • AutoSizedPathCentered.png
    • AutoSuggestBox_SoftKeboard.png-dark
    • AutoSuggestBox_SoftKeboard.png
    • Basics Pivot Test.png-dark
    • Basics Pivot Test.png
    • BeginTime_MultipleAnimations.png-dark
    • BeginTime_MultipleAnimations.png
    • BitmapIcon_Monochromatic.png-dark
    • BitmapIcon_Monochromatic.png
    • BorderCenteredwithmargins.png-dark
    • BorderCenteredwithmargins.png

@unodevops

Copy link
Copy Markdown
Contributor

⚠️⚠️ The build 239328 has failed on Uno.UI - CI.

@MartinZikmund MartinZikmund added the 7.1 PRs only. For issues, use the Release field. label Oct 14, 2026
MartinZikmund added a commit that referenced this pull request Nov 2, 2026
GridView stays on GridViewItemExpanded until its perf PRs (#25403, #25404, #25406, #25409, #25411, #25413) land. Apps opt into WinUI's chrome with DefaultGridViewItemStyle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT
MartinZikmund added a commit that referenced this pull request Nov 2, 2026
The implicit GridViewItem style is BasedOn DefaultGridViewItemStyle again,
as in WinUI, so default GridView items render through the ListViewItemPresenter
chrome and default to AllowDrop=True. The Fluent GridViewItemExpanded is removed.

Merge after #25403, #25404, #25406, #25409, #25411 and #25413.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT
MartinZikmund added a commit that referenced this pull request Nov 9, 2026
GridView stays on GridViewItemExpanded until its perf PRs (#25403, #25404, #25406, #25409, #25411, #25413) land. Apps opt into WinUI's chrome with DefaultGridViewItemStyle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT

@morning4coffe-dev morning4coffe-dev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

WinUI does the same in ParagraphNode::CanBypassMeasure: NoWrap text with TextTrimming None skips re-measure when only the width changes, and wrapping or trimming forces it. IsSizeValidFor matches that, and leaving Justify out follows WinUI's own note there. A possible follow-up is WinUI's second bypass case, wrapping text whose untrimmed desired width still fits when the available width shrinks, which is the common "container shrank but text still fits" case. One small ordering note inline.

_textLayoutVersion++;
}

_isMeasureInvalidated = false;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Clearing the flag after ParseText loses any InvalidateMeasure raised during the parse, for example a font-loaded callback from fallback resolution, which can now complete synchronously for local fonts. The layout system ignores that invalidation while measuring, so with this reset the next constraint-only measure would reuse the stale parse. Could you capture and clear it before parsing instead (var invalidated = _isMeasureInvalidated; _isMeasureInvalidated = false; ...)?

@mergify

mergify Bot commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue). If it does not match the queue conditions yet, it enters the queue once it does.

  • Queue this pull request

MartinZikmund added a commit that referenced this pull request Nov 13, 2026
GridView stays on GridViewItemExpanded until its perf PRs (#25403, #25404, #25406, #25409, #25411, #25413) land. Apps opt into WinUI's chrome with DefaultGridViewItemStyle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT
MartinZikmund added a commit that referenced this pull request Nov 13, 2026
GridView stays on GridViewItemExpanded until its perf PRs (#25403, #25404, #25406, #25409, #25411, #25413) land. Apps opt into WinUI's chrome with DefaultGridViewItemStyle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT
MartinZikmund added a commit that referenced this pull request Nov 13, 2026
GridView stays on GridViewItemExpanded until its perf PRs (#25403, #25404, #25406, #25409, #25411, #25413) land. Apps opt into WinUI's chrome with DefaultGridViewItemStyle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT
MartinZikmund added a commit that referenced this pull request Nov 13, 2026
GridView stays on GridViewItemExpanded until its perf PRs (#25403, #25404, #25406, #25409, #25411, #25413) land. Apps opt into WinUI's chrome with DefaultGridViewItemStyle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT
MartinZikmund added a commit that referenced this pull request Nov 14, 2026
GridView stays on GridViewItemExpanded until its perf PRs (#25403, #25404, #25406, #25409, #25411, #25413) land. Apps opt into WinUI's chrome with DefaultGridViewItemStyle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT
MartinZikmund added a commit that referenced this pull request Nov 17, 2026
GridView stays on GridViewItemExpanded until its perf PRs (#25403, #25404, #25406, #25409, #25411, #25413) land. Apps opt into WinUI's chrome with DefaultGridViewItemStyle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT
MartinZikmund added a commit that referenced this pull request Nov 17, 2026
GridView stays on GridViewItemExpanded until its perf PRs (#25403, #25404, #25406, #25409, #25411, #25413) land. Apps opt into WinUI's chrome with DefaultGridViewItemStyle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT
MartinZikmund added a commit that referenced this pull request Nov 17, 2026
GridView stays on GridViewItemExpanded until its perf PRs (#25403, #25404, #25406, #25409, #25411, #25413) land. Apps opt into WinUI's chrome with DefaultGridViewItemStyle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT
MartinZikmund added a commit that referenced this pull request Nov 17, 2026
GridView stays on GridViewItemExpanded until its perf PRs (#25403, #25404, #25406, #25409, #25411, #25413) land. Apps opt into WinUI's chrome with DefaultGridViewItemStyle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT
MartinZikmund added a commit that referenced this pull request Nov 18, 2026
GridView stays on GridViewItemExpanded until its perf PRs (#25403, #25404, #25406, #25409, #25411, #25413) land. Apps opt into WinUI's chrome with DefaultGridViewItemStyle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT
MartinZikmund added a commit that referenced this pull request Nov 18, 2026
GridView stays on GridViewItemExpanded until its perf PRs (#25403, #25404, #25406, #25409, #25411, #25413) land. Apps opt into WinUI's chrome with DefaultGridViewItemStyle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT
MartinZikmund added a commit that referenced this pull request Nov 18, 2026
GridView stays on GridViewItemExpanded until its perf PRs (#25403, #25404, #25406, #25409, #25411, #25413) land. Apps opt into WinUI's chrome with DefaultGridViewItemStyle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TXMNu2NWgGKJFTAYtNHkvT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

7.1 PRs only. For issues, use the Release field.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TextBlock re-shapes its text when only the measure constraint changes

5 participants