Skip to content

perf!: 88-byte Node and no unused arena capacity (HTML trees -63% heap, -22% parse Ir) - #29

Merged
Ttimmahlax merged 2 commits into
mainfrom
perf/node-layout-22-23
Nov 13, 2026
Merged

Ttimmahlax merged 2 commits into
mainfrom
perf/node-layout-22-23

Conversation

@Ttimmahlax

Copy link
Copy Markdown
Contributor

Fixes #22, fixes #23.

Breaking change, for 0.9.0. Node's public field types change. Code that only uses XmlDoc accessors (parent, first_child, prefix, ns_uri, ns_defs, …) is unaffected; code that reads Node fields directly needs the changes listed below.

What changes

#23: Node goes from 184 to 88 bytes.

  • The seven links are Link, a NodeId stored as id + 1 in a NonZeroU32: 4 bytes instead of 8 for Option<NodeId>. Link::get() gives the Option<NodeId>. The XmlDoc accessors keep returning Option<NodeId>, and last_attr gains an accessor.
  • prefix and ns_uri are Option<Atom> (4 bytes each), interned per document. XmlDoc::prefix / ns_uri still return Option<&str>. New: XmlDoc::intern, atom, set_prefix, set_ns_uri. A document now keeps one copy of each distinct prefix and URI instead of one per node.
  • ns_defs moves off the node into a per-document side table, sorted by element. XmlDoc::ns_defs and push_ns_def keep their signatures. Few elements declare namespaces, so this costs less than 8 bytes on every node.
  • Interning scans while a document has at most 16 distinct strings, then switches to a keyed hash index, so input with many distinct prefixes or URIs stays linear.
  • Both cross-document copies, xml_copy_children_from (entity content from a sub-parser) and XInclude's copy_node, re-intern through the destination document.
  • A test pins size_of::<Node>() == 88 on 64-bit targets.

#22: no unused arena capacity is kept.

  • The HTML parser reserves one node per 10 input bytes, the densest markup measured, so the arena never doubles and copies during the parse. It then calls the new XmlDoc::shrink_nodes once the tree is built.
  • The XML parser already reserved at that rate; it now shrinks too.

Memory

Retained heap: requested bytes still live after the parse returns, from a counting global allocator, summed over each corpus with every tree measured before it is dropped.

Corpus Before After Change
HTML, 197 real pages (8.2 MB) 89.2 MB (10.9× input) 32.7 MB (4.0×) −63%
HTML, 25 stress fragments 3.42 MB 1.14 MB −67%
XML, 8 files from libxml2's test tree (1.9 MB, namespaced docbook/RELAX NG included) 37.6 MB 12.9 MB −66%

On the real pages, the arena in use falls from 53.4 MB to 25.5 MB, and unused capacity from 28.7 MB to 2 KB. The largest single-document peak during a parse halves: 6.6 MB → 3.5 MB for HTML, 12.0 MB → 6.0 MB for XML.

Instruction counts

Callgrind, against main at c4d5365, with identical tree fingerprints on both HTML corpora. HTML is html_read_memory inclusive; XML is whole-program rxmlint --noout over the same 8 files.

Workload Before 88-byte Node + arena sizing (this PR) Change
HTML fragments, parse Ir 297,873,922 252,148,710 237,217,071 −20.4%
HTML real pages, parse Ir 315,625,915 268,804,528 245,414,738 −22.2%
XML, total Ir 187,789,004 184,169,966 184,517,141 −1.7%

Where it comes from, at function level on the real pages:

  • Layout: memcpy −43.5M Ir, because each node moved into the arena is half the size. Dropping a tree no longer visits a Vec and two Option<String>s per node: −9.6M and −6.1M, against +7.7M of Vec<Node> drop.
  • Arena sizing: memcpy another −23.7M Ir, because the doubling copies are gone.

The XML shrink costs +0.19% Ir for −31% retained XML heap.

Refuted

  • Shrink without reserving: HTML parse Ir rose 0.5% (fragments) and 2.0% (pages) over the layout alone, because the shrink then copies an arena that grew by doubling. Reserve-then-shrink is the only arm that wins on both instructions and memory.
  • The boxed namespace layout (88 B): not built. It would add a heap box to every namespaced XML node. Interning plus the ns_defs side table reaches the same 88 bytes without one, and stores each URI once per document instead of once per node. (The XML parser still builds an owned URI String per namespaced element before interning it; removing that transient allocation is a separate change.)

Migrating

Before After
doc.node(id).parent (likewise the other six links) doc.node(id).parent.get(), or doc.parent(id)
doc.node_mut(id).parent = Some(p) = p.into() / Link::from(Some(p))
doc.node(id).prefix / .ns_uri as Option<String> doc.prefix(id) / doc.ns_uri(id)
doc.node_mut(id).prefix = Some(s) doc.set_prefix(id, Some(&s))
doc.node(id).ns_defs doc.ns_defs(id)

Validation

  • cargo test --workspace --exclude rusty_xml-bench: gates 97, html_fixtures 6, html_libxml2 25, plus 3 new tree tests (size, link and atom round trips including past the scan limit, and side-table order when declarations arrive out of creation order).
  • fuzz 150000 on seeds 1 to 5: all exit 0.
  • Differential against the pinned xmllint 2.15.3: unchanged, 22/25 fragments and 194/197 pages identical.
  • cargo +1.85 check -p rusty_xml --tests and cargo check -p rusty_xml --target wasm32-unknown-unknown: both clean.

🤖 Generated with Claude Code

tim-almond-house and others added 2 commits November 13, 2026 11:51
BREAKING CHANGE: `Node`'s public field types change.
- The seven links are `Link`, a 4-byte `NodeId` niche (`id + 1` in a
  `NonZeroU32`), instead of 8-byte `Option<NodeId>`. `Link::get`
  returns the `Option<NodeId>`; `XmlDoc::parent`, `first_child` and the
  other accessors keep their signatures, and `last_attr` gains one.
- `prefix` and `ns_uri` are `Option<Atom>`, interned per document
  (`XmlDoc::intern`, `atom`, `set_prefix`, `set_ns_uri`).
  `XmlDoc::prefix` / `ns_uri` still return `Option<&str>`.
- `ns_defs` moves off the node into a per-document side table sorted by
  element; `XmlDoc::ns_defs` and `push_ns_def` are unchanged.

Interning scans while a document has at most 16 distinct strings and
uses a keyed hash index after, so input with many distinct prefixes or
URIs stays linear. Both cross-document copies (`xml_copy_children_from`
and XInclude's `copy_node`) re-intern through the destination.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…t after the parse (#22)

The HTML parser started from a 4-node arena and let it double, and no
parser released the capacity left over. On 197 real pages a third of
the retained heap was arena capacity no node used.

- HTML reserves one node per 10 input bytes, the densest markup
  measured, so the arena never reallocates during the parse, then
  calls the new `XmlDoc::shrink_nodes` once the tree is built.
- The XML parser already reserved at that rate; it now shrinks too.

Measured on top of the 88-byte Node, against the same build without
this commit. Parse Ir: HTML fragments 252,148,710 -> 237,217,071
(-5.9%), real pages 268,804,528 -> 245,414,738 (-8.7%); XML total
+0.19%. Retained heap: real pages 46.4 MB -> 32.7 MB, XML 18.6 MB ->
12.9 MB. Shrinking without reserving (refuted) raised HTML parse Ir by
0.5% / 2.0%, because the shrink then copies a doubled arena.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Node is 184 bytes, and most of it is empty for HTML HTML trees keep up to half their node arena unused

2 participants