PureXML/Sources/Emitting/Serializer.swift
Mihaela Mihaljevic d359208ff6 perf(serialize): precompute the attribute quote, bulk-copy comment/PI escaping
Two profile-guided serialize passes (#139, docs/benchmarks.md passes 29-30).
The attribute writer now precomputes the quote character as a String once
per element instead of interpolating a Character into every attribute,
avoiding the grapheme-breadcrumb machinery that repeats twice per attribute
for a value that never changes within an element. Escaping.comment and
processingInstruction's escaping fallback (taken whenever a comment has a
hyphen or PI data has a '?', a case pass 8 could skip past but not speed up)
is rewritten in the same byte-scan bulk-copy shape as passes 23-24, so a
value with one rare trigger no longer pays a full character-by-character
rebuild.

Both are same-observable-behavior changes. The quote change is byte-
identical by construction. The comment/PI rewrite is verified against a new
differential suite that reimplements the original character-loop as an
independent oracle and checks adjacency/trailing/combining-mark cases plus
two 20000-case seeded fuzzes, since a byte match inside a combining-mark
cluster is not, on its own, the bare trigger character. Full local suite
(1925 tests) unchanged.

Measured with interleaved same-state A/B: pass 29 ~1.5% at a 100k-item
corpus; pass 30 ~2x on a dedicated comment/PI micro-benchmark (the standard
corpus has no comment/PI content, so real documents with copyright banners
or hyphenated comment prose see this, not the headline ratio). A same-
session multi-append restructuring attempt measured a clear ~6% regression
and was reverted; a suspected hasInlineContent double-scan was profiled at
~0.2% of samples and not worth a pass. Both outcomes are recorded in
docs/benchmarks.md rather than dropped.
2026-07-15 04:05:29 +02:00

182 lines
7.6 KiB
Swift

