Repository navigation
Avoid rebuilding unescaped text during HTML serialization - #1388
Conversation
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]>
FlorianRappl
left a comment
There was a problem hiding this comment.
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
StringBuilderpool 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.
|
Response to this feedback. The recorded result establishes lower allocation for the captured full-page serialization. The |
FlorianRappl
left a comment
There was a problem hiding this comment.
It's a micro optimization but yes let's pick it in with the other changes for 1.8.6.
Prerequisites
Contribution Type
Description
HtmlMarkupFormatter.EscapeTextnow 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, rundotnet bin/Release/net10.0/AngleSharp.Benchmarks.dll --html-serializationfromsrc/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/op1.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.