Repository navigation
parser: slice input text instead of re-validating UTF-8 - #75
Merged
Merged
Conversation
ParserInput is always built from a &str, so its bytes are already valid UTF-8, yet names, attribute values, character data, comments, CDATA and PIs were each converted back with std::str::from_utf8, which scans every byte again. Keep the &str alongside the bytes and slice it instead: str::get only checks that the two offsets fall on character boundaries. The HTML parser's from_utf8_lossy calls take the same route, falling back to a lossy conversion only if a bound splits a character, so its output is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jonwiggins
force-pushed
the
feature/parser-utf8-slices
branch
from
November 20, 2026 21:46
7e16408 to
ee83eef
Compare
Owner
|
Thanks, merged. I rebased onto current main to pick up the clippy 1.100 fixes. I also checked that every |
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.
Summary
ParserInputis always built from a&str, so its bytes are known to be valid UTF-8. Despite that, the XML parser turned every name, attribute value, run of character data, comment, CDATA section and PI back into a&strwithstd::str::from_utf8, which scans every byte a second time. The HTML parser did the same withString::from_utf8_lossy.This PR keeps the original
&strinParserInputand slices it withstr::get(start..end), which only checks that the two offsets fall on character boundaries:ParserInput::str_slice(start, end) -> Option<&str>replaces eachfrom_utf8(&input[a..b]). The existing "invalid UTF-8" errors are kept for a split character, which can't happen when the offsets come from the parser.peek_char's multi-byte path decodes from the&stras well.input_texthelper that falls back tofrom_utf8_lossyif a slice ever splits a character, so its output is byte-for-byte unchanged.The HTML5 parser has its own input handling and isn't touched.
Results
Change in time per iteration against
main(negative is faster), measured two ways on an Apple M4 (details below):parser_benchfixturecargo bench(round 1, round 2)parse_smallparse_mediumparse_largeparse_deeply_nestedparse_many_attributesparse_namespace_heavyparse_htmlsax_parsereader_parsepush_parserroundtripvalidate_dtdparse_html5serialize_largexpath_simpleHow these were measured:
cargo bench:cargo bench --bench parser_benchwith criterion's defaults. For each benchmark,main, this branch andmainagain ran back to back, in two rounds with the order changed. Each figure compares one round's run of this branch with the mean of that round's twomainruns. Those twomainruns agreed within 3%, except onparse_small(+13.8% in round 2) andpush_parser(+6.0% in round 2).main, so I also copied the fixtures verbatim into a small harness that records the fastest of 25 batches of about 10 ms.mainand this branch alternated over 15 cycles, and each figure is the median of the per-cycle ratios, with the interquartile range in brackets. Rows marked † come from a separate 30-cycle run of just those benchmarks.serialize_largenever reaches the parser's timed path, yet it came out 1–4% slower. I can't attribute that to anything but code layout differing between the two builds, which is also the likeliest reason the two methods disagree onparse_mediumandparse_html. Differences of a few percent between builds shouldn't be read as real.Type of Change
Test Plan
New tests:
test_str_slice_respects_char_boundaries: a slice that splits a multi-byte character returnsNone.test_peek_char_multibyte: multi-byte characters are peeked correctly.test_parse_non_ascii_in_every_construct: non-ASCII text in each construct that now goes throughstr_slice(DOCTYPE name and internal subset, element and attribute names, attribute values with entity references, comments, CDATA, PIs, and character data including a 4-byte character) parses to the expected strings.test_parse_non_ascii_text_and_attribute_values: the same for the HTML parser.Each of the CI jobs was run locally on macOS (aarch64):
cargo fmt --all -- --check: passescargo test --all-featureson 1.100.0: passes, including the W3C, libxml2-compat and html5lib suitescargo test --features cli,ffion 1.81.0: passesRUSTDOCFLAGS="-D warnings" cargo doc --all-features --no-deps: passescargo clippy --all-targets --all-features -- -D warnings: clean apart from theassert_is_empty/needless_boolerrors already onmain, which fix: resolve clippy assert_is_empty and needless_bool warnings #74 fixesChecklist
cargo fmt --all)cargo clippy --all-targets --all-features -- -D warnings), once fix: resolve clippy assert_is_empty and needless_bool warnings #74 is mergedcargo test --all-features)ParserInputis crate-internal)<module>: <summary>convention🤖 Generated with Claude Code