/// One step of the explicit serialization walk: either a node to write, or a
/// pending close tag to emit once a node's children are done. File-scope and
/// private: an internal detail of the iterative serializer.
private enum SerializeStep {
case node(PureXML.Model.Node, Int, Bool)
case close(SerializeClose)
}
/// The deferred close tag for an element, carried on the work stack.
private struct SerializeClose {
let name: String
let depth: Int
let formatted: Bool
let indentClose: Bool
}
public extension PureXML.Emitting {
/// Serializes a ``PureXML/Model/Node`` tree back into XML text.
///
/// The walk is iterative, not recursive: it drives an explicit work stack so
/// that arbitrarily deep documents cannot overflow the call stack (the
/// approach libxml2 takes in `xmlNodeDumpOutputInternal`). Reimplemented in
/// Swift; no upstream code is copied.
struct Serializer: Sendable {
public var options: Options
public init(options: Options = .default) {
self.options = options
}
/// Serializes a node into bytes in `encoding`, the libxml2
/// `xmlSaveToFilename(..., encoding)` model. The XML declaration carries the
/// encoding's canonical name, a byte-order mark precedes a UTF-16/32 stream,
/// and any scalar the encoding cannot represent becomes a numeric character
/// reference. Falls back to UTF-8 for an encoding without an output table.
public func serialize(_ node: PureXML.Model.Node, encoding: PureXML.Parsing.InputEncoding) -> [UInt8] {
var encodingOptions = options
encodingOptions.includeXMLDeclaration = true
encodingOptions.encodingName = PureXML.Parsing.ByteEncoder.canonicalName(encoding)
let text = Serializer(options: encodingOptions).serialize(node)
return PureXML.Parsing.ByteEncoder.byteOrderMark(encoding)
+ PureXML.Parsing.ByteEncoder.encode(text, as: encoding)
}
/// Serializes a node into an XML string.
public func serialize(_ node: PureXML.Model.Node) -> String {
var output = ""
if let declaration = options.xmlDeclaration {
output += declaration + options.lineEnding
}
var stack: [SerializeStep] = [.node(node, 0, options.prettyPrint)]
while let step = stack.popLast() {
switch step {
case let .node(node, depth, formatted):
write(node, depth: depth, formatted: formatted, output: &output, stack: &stack)
case let .close(close):
if close.indentClose {
output += pad(close.depth)
}
output += "</\(close.name)>"
if close.formatted {
output += options.lineEnding
}
}
}
return output
}
private func write(
_ node: PureXML.Model.Node,
depth: Int,
formatted: Bool,
output: inout String,
stack: inout [SerializeStep],
) {
switch node {
case let .document(children):
push(children, depth: depth, formatted: formatted, into: &stack)
case let .element(element):
write(element, depth: depth, formatted: formatted, output: &output, stack: &stack)
case let .text(value):
output += Escaping.text(value, asciiOnly: options.asciiOnly, escapeCarriageReturn: options.textEscaping.escapesCarriageReturn)
case let .cdata(value):
output += options.cdataAsText
? Escaping.text(value, asciiOnly: options.asciiOnly, escapeCarriageReturn: options.textEscaping.escapesCarriageReturn)
: "<![CDATA[\(value)]]>"
case let .comment(value):
output += "<!--\(Escaping.comment(value))-->"
case let .processingInstruction(target, data):
output += data.isEmpty ? "<?\(target)?>" : "<?\(target) \(Escaping.processingInstruction(data))?>"
}
}
private func write(
_ element: PureXML.Model.Element,
depth: Int,
formatted: Bool,
output: inout String,
stack: inout [SerializeStep],
) {
if formatted {
output += pad(depth)
}
output += "<" + element.name.description
let quote = options.attributeQuote.character
let quoteString = String(quote)
for attribute in element.attributes {
let value = Escaping.attribute(attribute.value, quote: quote, asciiOnly: options.asciiOnly)
output += " \(attribute.name.description)=\(quoteString)\(value)\(quoteString)"
}
if element.children.isEmpty, options.selfCloseEmptyElements {
output += "/>"
if formatted {
output += options.lineEnding
}
return
}
output += ">"
// Suppress formatting when the element has text or CDATA children:
// re-indenting mixed content would change its significant whitespace.
// libxml2 makes the same choice in xmlNodeDumpOutputInternal.
let hasInlineContent = element.children.contains { child in
switch child {
case .text, .cdata: true
default: false
}
}
let childFormatted = childFormatting(of: element, parentFormatted: formatted, hasInlineContent: hasInlineContent)
if childFormatted {
output += options.lineEnding
}
stack.append(.close(SerializeClose(
name: element.name.description,
depth: depth,
formatted: formatted,
indentClose: childFormatted,
)))
push(
element.children,
depth: childFormatted ? depth + 1 : depth,
formatted: childFormatted,
into: &stack,
)
}
/// Whether to format (indent) an element's children: never when it has
/// inline content or none, never under `xml:space="preserve"` (its content
/// is emitted verbatim), and otherwise as the parent decided, with
/// `xml:space="default"` re-enabling formatting if the options pretty-print.
private func childFormatting(of element: PureXML.Model.Element, parentFormatted: Bool, hasInlineContent: Bool) -> Bool {
guard !hasInlineContent, !element.children.isEmpty else { return false }
switch Self.xmlSpace(of: element) {
case "preserve": return false
case "default": return options.prettyPrint
default: return parentFormatted
}
}
private static func xmlSpace(of element: PureXML.Model.Element) -> String? {
element.attributes.first { $0.name.localName == "space" && $0.name.prefix == "xml" }?.value
}
/// Pushes children in reverse so they pop off the work stack in order.
private func push(
_ children: [PureXML.Model.Node],
depth: Int,
formatted: Bool,
into stack: inout [SerializeStep],
) {
for child in children.reversed() {
stack.append(.node(child, depth, formatted))
}
}
private func pad(_ depth: Int) -> String {
String(repeating: options.indent, count: depth)
}
}
}