Repository navigation
perf(tokenizer): append attribute values and character runs as slices of the input - #1897
Merged
Merged
Conversation
There was a problem hiding this comment.
🔵 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
force-pushed
the
perf/append-input-runs
branch
2 times, most recently
from
October 24, 2026 22:28
699df96 to
67c44b0
Compare
… 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
force-pushed
the
perf/append-input-runs
branch
from
October 24, 2026 23:13
67c44b0 to
03228ef
Compare
43081j
approved these changes
Oct 26, 2026
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]>
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.
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:
mastertest/data/huge-page)ParserStreamin 64 KB chunksSAXParserThe 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)