Repository navigation
perf!: 88-byte Node and no unused arena capacity (HTML trees -63% heap, -22% parse Ir) - #29
Merged
Merged
Conversation
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>
This was referenced Nov 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #22, fixes #23.
What changes
#23:
Nodegoes from 184 to 88 bytes.Link, aNodeIdstored asid + 1in aNonZeroU32: 4 bytes instead of 8 forOption<NodeId>.Link::get()gives theOption<NodeId>. TheXmlDocaccessors keep returningOption<NodeId>, andlast_attrgains an accessor.prefixandns_uriareOption<Atom>(4 bytes each), interned per document.XmlDoc::prefix/ns_uristill returnOption<&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_defsmoves off the node into a per-document side table, sorted by element.XmlDoc::ns_defsandpush_ns_defkeep their signatures. Few elements declare namespaces, so this costs less than 8 bytes on every node.xml_copy_children_from(entity content from a sub-parser) and XInclude'scopy_node, re-intern through the destination document.size_of::<Node>() == 88on 64-bit targets.#22: no unused arena capacity is kept.
XmlDoc::shrink_nodesonce the tree is built.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.
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
mainat c4d5365, with identical tree fingerprints on both HTML corpora. HTML ishtml_read_memoryinclusive; XML is whole-programrxmlint --nooutover the same 8 files.NodeWhere it comes from, at function level on the real pages:
memcpy−43.5M Ir, because each node moved into the arena is half the size. Dropping a tree no longer visits aVecand twoOption<String>s per node: −9.6M and −6.1M, against +7.7M ofVec<Node>drop.memcpyanother −23.7M Ir, because the doubling copies are gone.The XML shrink costs +0.19% Ir for −31% retained XML heap.
Refuted
ns_defsside 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 URIStringper namespaced element before interning it; removing that transient allocation is a separate change.)Migrating
doc.node(id).parent(likewise the other six links)doc.node(id).parent.get(), ordoc.parent(id)doc.node_mut(id).parent = Some(p)= p.into()/Link::from(Some(p))doc.node(id).prefix/.ns_uriasOption<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_defsdoc.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 150000on seeds 1 to 5: all exit 0.cargo +1.85 check -p rusty_xml --testsandcargo check -p rusty_xml --target wasm32-unknown-unknown: both clean.🤖 Generated with Claude Code