Repository navigation
🐛 fix(tokenizer): stop buffering charref runs - #2294
Merged
gaborbernat merged 1 commit intoNov 16, 2026
Merged
Conversation
A character reference still open at the end of a streamed chunk kept every character it had read in the input buffer until the reference ended. A numeric reference kept each digit, which is never output, and a named reference kept each alphanumeric past the longest entity name, which is output as text. The digits of a numeric reference leave the buffer as they are read. A named reference past its longest name emits its head when it suspends, then emits the rest of its name characters as text. That text is flushed once it passes 4096 code points, and a ';' that ends the run is reported when it arrives. Split and captured modes keep buffering, because their reference token carries the source text. Fixes tox-dev#2283
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.
Streamed input kept every unfinished character reference in the tokenizer's input buffer until the reference ended, so the buffer grew with the length of the reference (the suspension and the named-reference suspension). Once this merges, the buffer holds only what a reference still needs, so in text and RCDATA it no longer tracks the length of a reference that never ends. Fixes #2283.
Numeric digits leave the buffer
A numeric reference outputs one character, so its digits are never needed once read. A suspended numeric reference now keeps only its
&#or&#xprefix in the buffer, and the number of digits it dropped is added to the column when the reference resolves.Named runs past the longest name are emitted as text
Past the longest entity name, the table lookup has already settled the reference's outcome. Its head is emitted when it suspends, and the name characters after it are output as text as they arrive. Text is flushed once it passes 4096 code points, so the buffer holds one feed plus that limit. A
;that ends the run still reports the same parse error, at the same position, as one-shot parsing.Behavior changes
Token boundaries in streaming mode can move: a long named run may arrive as several text tokens. The tree builder joins adjacent text, so the tree, the serialized output and the parse errors are unchanged. Split mode (
resolve_references=False) and capture modes keep buffering, because their reference token carries the source text. Attribute values are not bounded: the value is part of the tag token the caller receives, so it has to be held in memory, and it grows the same way on main without any reference. The reference buffer itself is bounded in attribute values too, which is this defect.Performance
Callgrind on release LTO builds, 20 and 220 repetitions, instructions per call, against upstream main
7e6d0f9f7:tokenizeentity-heavy: 453,391 → 457,926 (+1.00%).tokenizetypical markup: 733,691 → 735,339 (+0.22%).tokenizetext-heavy prose (no references): 12,795 → 12,764 (−0.24%).The added cost is on the reference path. I did not isolate it to a single line, but the rows that contain references are the ones that move, and the control without references does not. The likely source is
consume_charref, which now checks for a deferred tail on every reference. Two attempts to reduce it did not help: moving the deferred-tail code out of line (+1.10%), and restoring the original return structure with the tail handled per branch (+1.18%). The committed version is the one measured above.Reported by Anthropic OSS Scanner: ANT-2026-K9692DF8.