Repository navigation
🐛 fix(detect): keep Hebrew tiebreak in sync - #1637
Conversation
2b836d3 to
5af4a7f
Compare
Merging this PR will improve performance by 9.31%1 warning
Performance Changes
Tip Curious why performance improved? Comment Comparing Footnotes
|
The logical Hebrew candidate still needs punctuation for the visual tiebreak after disqualification. It no longer needs scoring, and a disqualified visual candidate cannot use the punctuation. Avoiding both paths on large inputs restores the encoding chunks benchmark.
The benchmark refresh in #2503 showed `encoding-detection` on windows-1251 russian flipping from 1.99x faster than resiliparse to 0.71x. Instruction counts confirm a real regression since the September tables: `th_sb_feed` went from 4.54M to 5.04M per call. Three per-byte tests had been added to the single-byte walk that runs once per candidate, 20 times per feed: - the run skip from #2470 checked `byte < 0x80 && buf[index - 1] < 0x80 && cand->alive && index >= scanned_to` on every byte; - the Hebrew tiebreak (#1637) checked `cand->alive` on every byte so a dead logical candidate could keep counting punctuation; - the loop had grown enough that the compiler stopped hoisting the `cand->kind` tests out of it. The walk now gets one copy per candidate kind with the kind constant, the same width-specialization pattern the tokenizer uses. A dead logical candidate hands the rest of the bytes to a small punctuation-only loop. The start of the next skippable ASCII run is located with a strided two-word SWAR probe, and the per-byte loop runs up to it, so it tests only its own bound. The run is measured only once a live candidate reaches it, so a candidate that dies on the way scans nothing past that point. A feed without any 16-byte ASCII stretch never looks for skips. Scores are unchanged: the skipped runs are the same ones as before. Callgrind, turbohtml's own instructions per `detect` call: | case | September | main | this PR | vs main | |---|---|---|---|---| | ascii (1 kB) | 6,706 | 6,389 | 6,389 | 0.0% | | utf-8 russian (4 kB) | 49,871 | 49,578 | 49,578 | 0.0% | | windows-1251 russian (4 kB) | 5,991,505 | 6,490,091 | 5,571,071 | -14.2% | | windows-1252 french (4 kB) | 5,503,337 | 5,984,168 | 4,713,345 | -21.2% | | shift_jis japanese (4 kB) | 2,751,102 | 2,863,540 | 2,656,394 | -7.2% | | utf-8 page (95 kB) | 3,278 | 3,404 | 3,404 | 0.0% | Streaming detection, `encoding-chunks`: | case | main | this PR | vs main | |---|---|---|---| | Japanese prefix, 64 KiB ASCII suffix, 4 KiB chunks | 356,767 | 353,044 | -1.0% | | Japanese prefix, 2,088-byte ASCII suffix, 16-byte chunks | 645,751 | 585,087 | -9.4% | | Japanese prefix, 64 KiB ASCII suffix, one feed | 310,585 | 307,966 | -0.8% | | 64 KiB ASCII, 4 KiB chunks | 922,337 | 922,316 | 0.0% | A new test feeds a stream whose second chunk opens on a long ASCII run, the only way the candidates start on one, and checks it matches a one-shot `detect`.
A fuzz testcase found byte detection choosing different encodings for the same input when it is fed in one piece or two. Applications using streaming detection could decode those bytes differently from applications using one-shot detection.
The single-byte detector now continues the logical Hebrew punctuation tiebreak after that candidate is disqualified, matching the path taken across chunk boundaries.