Skip to content

GetInnerText walks all ancestors for visibility on every element #262

Closed

Description

@michaelthyregod

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.Xml for 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

  1. 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.
  2. Pass the parent's visibility down in GetInnerText. An element that does not declare visibility has its parent's. This is smaller and local, but only fixes GetInnerText.

Happy to send a PR with paired benchmarks (real-world GetInnerText, render tree, deep trees) for whichever you prefer.

Activity

  1. FlorianRappl commented on Nov 2, 2026

    @FlorianRappl
    Contributor

    I don't see the problem.

    It should just use the render tree, which now has caching and continues to improve performance.

  2. michaelthyregod commented on Nov 2, 2026

    @michaelthyregod
    ContributorAuthor

    Thanks. I checked the render tree route: building it is cheap, but asking it what is visible is not. RenderNodeExtensions.IsVisible reads visibility from 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 nested divs, calling IsVisible() 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 when div declares visibility (rough numbers, busy machine). Render() itself takes 11-61 ms there.

    So moving GetInnerText onto the render tree alone would keep the walk. Caching the result of Snapshot.Inherit per name (snapshots are immutable) would fix both GetInnerText and IsVisible, and anything else reading an undeclared inherited property. I'm happy to do that, or to rebuild GetInnerText on the render tree plus the cache, whichever you prefer, with paired benchmarks.

  3. FlorianRappl commented on Nov 2, 2026

    @FlorianRappl
    Contributor

    In general GetInnerText should 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.

  4. michaelthyregod commented on Nov 2, 2026

    @michaelthyregod
    ContributorAuthor

    With #263 merged, here is how I would rebuild GetInnerText on 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 GetInnerText test fixtures and on both documents of InnerTextBenchmarks (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 GetInnerText never reads:

    • it cascades ::before and ::after for every element (about a third of the difference),
    • it builds the specified style of every element,
    • it computes the styles of hidden subtrees (display: none) and of elements without a box (script, style, ...), which today's walk skips.

    Proposal

    1. Make the render tree cheaper (a small PR of its own, measured with CssCascadeBenchmarks.RenderTree):
      • skip the ::before / ::after cascade when no rule has a pseudo-element subject,
      • build the specified style of a node only when it is read.
    2. Rebuild GetInnerText on 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.
    3. Optional: a public 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

    • Shadow DOM: the render tree shows the shadow tree of a host (with the slotted nodes in their slots), while today's GetInnerText reads the host's own children. For <div> with a shadow root <p>shadow</p><slot> and the light child <span>light</span>, today gives light and the render tree shadow\n\nlight. Should GetInnerText follow the render tree here, or keep today's result?
    • Step 3: do you want the public overload?
  5. FlorianRappl commented on Nov 3, 2026

    @FlorianRappl
    Contributor

    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 GetInnerText on an ElementRenderNode would also be a good addition.

    Regarding shadow DOM: I tried putting these words into an example. Correct me if I misunderstood...

    <div id="host">
      #shadow-root
        <p>shadow</p>
        <slot></slot>
      <span>light</span>
    </div>

    I think the correct behavior (again, please check) should be:

    API / behavior Result
    textContent light
    innerText (rendered host) shadow\n\nlight
    Render tree text shadow\n\nlight

    So 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 innerText is to give back the text that would be rendered.

  6. added this to the v1.4.0 milestone on Nov 3, 2026
  7. michaelthyregod commented on Nov 4, 2026

    @michaelthyregod
    ContributorAuthor

    Thanks! Your example is what I meant. I checked the shadow DOM part, and innerText does not follow the render tree there:

    • The spec's getter runs the rendered text collection steps "for each child node" of the element, and those recurse into each child node in tree order: the DOM children, never the shadow root.
    • WPT tests it as "Shadow DOM contents ignored" (getter-tests.js): a host whose shadow root holds abc has innerText "".

    So for the example:

    API / behavior Result
    textContent light
    innerText light (the span is slotted, so it is rendered)
    Render tree text shadow\n\nlight

    Being rendered still matters: a light child assigned to no slot is not rendered and drops out of innerText, which today's GetInnerText gets wrong. So GetInnerText walks 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. GetInnerText reads 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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions