Skip to content

Tree builder and RcDom take time growing with the square of the input on four simple inputs #812

Open

Description

@JuroOravec

Hi,

These four inputs make parse_document with markup5ever_rcdom take time that roughly quadruples each time the input doubles. None of them crashes. A program parsing untrusted HTML can limit the input size, so I'm filing this as a performance report.

Measured with html5ever 0.39.0 and markup5ever_rcdom 0.39.0+unofficial (the newest pair that build together; html5ever 0.40.1 needs a newer rcdom), release build, Apple M-series:

Input Size Time
1,000 <body> start tags, each with 64 new attribute names 489 KB 1.35 s
2,000 of them 1.0 MB 5.05 s
<p><b x{i}>t</p> repeated 4,000 times (i counts up) 65 KB 1.02 s
repeated 8,000 times 131 KB 4.12 s
40,000 nested <div> 195 KB 1.69 s
80,000 nested <div> 390 KB 9.00 s
<table>x</table> repeated 40,000 times 625 KB 0.44 s
repeated 80,000 times 1.25 MB 1.73 s

Where the time seems to go, from reading the code (I have not profiled every case):

  1. Repeated <body> (and <html>) tags. The tree builder calls TreeSink::add_attrs_if_missing for each repeated start tag (tree_builder/rules.rs). RcDom's implementation builds a HashSet of every attribute the element already has, on every call, so each new tag pays again for all the attributes the earlier ones added.
  2. Misnested formatting tags. Each <p> closes over an open <b>, so the <b> is reconstructed in the next paragraph. With distinct attributes, none of the entries in the list of active formatting elements match, and the list and the reconstruction work grow with the number already seen.
  3. Deep nesting. Each new element appears to cost time in the depth of the stack of open elements.
  4. Text directly inside tables. The text is foster-parented, and RcDom appears to scan the parent's children to place it, so each new piece costs time in the number already placed.

The attribute-name check in the tokenizer is already covered by #796 and #804, so it isn't repeated here.

Reproduction (cargo run --release):

[dependencies]
html5ever = "0.39"
markup5ever_rcdom = "=0.39.0+unofficial"
use std::time::Instant;

use html5ever::tendril::TendrilSink;
use html5ever::{parse_document, ParseOpts};
use markup5ever_rcdom::RcDom;

fn time(label: &str, html: String) {
    let start = Instant::now();
    let _dom = parse_document(RcDom::default(), ParseOpts::default())
        .one(html.as_str());
    println!("{label}: {:.2?} ({} KB)", start.elapsed(), html.len() / 1024);
}

fn main() {
    for n in [500, 1_000, 2_000] {
        let html = (0..n)
            .map(|i| {
                let attrs: String =
                    (0..64).map(|j| format!(" b{i}_{j}")).collect();
                format!("<body{attrs}>")
            })
            .collect();
        time(&format!("{n} <body> tags x 64 new attributes"), html);
    }
    for n in [2_000, 4_000, 8_000] {
        let html = (0..n).map(|i| format!("<p><b x{i}>t</p>")).collect();
        time(&format!("{n} x <p><b x{{i}}>t</p>"), html);
    }
    for n in [20_000, 40_000, 80_000] {
        time(&format!("{n} nested <div>"), "<div>".repeat(n));
    }
    for n in [20_000, 40_000, 80_000] {
        time(&format!("{n} x <table>x</table>"), "<table>x</table>".repeat(n));
    }
}

Happy to test patches.

Activity

  1. jdm commented on Nov 3, 2026

    @jdm
    Member

    Rcdom is only used for writing tests for this repository and we don't publish it. We're pretty conservative about accepting patches for it that aren't required for its primary use case.

  2. hherb commented on Nov 9, 2026

    @hherb

    A data point on case 2 (misnested formatting elements): memory grows with the square of the input too, not only time, because each reconstruction creates real elements in the sink. This is with scraper's HtmlTreeSink (scraper 0.27, html5ever 0.39, release build, Apple M-series), not RcDom.

    Input: <div> + N × <b id={k}> + </div>, then <div>x</div> × N. Each x reconstructs all N entries, which have distinct attributes so Noah's Ark matches none, so about N² elements are created.

    N bytes time, fed in 16,384-char chunks time, one parse_document peak RSS
    1,000 21,901 0.30 s 186 MB
    2,000 44,901 1.18 s 0.49 s 715 MB (712 MB one-shot)
    4,000 90,901 4.71 s 1.97 s 2.8 GB (2.8 GB one-shot)

    markupever 0.4.0 (html5ever 0.40) shows the same shape: 313 MB, 1.2 GB, 4.6 GB at those N. So a caller that bounds parse time by feeding chunks against a deadline still sees memory climb with every second of parse (here ~0.6 GB/s chunked, ~1.4 GB/s one-shot; ~2.3 GB/s in markupever), and a size cap does not help much, since 91 KB is already 2.8 GB. Reproduction:

    let n = 4000;
    let mut html = String::from("<div>");
    for k in 0..n { html.push_str(&format!("<b id={k}>")); }
    html.push_str("</div>");
    for _ in 0..n { html.push_str("<div>x</div>"); }
    let _doc = scraper::Html::parse_document(&html);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions