Skip to content

[blazor] Avoid quadratic MarkupString insertion - #70533

Draft
maraf wants to merge 2 commits into
dotnet:mainfrom
maraf:maraf-markupstring-insertion-regression
Draft

maraf wants to merge 2 commits into
dotnet:mainfrom
maraf:maraf-markupstring-insertion-regression

Conversation

@maraf

@maraf maraf commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

[blazor] Avoid quadratic MarkupString insertion

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing one.

Avoid quadratic work when rendering large markup fragments.

Description

Each MarkupString is rendered into a newly created logical container. Its top-level nodes can therefore be appended in order without scanning the container's existing logical siblings before each insertion. After insertion (including synchronous script execution), compact the logical-children array once, dropping nodes that are disconnected from the DOM. This changes markup insertion from repeated sibling scans to a single linear cleanup pass, without changing orphan cleanup for other logical insertions.

For a fresh container, the previous per-insertion scan never changed where nodes were placed: every insertion was already an append. DOM output, script execution order, and each node's position when its script runs are therefore unchanged. Parent indexes are unaffected because the markup container is a single logical child of its parent, and #66007's orphan handling still applies to every other insertion, including creation of the markup container.

Behavior notes

A top-level markup node now remains tracked exactly when it still has a DOM parent at the moment markup insertion finishes. Previously, this depended on whether a later insertion happened to scan after the node was detached. This only affects scripts or custom elements that externally mutate Blazor-rendered markup:

  • A node detached during the final insertion, such as a trailing self-removing <script>, is now dropped from tracking, so the next markup update no longer throws Cannot read properties of null (reading 'removeChild').
  • A node detached and then reattached elsewhere before insertion finishes stays tracked and is removed from its new location when the markup is cleared. This matches nodes moved directly elsewhere, which were already tracked.
  • A node detached during the final insertion and reattached asynchronously is no longer tracked, so it remains in its new location when the markup is cleared.

Nodes detached asynchronously after insertion completes are handled as before.

Benchmark

Five-run medians from the same in-tree interactive Server sample using Playwright WebKit 27.2 on macOS ARM64. Timings are click-to-DOM-update, including local Server processing and transport; they are not a comparison with .NET 10. Values are original implementation / MutationObserver implementation previously on this PR / current fresh-container implementation.

Markup Original Observer Fresh-container
1,000 flat nodes 3 ms 2 ms 2 ms
2,000 flat nodes 5 ms 3 ms 3 ms
4,000 flat nodes 14 ms 5 ms 4 ms
8,000 flat nodes 48 ms 10 ms 7 ms
16,000 flat nodes 191 ms 17 ms 12 ms
18,000 flat nodes 242 ms 20 ms 15 ms
18,000 wrapped nodes 9 ms 9 ms 9 ms
2,000 two-node fragments 10 ms 10 ms 10 ms

Validation

  • npm test --workspace @microsoft/microsoft.aspnetcore.components.web.js -- --runInBand test/LogicalElements.test.ts test/DomSync.test.ts test/BrowserRenderer.updateText.test.ts — 54 passed.

  • Targeted ESLint on BrowserRenderer.ts and LogicalElements.ts — 0 errors (existing warnings only).

  • The E2E project built successfully. Focused Selenium run with Chromium on macOS ARM64 — 9 passed, including flat and wrapped markup, script removal/reparenting and clear, markup blocks, and enhanced navigation. The final self-removing-script test fails against both the previous MutationObserver implementation and the original implementation, and passes with this cleanup approach.

    source activate.sh && TEST_CHROME_BINARY="$HOME/Library/Caches/ms-playwright/chromium-1252/chrome-mac-arm64/Google Chrome for Testing.app/Contents/MacOS/Google Chrome for Testing" MICROSOFT_ASPNETCORE_TESTING_SELENIUM_SUPPORTED=true dotnet test src/Components/test/E2ETest/Microsoft.AspNetCore.Components.E2ETests.csproj --no-build --no-restore -p:SeleniumE2ETestsSupported=true --filter 'FullyQualifiedName~CanRenderManyTopLevelMarkupNodesWithoutRepeatedSiblingScans|FullyQualifiedName~CanRenderManyWrappedMarkupNodesWithoutRepeatedSiblingScans|FullyQualifiedName~CanClearMarkupAfterScriptsRemoveAndMoveNodes|FullyQualifiedName~EnhancedNavigationTest.CanNavigateBetweenPagesWhenLayoutContainsInteractiveComponent|FullyQualifiedName~MarkupBlock'
  • Ownership behavior was compared in a temporary interactive Server sample with WebKit 27.2, using the original and current bundles (10 runs each, identical outcomes per version). These ownership cases are not covered by permanent tests because they depend on external mutation of Blazor-managed DOM.

A full framework build did not complete because of an unrelated MSB4006 circular dependency involving GetFilesToPublish in Microsoft.AspNetCore.App.Runtime.sfxproj; focused in-tree builds succeeded.

Fixes #70503

Note

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

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@github-actions github-actions Bot added the area-blazor Includes: Blazor, Razor Components label Nov 12, 2026
@maraf maraf self-assigned this Nov 12, 2026
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inserting a MarkupString with many top-level nodes is quadratic since .NET 11 (insertLogicalChild orphan scan)

1 participant