Skip to content

Avoid rebuilding unescaped text during HTML serialization - #1388

Merged
FlorianRappl merged 2 commits into
AngleSharp:develfrom
perfloop-oss:perfloop-pr-open-hwqec5t8jp
Oct 23, 2026
Merged

FlorianRappl merged 2 commits into
AngleSharp:develfrom
perfloop-oss:perfloop-pr-open-hwqec5t8jp

Conversation

@perfloop-agent

Copy link
Copy Markdown
Contributor

Prerequisites

  • I have read the CONTRIBUTING document
  • My code follows the code style of this project

Contribution Type

  • Bug fix (non-breaking change which fixes an issue, please reference the issue id)
  • New feature (non-breaking change which adds functionality, make sure to open an associated issue first)
  • Breaking change (breaking change that could impact existing functionality)
  • My change requires a change to the documentation
  • I have updated the documentation accordingly
  • I have added tests to cover my changes
  • All new and existing tests passed

Description

HtmlMarkupFormatter.EscapeText now checks for the four HTML text characters it escapes before obtaining a pooled builder. If none is present, it returns the original immutable string; escaped text keeps the existing entity output. No library API changes.

The change is recorded under a new 1.8.6 changelog section, and the .NET fallback and documentation package versions are aligned at 1.8.6.

The checked-in BenchmarkDotNet workload parses the committed W3C Selectors page once and measures complete default document.ToHtml() calls with the default pool. After building the benchmark project, run dotnet bin/Release/net10.0/AngleSharp.Benchmarks.dll --html-serialization from src/AngleSharp.Benchmarks.

The full-page comparison checks output against the prior escaping loop using a small external harness. That harness is not included in this PR; the checked-in benchmark provides a repository-native rerun.

W3C page ToHtml (11 paired runs): B/op 1.042 MB → 0.8068 MB (paired change -22.7%).

The result is limited to the committed W3C page. Production input frequency and production latency effects remain unmeasured.

Core and benchmark builds pass. The release-notes parser accepts the 1.8.6 heading, and the documentation package manifest and generated version agree with the .NET version.

The Core.Tests run excludes AngleSharp.Core.Tests.Examples.ReadmeTests.SimpleExampleOnReadmeShouldWork; it fetches live Wikipedia content and remains target-CI work.

Issue: #1387


Generated by Perfloop. Human sponsor: Tomás Senart.

Signed-off-by: Perfloop Agent <[email protected]>
Signed-off-by: Tomás Senart <[email protected]>
Signed-off-by: Perfloop Agent <[email protected]>
Signed-off-by: Tomás Senart <[email protected]>
@CLAassistant

CLAassistant commented Oct 18, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@FlorianRappl FlorianRappl left a comment

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.

I am not really convinced. There are multiple reasons:

  • The serializer is not the hot-path, so optimizations there are not so interesting in general (does not exclude this change, but makes it a lot less attractive)
  • The StringBuilder pool already optimizes allocation
  • The given optimization works great when a special character quite far back (or not even included) in the given string is present, but fails, e.g., if the first / an early character requires escaping
  • To identify if escaping is needed we still have to iterate through the string until we found one (IndexOfAny)

So the allocation reduction presented in the OP seems good, but presumably this is misleading. The increased allocation could mean that there are long internal strings that now have also expanded the string pool. This is, however, not useless / unused memory, but could be used again if longer strings are also detected in another parse run - among other scenarios. Key question is if computation time also goes down (I suspect it to be roughly equal, maybe even slightly higher).

Long story short: If it optimizes, it should cover the described cases also well, otherwise I only see complexity where we should remain simple.

@perfloop-agent

Copy link
Copy Markdown
Contributor Author

Response to this feedback.

The recorded result establishes lower allocation for the captured full-page serialization. The ns/op claim is only a no-regression guard, so it does not establish that computation time improves. Its measured Candidate median is lower in the completed run, but I will not present that as a time win; I will assess the reviewer’s early-escape cases and whether the change remains worthwhile without a demonstrated CPU improvement.

@FlorianRappl FlorianRappl left a comment

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.

It's a micro optimization but yes let's pick it in with the other changes for 1.8.6.

@FlorianRappl FlorianRappl added this to the v1.8.6 milestone Oct 23, 2026
@FlorianRappl
FlorianRappl merged commit fb371ee into AngleSharp:devel Oct 23, 2026
5 checks passed
This was referenced Oct 25, 2026
This was referenced Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants