diff --git a/CHANGELOG.md b/CHANGELOG.md index bba8dc8..c1bff24 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Parser throughput on plain-ASCII documents roughly halved against the libxml2 reference (measured `parse` 20.1x -> 9.5x slower than libxml2 on the 20k-item generated corpus, `bash scripts/benchmark.sh 20000 5`; ~53% wall-clock reduction). The work was profile-driven (macOS `sample`, not guesswork): the scanner, not tree construction, dominated parse time (SAX is ~84% of parse), and within it the cost was per-`Character` `String` machinery (`String.Iterator.next`, `_stringCompareWithSmolCheck`, `_uncheckedFromUTF8`, bridge-object ARC). The root cause was that a single buffered lookahead `Character` defeated every existing byte fast path (each requires an empty buffer), and the content/markup/attribute dispatch all peeked a `Character` before scanning. The fix keeps the reader on the byte path: a non-buffering `peekByte` for dispatch, a byte-mode `skipSpace`, an all-ASCII `takeASCIIName`, an `attributeRunBytes` bulk scanner (with the 3.3.3 whitespace-to-space transform, deferring any carriage return to the Character path so CRLF is not double-counted), and a `sawAmpersand` flag so ampersand-free text skips reference decoding entirely. The single highest-leverage change was detecting the closing `<` of a text run via `peekByte` instead of buffering it, which had been cascading the next element's whole dispatch and name scan onto the slow path. The Character path remains the exact fallback for a streaming source, spliced entity text, and any non-ASCII byte; byte and Character paths are kept from diverging by `scanContentMarkup` (shared dispatch) and by `ByteFastPathTests`, which pins the fast-path/fallback boundaries (non-ASCII names, CRLF and character references in attribute values, ampersand-free vs entity-bearing text, over-long names, raw `<` in attributes). Verified against the full local suite (1668 tests) plus the Apache Xalan XSLT gold-output corpus, the W3C XML conformance corpora (xmltest, OASIS, Sun, Eduni, IBM, Japanese), and the RELAX NG spec suite, all green; no spec behavior changed. +- Element-content markup dispatch now branches on the single byte after `<` (end tag, processing instruction, declaration, or start tag) instead of running up to five literal string comparisons per element before the start-tag branch wins, a per-element cost the profile indicted (`Reader.matchesBytes`). The Character path keeps the equivalent `matches()`-based dispatch for a streaming source or spliced entity text, and a ` ~9.0x slower than libxml2 on the 20k-item corpus; the byte and Character paths are held identical by the differential oracle (`ParserPathDifferentialTests`). No spec behavior changed. + ## [0.3.0] - 2026-06-24 ### Added diff --git a/Sources/Parsing/EventReaderContent.swift b/Sources/Parsing/EventReaderContent.swift index fd3b6ce..ab33ab9 100644 --- a/Sources/Parsing/EventReaderContent.swift +++ b/Sources/Parsing/EventReaderContent.swift @@ -4,25 +4,64 @@ extension PureXML.Parsing.EventReader { /// At a '<' in element content, dispatches to the matching markup scanner. /// The caller has confirmed the lead is '<' and set the event start. Shared /// by the byte and Character dispatch paths so the two cannot diverge. + /// + /// On the byte fast path the single byte after '<' selects the branch ('/' + /// end tag, '?' PI, '!' a declaration, otherwise a start tag), so an + /// element no longer runs up to five literal string comparisons before the + /// start-tag branch wins. The Character path below is the exact fallback for + /// a streaming source or spliced entity text, and a ' Event { + switch reader.peekByte(1) { + case 0x2F: // '/' + try scanEndTag() + case 0x3F: // '?' + try scanContentProcessingInstruction() + case 0x21: // '!' + try scanContentDeclaration() + case .some: + try scanStartTag() + case nil: + try scanContentMarkupViaCharacters() + } + } + + /// The Character-path markup dispatch, used off the byte fast path (a + /// streaming source or spliced entity text). Equivalent to the byte switch + /// in `scanContentMarkup`. + private mutating func scanContentMarkupViaCharacters() throws -> Event { if reader.matches(" Event { if reader.matches("