Skip to content

🐛 fix(tokenizer): stop buffering charref runs - #2294

Merged
gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:fix/charref-buffer-bound-1828
Nov 16, 2026
Merged

gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:fix/charref-buffer-bound-1828

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

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 &#x prefix 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:

  • tokenize entity-heavy: 453,391 → 457,926 (+1.00%).
  • tokenize typical markup: 733,691 → 735,339 (+0.22%).
  • tokenize text-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.

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
@gaborbernat gaborbernat added the bug Something isn't working label Nov 16, 2026
@gaborbernat
gaborbernat merged commit 493cc88 into tox-dev:main Nov 16, 2026
1 of 48 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unfinished character references keep their run in the streaming buffer

1 participant