[repo-assist] perf: HtmlDocument.ToString reuses one StringBuilder across all child elements - #1787
Draft
github-actions[bot] wants to merge 1 commit into
Conversation
… elements HtmlDocument.ToString() previously called element.ToString() for each top-level element, which created a new StringBuilder, serialised the node into it, extracted the intermediate string, and then appended that string to the document-level StringBuilder. For pages with N root elements this meant N extra string allocations and N extra copy passes. Fix: extract the serialisation logic into an internal SerializeTo(sb,...) method on HtmlNode so the document can pass its own StringBuilder directly. HtmlNode.ToString() creates the StringBuilder and delegates, preserving the public API unchanged. All 3926 tests pass. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🤖 This PR was created by Repo Assist, an automated AI assistant.
Summary
HtmlDocument.ToString()previously calledelement.ToString()for each top-level child element, which:StringBuilderinternallystringstringto the document-levelStringBuilderFor a typical HTML page with
<html>,<head>,<body>(3+ root elements), this meant 3 extra string allocations and 3 extra copy passes before the final document string was available.Fix
Extract the serialisation logic into an
internalSerializeTo(sb, indentation, canAddNewLine, insidePre)method onHtmlNode.HtmlDocument.ToString()now passes its ownStringBuilderdirectly, eliminating the intermediate strings.HtmlNode.ToString()creates a freshStringBuilderand delegates toSerializeTo— the public API is unchanged.Benefit
HtmlDocumentvalues (common in HTML type provider output and scraping pipelines).<!DOCTYPE>,<html>, this removes at least 2 intermediate string allocations.Trade-offs
SerializeTois markedinternal— it does not affect the public API.Test Status
All 3926 tests pass on Linux/net8.0:
Add this agentic workflow to your repo
To install this agentic workflow, run