← Back to Pull requests

DOMParser: add direct accessors for names, values and namespaces #9

Open
depth42 opened this pull request with 135 lines diff across 4 files
View on GitHub
depth42 depth42 commented on Oct 02, 2026 09:01 UTC
Author

Summary

Adds three accessors on Document that mirror the corresponding NodeView/AttributeView properties but read the node and attribute arenas directly:

public func name(of node: Reference) -> XML.QualifiedNameView?
public func value(of node: Reference) -> Span<XML.Byte>?
public func namespace(of node: Reference) -> Span<XML.Byte>?

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 a NodeView or AttributeView to a local makes the compiler heap-box it: each let view = document.view(of: ref) compiles to a swift_allocObject (a 72-byte box) plus a swift_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 by malloc/free/swift_release_dealloc.

The views are ~Copyable & ~Escapable, so callers can't work around this: chaining document.view(of: ref).name without 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 of borrow bytes. With borrow, a name built from a span extracted inside a function can't be returned from it.

  • XPath had an internal Document.namespace(of:) with the same semantics; it now uses the public one.
  • Testing

  • swift test: all suites pass. The new test direct content accessors match node and attribute views checks every node and attribute kind (element, attribute, DTD, comment, PI, text, CDATA, document root) against the view-based API.

  • XMLTS conformance tests weren't run locally (artifact not fetched); the change doesn't touch the SAX parser or lexer.

  • DOMParserBenchmark/XPathBenchmark vs. 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)

    Builder reserves capacity >> 5 nodes (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-byte Node array then doubles and gets copied mid-parse. Reserving capacity >> 4 gave ~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

    Diff: 135 lines (+122 / -13) across 4 files Reviewer: None requested Review duration: 9502m
    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
    0.49
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.56 · 4 substantive comment(s)
    0.56
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 10%
    65%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    compnerd compnerd commented on code on Oct 05, 2026 16:45 UTC
    Code Review
    Sources/DOMParser/Document.swift:1
    @@ -95,6 +95,55 @@ public struct Document: ~Copyable, Sendable {
         return XML.Location(line: Int(location.line), offset: Int(location.offset))
       }
     
    +  // MARK: - Node content
    +  //
    +  // These mirror the corresponding ``NodeView`` and ``AttributeView``
    +  // properties but read the arenas directly.  Prefer them in hot traversal
    +  // loops: binding a `~Copyable & ~Escapable` view to a local currently costs
    +  // a heap allocation per view.

    Is there a particular value to this comment?

    depth42 depth42 commented on code on Oct 06, 2026 10:03 UTC
    Code Review
    Sources/DOMParser/Document.swift:1
    @@ -95,6 +95,55 @@ public struct Document: ~Copyable, Sendable {
         return XML.Location(line: Int(location.line), offset: Int(location.offset))
       }
     
    +  // MARK: - Node content
    +  //
    +  // These mirror the corresponding ``NodeView`` and ``AttributeView``
    +  // properties but read the arenas directly.  Prefer them in hot traversal
    +  // loops: binding a `~Copyable & ~Escapable` view to a local currently costs
    +  // a heap allocation per view.

    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.

    compnerd compnerd commented on code on Oct 06, 2026 15:00 UTC
    Code Review
    Sources/DOMParser/Document.swift:1
    @@ -95,6 +95,55 @@ public struct Document: ~Copyable, Sendable {
         return XML.Location(line: Int(location.line), offset: Int(location.offset))
       }
     
    +  // MARK: - Node content
    +  //
    +  // These mirror the corresponding ``NodeView`` and ``AttributeView``
    +  // properties but read the arenas directly.  Prefer them in hot traversal
    +  // loops: binding a `~Copyable & ~Escapable` view to a local currently costs
    +  // a heap allocation per view.

    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.

    depth42 depth42 commented on code on Oct 06, 2026 15:32 UTC
    Code Review
    Sources/DOMParser/Document.swift:1
    @@ -95,6 +95,55 @@ public struct Document: ~Copyable, Sendable {
         return XML.Location(line: Int(location.line), offset: Int(location.offset))
       }
     
    +  // MARK: - Node content
    +  //
    +  // These mirror the corresponding ``NodeView`` and ``AttributeView``
    +  // properties but read the arenas directly.  Prefer them in hot traversal
    +  // loops: binding a `~Copyable & ~Escapable` view to a local currently costs
    +  // a heap allocation per view.

    Agreed — moved it out. The rationale is in the commit message; the inline block is gone.

    Reviewers
    None requested
    Assignees
    depth42 depth42
    Labels
    risk: low confidence: 65.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 48.8% (MEDIUM) · Higher = larger blast radius
    48.8%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 65.8% · Lower = rushed or shallow review
    65.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 16.7% · Safe to merge
    16.7%
    Repository