Skip to content

perf(tokenizer): append attribute values and character runs as slices of the input - #1897

Merged
fb55 merged 3 commits into
masterfrom
perf/append-input-runs
Oct 26, 2026
Merged

fb55 merged 3 commits into
masterfrom
perf/append-input-runs

Conversation

@fb55

@fb55 fb55 commented Oct 24, 2026 •

Copy link
Copy Markdown
Collaborator

Supersedes #1870.

Implements @43081j's proposal in #1870 (comment).

Attribute values and character tokens are currently built one code point at a time (value += ch). Every += leaves a rope node behind, so a 4M-character attribute value holds about 122 MB while it is being built.

Now a code point that is in the input as is only extends a run [start, end) of the preprocessor's buffer. The run is appended as one slice when something else is appended (a CR read as LF, a NUL replacement, a decoded character reference) or when the value or token is complete. Lookahead text (<, </, -, ]) continues the run. No thresholds, piece arrays or joins. One run is pending at a time: the character token's text until another token starts, then that token's attribute value or comment data. A run never spans a chunk drop: drops happen after the pending character token is emitted and never inside a tag.

The second commit copies runs of 13 or more characters out of the input buffer (_inputText). V8 shares the buffer for slices of that length, so plain slices would keep the whole page alive for as long as an attribute value or text node lives; shorter slices are copies already. The third commit does the same for comment data.

CPU time of the parsing process, median of five processes (the last four rows: seven); #1870 rebased onto master:

master #1870 this PR
4M-character attribute value, heap held after parse 122 MB 3.9 MB 3.9 MB
4M-character comment, heap held after parse 126 MB 126 MB 3.9 MB
4M-character attribute value, parse time 484 ms 135 ms 64 ms
40 pages of 77 KB 96 ms 104 ms 103 ms
900 KB of prose in one element 135 ms 147 ms 137 ms
200 pages, one 24-character attribute kept from each, heap held 0.3 MB 0.3 MB 0.2 MB
908 KB real page (test/data/huge-page) 104 ms 110 ms 79 ms
the same page through ParserStream in 64 KB chunks 105 ms 105 ms 72 ms
the same page through SAXParser 58 ms 61 ms 38 ms
3,461 html5lib tree-construction inputs 36 ms 45 ms 42 ms

The html5lib inputs are 20 to 100 characters each; their extra cost is per token boundary, 5% once a process is warm, and #1870 pays it too. Four real pages (an article, the jQuery docs, the WHATWG parsing section, a wiki page) parse 15% faster than on master.

🤖 Generated with Claude Code (Fable 5.1)

Copilot AI balanced review requested due to automatic review settings October 24, 2026 17:50
@coveralls

coveralls commented Oct 24, 2026 •

Copy link
Copy Markdown

Coverage Status

Coverage is 95.626% — perf/append-input-runs into master. No base build found for master.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

No confirmed defects remain, but cross-state buffering, streaming boundaries, and V8-specific memory behavior warrant final human validation.

No open issues 🎉

What changed in this PR

This PR reduces tokenizer allocation overhead by accumulating unchanged attribute values, character runs, and comment data as input slices rather than appending each code point.

Changes:

  • Adds slice-based accumulation with flushing around transformations and token emission.
  • Detaches longer slices to avoid retaining the original input buffer in V8.
  • Adds regression coverage for streaming, buffer drops, surrogate pairs, and transformed input.
File Description
packages/​parse5/​lib/​tokenizer/​preprocessor.ts Exposes the current code point’s starting buffer index.
packages/​parse5/​lib/​tokenizer/​index.ts Implements slice accumulation and detached strings for attributes, text, and comments.
packages/​parse5/​lib/​tokenizer/​index.test.ts Tests long runs, lookahead, transformations, and chunked input.

Review effort: Balanced

@fb55
fb55 force-pushed the perf/append-input-runs branch 2 times, most recently from 699df96 to 67c44b0 Compare October 24, 2026 22:28
fb55 and others added 3 commits October 25, 2026 00:10
… of the input

Attribute values and character tokens were built from the input one
code point at a time (`value += ch`, `chars += ch`). In V8 every `+=`
leaves a rope node behind until the string is read, so a long run (an
inline `data:` image, a large inline script) held tens of bytes per
character while it was being built: about 122 MB for a 4M-character
attribute value.

Instead, a code point that is in the input as is only extends a run,
`[start, end)` of the preprocessor's buffer. The run is appended as one
slice when something else has to be appended (a CR read as LF, a NUL
replacement, a decoded character reference) or when the value or token
is complete. Text emitted after looking ahead (`<`, `</`, `<!`, `-`,
`]`) continues the run as well, since it is identical to the input. One
run is pending at a time: the character token's text until another token
is created, then that token's attribute value.

The parsed chunk is only dropped once the pending character token has
been emitted and never inside a tag, so a run never spans a drop; after
a drop the run is empty and the next code point starts a new one.

Co-authored-by: James Garbutt <[email protected]>
Co-authored-by: Kam <[email protected]>
Co-Authored-By: Claude Fable 5.1 <[email protected]>
A slice of 13 or more characters shares the input's buffer in V8, and
other engines keep substrings dependent on their source as well. Values
and text cut from the input would therefore keep the whole buffer alive
for as long as they are: parsing 200 distinct 77 KB pages and keeping
one 24-character attribute value from each held 15.6 MB. Concatenating a
run and slicing the result flattens it into a buffer of its own, so the
tokens no longer refer to the input once they are emitted.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Comment data was still appended one code point at a time, so a long
comment (a commented-out script, a data block in a bogus comment) held
tens of bytes per character while it was being built: 126 MB for a
4M-character comment. The same two indices now track the input run of
the current comment; the text appended after looking ahead (`-`, `--`,
`--!`, `<`, `!`) continues the run as it does for character tokens, and
the run is appended when the comment is emitted.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@fb55
fb55 merged commit e32b1b0 into master Oct 26, 2026
11 checks passed
fb55 added a commit that referenced this pull request Nov 7, 2026
At EOF inside a tag the tag token is dropped, but the pending character token got the EOF position as its end, so its source range covered the unfinished tag. The EOF path ends pending characters at the open token's start when there is one. An inappropriate end tag dropped in RCDATA or RAWTEXT already clears `currentToken` (#1897), so it cannot serve as that boundary.

The location tests check the text range before an unfinished tag at EOF.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants