Repository navigation
🐛 fix(tokenizer): tokenize svg and math content as browsers do - #1988
Conversation
tokenize(), Tokenizer and the html.parser shim switched to raw text after any start tag named title, style, script, xmp, plaintext or another raw-text name, also inside <svg> and <math>. There those are ordinary elements, so the markup after them came out as one text token, and <![CDATA[ came out as a comment instead of text. Code reading tokens missed elements a browser builds. The tokenizer now keeps the stack of open elements with the namespace a browser gives each one, pairing tags as written: breakout tags, integration points (foreignObject, desc, title, annotation-xml with an HTML encoding) and the scope rules for HTML end tags follow the tree builder. Ordinary HTML does not pay for the stack. Until the first svg or math start tag the tokenizer only counts the tags it hands over and keeps the input; that start tag rebuilds the stack by tokenizing the input again up to it. A streaming Tokenizer keeps at most about 64K code points this way, then follows every tag so it can reclaim consumed input. Fixes tox-dev#1714
Merging this PR will improve performance by 7.24%2 warnings
Performance Changes
Tip Curious why performance improved? Comment Comparing Footnotes
|
Formatting from tox -e fix, tests for the uncovered spine paths (long custom names, end-tag attributes, a hash-slot collision), and the fragment context interned through th_tag_lookup so th_tag_intern can go.
The kind check in name_eq never failed in the tests: no font attribute had the length of "face" or "size" with a code point outside Latin-1. The new case covers that branch and checks that the font is not treated as a breakout.
# Conflicts: # src/turbohtml/_c/dom/tree.c # src/turbohtml/_c/tokenizer/rewrite.c
#1988 made the Tokenizer run inline SVG through the full tree-construction rules for every element inside an `<svg>`. That pushed the SVG-heavy tokenize rows past the 5% ceiling against the commit before #1988 (`ed2b89473`). This PR records the children of an SVG element as keys in the spare slots of the element spine and writes them out only when a tag needs the full rules. The token output does not change, and the rows return under the ceiling. ### What changes Inside an SVG element that is not an integration point, a start tag that neither breaks out of foreign content nor opens an integration point is recorded as a bare key. An end tag that names the innermost recorded element pops it. An `<svg>` opened from HTML, and its end tag, take a short path too. Any tag that needs the full rules first writes the recorded elements out, so those rules read the same spine they read before. `th_tag_key` in `src/turbohtml/_c/data/tag_atom.h` no longer tests a readable length. `tools/generate_tags.py` emits that function, so the header is regenerated. The tokenizer reserves 16 bytes for its name buffer before any name is written, so the 8-byte read stays in bounds. ### Why the output does not change Each recorded element gets the namespace, integration-point flag, table flag and name that the full rules would have assigned. The spine is complete before any rule reads it, and a tag that changes the shape of the spine takes the full rules. `tests/tokenizer/test_svg_run.py` pins the token output of upstream `main` for the new paths: entering and leaving SVG, every slow-path tag inside a run, mismatched and long end tags, the depth limit, chunked feeds at every split point, `reset()` during a run, and `capture_attributes=False`. In the coverage build, `tests/tokenizer/` covers every changed C line and branch. ### Performance Callgrind on release LTO builds with `PYTHONHASHSEED=0`. Values are instructions per call: the 220-repetition count minus the 20-repetition count, divided by 200. Base is `ed2b89473`, the base of #1988. Main is `739e421a3`, the commit this branch was built on. Head is this change measured before the rebase onto `56aa6b062`. Upstream has since moved with two commits in the selector and c14n code, and no tokenizer source or header they include changed, so the table keeps the measured base and main. `svg-icons` is a probe of inline icons: one `<svg>` inside a `<span>` for each of 2,500 icons. Its base row comes from the base build, measured with the same child script as the other rows. `escape` is the untouched control row. | Row | Base `ed2b89473` | Main `739e421a3` | Head | Head vs base | Head vs main | | --- | ---: | ---: | ---: | ---: | ---: | | svg-mostly | 11,466,041 | 12,246,500 | 11,931,402 | +4.06% | -2.57% | | svg-tail | 8,602,031 | 9,004,868 | 8,971,907 | +4.30% | -0.37% | | tokenize | 8,599,402 | 9,001,956 | 8,969,130 | +4.30% | -0.36% | | tokenize-attributes | 81,360 | 81,554 | 81,560 | +0.25% | +0.01% | | htmlparser | 7,257,465 | 7,543,308 | 7,530,768 | +3.77% | -0.17% | | tok-chunk8k | 8,651,553 | 9,054,295 | 9,026,943 | +4.34% | -0.30% | | tok-x4 | 34,563,510 | 36,175,509 | 36,065,999 | +4.35% | -0.30% | | parse | 10,350,020 | 10,125,798 | 10,112,447 | -2.30% | -0.13% | | sax | 11,941,800 | 11,714,360 | 11,704,201 | -1.99% | -0.09% | | escape | 269,575 | 269,575 | 269,575 | +0.00% | +0.00% | | svg-icons | 13,047,270 | 14,032,507 | 13,675,010 | +4.81% | -2.55% | Against base, every row is at or below +4.81%. Against main, no row is more than +0.01% above. ### Behavior changes None. The token output is unchanged. The change only affects how fast the Tokenizer follows SVG content.
Fixes #1714
tokenize(),Tokenizerand thehtml.parsershim switched into raw text, RCDATA, script data or PLAINTEXT after any start tag with one of those names, wherever the tag sat. Inside<svg>and<math>the same names are ordinary elements, and a browser keeps reading markup there. Code that reads the token stream, such as a port of anhtml.parsersubclass, got text where a browser builds elements, and a CDATA section arrived as a comment. 🧭 Now the token stream follows the namespace of the open elements, as tree construction does.The
Tokenizerkeeps a stack of the open elements, each with the namespace a browser gives it. It applies the tree-construction rules for foreign content: which start tags break out of<svg>or<math>, which elements are integration points, and which end tags close a foreign element. The stack is capped at the tree builder's nesting limit, and past it the outer half is forgotten, because the innermost elements decide the namespace. The tokenizer now interns each tag name once, and the tree builder and the rewriter read that result.The tokenizer pairs tags as written. Implied end tags, insertion modes and the list of active formatting elements stay with the tree builder, so where a decision needs them, use
parse()orsaxparserather than the token stream.Behavior changes:
<svg>and<math>, raw-text names are elements and their content is markup. A CDATA section there is text, and thehtml.parsershim delivers it tohandle_data.