DOMParser: add direct accessors for names, values and namespaces #9
All Vouch review confidence checks passed
Residual risk 0.17 = Change Risk (0.49) × (1 − Review Confidence (0.66)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.49 · 135 lines diff across 4 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.56 · 4 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 10%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.294
No Reviewer Assigned
no_reviewer_assigned
0.120
Real PR Conversation Timeline
5 items
Is there a particular value to this comment?
It explains the performance benefit of these accessors in hot loops. I use xylem in a performance critical app and I needed to implement them to get the traversal performance in the same ballpark as the libxml I was using before.
Right but it seems that this explanation is better suited for the commit message rather than the implementation. The commit message should explain why the change is made/how it is correct. The local comments are to explain why the code is written in a non-obvious manner or doesn't match the surrounding code.
Agreed — moved it out. The rationale is in the commit message; the inline block is gone.
depth42
Summary
Adds three accessors on
Documentthat mirror the correspondingNodeView/AttributeViewproperties but read the node and attribute arenas directly:They accept both node and attribute references, so a full traversal no longer needs to bind a view.
Motivation
On Swift 6.4 (
-O), binding aNodeVieworAttributeViewto a local makes the compiler heap-box it: eachlet view = document.view(of: ref)compiles to aswift_allocObject(a 72-byte box) plus aswift_release. The view is copied into the box, a single getter is called, and the box is freed. A sampling profile of a walk that reads element names, namespaces, attributes and text is dominated bymalloc/free/swift_release_dealloc.The views are
~Copyable & ~Escapable, so callers can't work around this: chainingdocument.view(of: ref).namewithout a local binding is rejected with "lifetime-dependent value escapes its scope".Walk over a 9.5 MB document (154k elements, 183k attributes, ~212k text nodes), M2 Max, Swift 6.4, median of 15 runs:
| Walk | Time |
|-----------------------------------------|----------|
| structure only (
firstChild/nextSibling/parent) | 1.97 ms || names + namespaces + attributes + text via views | 20.00 ms |
| same via
name(of:)/value(of:)/namespace(of:)| 4.39 ms |Both full walks produce identical checksums.
Other changes
QualifiedNameView.init(unvalidated:colon:)copies the span it is given, so its lifetime dependency is now@_lifetime(copy bytes)instead ofborrow bytes. Withborrow, a name built from a span extracted inside a function can't be returned from it.Document.namespace(of:)with the same semantics; it now uses the public one.Testing
swift test: all suites pass. The new testdirect content accessors match node and attribute viewschecks every node and attribute kind (element, attribute, DTD, comment, PI, text, CDATA, document root) against the view-based API.DOMParserBenchmark/XPathBenchmarkvs.9881c95: instruction counts are flat (within ±2%) and wall-clock is within thresholds. The existing DOM benchmark only reads text through views, so it doesn't exercise the path this improves.Possible follow-up (not in this PR)
Builderreservescapacity >> 5nodes (1 per 32 input bytes). Pretty-printed documents with whitespace text nodes easily exceed that: one real-world, attribute-heavy file has ~1 node per 27 bytes. The 80-byteNodearray then doubles and gets copied mid-parse. Reservingcapacity >> 4gave ~5% faster parses on those files, but it doubles the up-front reservation for low-density documents, so I left it out.🤖 Assisted by Claude Opus 5.5