Repository navigation
Description
Prerequisites
- Can you reproduce the problem in a MWE?
- Are you running the latest version of AngleSharp.Css?
- Did you check the FAQs to see if that helps you?
- Are you reporting to the correct repository? (there are multiple AngleSharp libraries, e.g.,
AngleSharp.Xmlfor Xml support) - Did you perform a search in the issues?
Description
GetInnerText reads visibility for every element (ItcInCssBox in ElementExtensions.cs). visibility is inherited and usually declared nowhere, and since 1.3.0 a computed style looks inherited declarations up through the parents' snapshots instead of copying them (CssStyleDeclaration.Snapshot.Inherit). A property that no ancestor declares is never found, so every lookup walks up to the root: the cost per element grows with its depth, and on deep trees the total grows faster than linearly. Real pages pay the same walk on every element, only shorter (their depth).
Related to the earlier GetInnerText performance issue #55, but a different cause.
Steps to Reproduce
On devel (c5d2853), net10.0:
var css = "div{color:red}"; // vs. "div{color:red;visibility:visible}"
var document = BrowsingContext.New(Configuration.Default.WithCss())
.GetService<IHtmlParser>().ParseDocument("<style>" + css + "</style>");
IElement current = document.Body;
for (var i = 0; i < depth; i++)
{
var child = document.CreateElement("div");
current.AppendChild(child);
current = child;
}
current.TextContent = "x";
document.Body.GetInnerText();Best of 7 runs, on a thread with a large stack (the walk over the tree is recursive), and not on an idle machine, so the numbers are rough:
| Depth | div{color:red} |
div{color:red;visibility:visible} |
|---|---|---|
| 1000 | 14 ms | 1.6 ms |
| 2000 | 43 ms | 3.7 ms |
| 4000 | 69 ms | 17 ms |
| 8000 | 440 ms | 36 ms |
Expected Behavior
Reading an inherited property that no ancestor declares costs about the same on every element, independent of its depth.
Actual Behavior
Every lookup walks all ancestor snapshots. Declaring visibility on every element stops each walk after one step, and that alone makes GetInnerText 4-12x faster here. The second column still grows faster than linearly, so there is a smaller second cost I have not identified yet.
Possible Solution / Known Workarounds
- Memoize lookups in the snapshot. Snapshots are immutable, so the result of
Inherit(including "not found") can be cached per name. This also helps the render tree and anything else that reads an undeclared inherited property, at the cost of some memory per snapshot. - Pass the parent's visibility down in
GetInnerText. An element that does not declarevisibilityhas its parent's. This is smaller and local, but only fixesGetInnerText.
Happy to send a PR with paired benchmarks (real-world GetInnerText, render tree, deep trees) for whichever you prefer.
Activity
FlorianRappl commentedon Nov 2, 2026
I don't see the problem.
It should just use the render tree, which now has caching and continues to improve performance.
michaelthyregod commentedon Nov 2, 2026
Thanks. I checked the render tree route: building it is cheap, but asking it what is visible is not.
RenderNodeExtensions.IsVisiblereadsvisibilityfrom the render nodes' computed styles, and those look inherited declarations up through the parent snapshots the same way. On a pre-built render tree for the same nesteddivs, callingIsVisible()on every node takes 7 / 10 / 38 / 253 ms at depth 1000 / 2000 / 4000 / 8000, and 0.1 / 0.2 / 0.6 / 0.5 ms whendivdeclaresvisibility(rough numbers, busy machine).Render()itself takes 11-61 ms there.So moving
GetInnerTextonto the render tree alone would keep the walk. Caching the result ofSnapshot.Inheritper name (snapshots are immutable) would fix bothGetInnerTextandIsVisible, and anything else reading an undeclared inherited property. I'm happy to do that, or to rebuildGetInnerTexton the render tree plus the cache, whichever you prefer, with paired benchmarks.FlorianRappl commentedon Nov 2, 2026
In general
GetInnerTextshould re-use / build on-top of the render tree. Otherwise we need to optimize two things (and inner text - while doing some own logic - fully relies on reading computed styles, i.e., on the render tree).The way I see it it's
to rebuild GetInnerText on the render tree plus the cache.Remember inherited lookups on the way up
michaelthyregod commentedon Nov 2, 2026
With #263 merged, here is how I would rebuild
GetInnerTexton the render tree, before writing it. I tried it as a prototype: render the element's subtree, then walk the render nodes with today's rules (line breaks, tables, paragraphs,white-space,text-transform).Output: the same as today on all 46
GetInnerTexttest fixtures and on both documents ofInnerTextBenchmarks(68,868 and 757,568 characters, identical). The one difference is a shadow host (see below).Cost: as is, it is slower: on the selector-heavy document about 1,270 ms instead of about 750 ms (rough, best of 5). The render tree does work that
GetInnerTextnever reads:::beforeand::afterfor every element (about a third of the difference),display: none) and of elements without a box (script,style, ...), which today's walk skips.Proposal
CssCascadeBenchmarks.RenderTree):::before/::aftercascade when no rule has a pseudo-element subject,GetInnerTexton the render tree: render the element's subtree and walk it; generated content stays out, as today. An internal option leaves out the subtrees of hidden and box-less elements for this walk;Render()keeps building the full tree.GetInnerText(this ElementRenderNode node), so a caller with a render tree gets the text without computing any style again.Both PRs with paired benchmarks against
devel.Questions
GetInnerTextreads the host's own children. For<div>with a shadow root<p>shadow</p><slot>and the light child<span>light</span>, today giveslightand the render treeshadow\n\nlight. ShouldGetInnerTextfollow the render tree here, or keep today's result?FlorianRappl commentedon Nov 3, 2026
Yeah the proposal sounds good. Especially the computation of a partial render tree (e.g., skipping before, after, or anything else that is not required) would be good. This way, one can either construct a partial render tree (which could be completed any time) or use a partial tree (which, in case of missing items, would re-compute those - otherwise, it could just take it as is).
The
GetInnerTexton anElementRenderNodewould also be a good addition.Regarding shadow DOM: I tried putting these words into an example. Correct me if I misunderstood...
I think the correct behavior (again, please check) should be:
lightshadow\n\nlightshadow\n\nlightSo in my opinion it should follow the render tree here. In general the render tree is also what would be rendered and the job of
innerTextis to give back the text that would be rendered.michaelthyregod commentedon Nov 4, 2026
Thanks! Your example is what I meant. I checked the shadow DOM part, and
innerTextdoes not follow the render tree there:abchasinnerText"".So for the example:
lightlight(the span is slotted, so it is rendered)shadow\n\nlightBeing rendered still matters: a light child assigned to no slot is not rendered and drops out of
innerText, which today'sGetInnerTextgets wrong. SoGetInnerTextwalks the DOM children and asks the render tree whether (and how) each one is rendered.For the partial tree, my plan is that each render node computes its specified style,
::before/::after, and children on first read (the pseudo-elements not at all when no rule targets them).Render()completes the tree right away, so it returns the same as today.GetInnerTextreads only what it needs, so hidden subtrees are never computed. I'd keep the partial tree internal in the first PR and make it public once the semantics are settled (a part computed later sees the DOM of that moment).Build the parts of the render tree only when needed
Get the inner text from a partial render tree