Skip to content

[release/11.0] [blazor] Avoid quadratic MarkupString insertion - #70904

Open
github-actions[bot] wants to merge 1 commit into
release/11.0from
backport/pr-70533-to-release/11.0
Open

github-actions[bot] wants to merge 1 commit into
release/11.0from
backport/pr-70533-to-release/11.0

Conversation

@github-actions

@github-actions github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Agent assessment

Self-reported assessment, not an independent review.

Assessed revision: f6270f0cd7640a8c8b3de3a0519f159388fd0c15

Dimension Assessment Rationale
Change confidence 8.5/10 ↑ Backport diff matches merged #70533 line for line.
↑ On main: 54 JS tests and 9 focused Selenium tests passed after merging latest main.
↓ Not built or tested on release/11.0; CI for this PR not yet inspected.
↓ Ownership of script-moved markup nodes changes and is not covered by permanent tests.
Diagnosis confidence 9.5/10 ↑ Browser test counted 499,500 parentNode reads for 1,000 top-level nodes before the fix and 1,000 after.
↑ release/11.0 contains the #66007 orphan sweep that causes the scan.
Evidence fit 9/10 ↑ Reported symptom matches: flat markup slow, same HTML in one wrapper fast.
↓ Benchmarked in an in-tree sample, not the reporter's 1.3 MB Markdig document or a .NET 10 comparison.

Backport of #70533 to release/11.0

/cc @maraf

[blazor] Avoid quadratic MarkupString insertion

Make rendering a MarkupString with many top-level nodes linear again.

Description

#66007 added a sweep to insertLogicalChild that scans every logical sibling of the parent for orphaned nodes on each insertion. insertMarkup inserts each top-level node of a MarkupString into the same new container, so a fragment with n top-level nodes did about n²/2 sibling checks.

Because the markup container is newly created, its nodes can be appended without scanning earlier siblings: every insertion was already an append, so DOM output and script execution order are unchanged. After all nodes are inserted, a single linear pass drops nodes that scripts or custom elements disconnected during insertion. All other logical insertions keep the #66007 orphan handling, including creation of the markup container itself.

Behavior notes, affecting only scripts or custom elements that externally mutate Blazor-rendered markup:

  • A node that detaches itself during the final insertion (for example, a trailing self-removing <script>) is now dropped from tracking, so the next update of that markup no longer throws Cannot read properties of null (reading 'removeChild').
  • A top-level node stays tracked exactly when it still has a DOM parent when insertion finishes, so ownership of nodes that scripts move elsewhere during insertion can differ from 11.0.0.

Fixes #70503

Customer Impact

Apps that render large HTML through MarkupString (for example, Markdown converted to HTML) can freeze the page. The issue reports an 18,000-paragraph document confirming in 8,700 ms on 11.0.0 versus 47 ms on .NET 10 (18.75 s under CPU load). Blazor Server, WebAssembly, and Blazor Hybrid (BlazorWebView in MAUI, WPF, and WinForms) share the affected script. The only workaround is to wrap the markup in a single root element.

Regression?

  • Yes
  • No

Regressed from .NET 10 by #66007, which shipped in 11.0.0.

Risk

  • High
  • Medium
  • Low

The change is limited to insertMarkup and a new linear helper in LogicalElements.ts; insertLogicalChild scans by default, so other callers and the enhanced-navigation fix from #66007 are unchanged. Visible output and script order are unchanged. The only behavior differences involve scripts externally mutating Blazor-managed DOM, which Blazor documents as unsupported.

Verification

  • Manual (required)
  • Automated

Verified on main before merge; this backport is identical and was not separately built or tested on release/11.0.

  • Manual: an in-tree interactive Server sample in WebKit 27.2 rendered 18,000 flat top-level nodes in a median 15 ms versus 242 ms before the fix (five runs each). Wrapped markup and many small fragments were unchanged.
  • Automated: new Selenium tests run in Server and WebAssembly:
    • Render 1,000 flat and 1,000 wrapped top-level nodes, counting sibling reads. The flat test failed before the fix with 499,500 parentNode reads.
    • Clear markup after scripts remove and move nodes. This fails without the fix with the removeChild error above.
  • After merging latest main: 54 focused JavaScript tests and 9 focused Selenium tests passed, including the Fix Blazor crash when interactive component is in layout with enhanced nav #66007 enhanced-navigation regression test.

Packaging changes reviewed?

  • Yes
  • No
  • N/A

When servicing release/2.3

  • Make necessary changes in eng/PatchConfig.props

Note

This PR description was generated with the assistance of GitHub Copilot.

* [blazor] Avoid quadratic MarkupString insertion

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

* [blazor] Replace mutation observer with linear cleanup

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@github-actions
github-actions Bot requested a review from a team as a code owner November 30, 2026 09:30
@maraf maraf added Servicing-consider Shiproom approval is required for the issue area-blazor Includes: Blazor, Razor Components labels Nov 30, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Hi @github-actions[bot]. Please make sure you've updated the PR description to use the Shiproom Template. Also, make sure this PR is not marked as a draft and is ready-to-merge.

To learn more about how to prepare a servicing PR click here.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-blazor Includes: Blazor, Razor Components Servicing-consider Shiproom approval is required for the issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant