Skip to content

🐛 fix(detect): keep Hebrew tiebreak in sync - #1637

Merged
gaborbernat merged 2 commits into
tox-dev:mainfrom
gaborbernat:fix/encoding-stream-candidate
Nov 6, 2026
Merged

gaborbernat merged 2 commits into
tox-dev:mainfrom
gaborbernat:fix/encoding-stream-candidate

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

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.

@gaborbernat gaborbernat added the bug Something isn't working label Nov 6, 2026
@gaborbernat
gaborbernat force-pushed the fix/encoding-stream-candidate branch from 2b836d3 to 5af4a7f Compare November 6, 2026 17:22
@codspeed

codspeed Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Merging this PR will improve performance by 9.31%

1 warning

⚠️ 4 benchmarks with a significant change ran in a different software environment

Their base and head results were measured with a different OS or toolchain, such as compiler, runtime or library versions, so the changes might come from this difference rather than from the code. Open an affected benchmark to see what changed.

See affected benchmarks

⚡ 3 improved benchmarks
❌ 1 (👁 1) regressed benchmark
✅ 578 untouched benchmarks
⏩ 32 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_feature[shadow-slot-comments] 110 µs 83.4 µs +31.92%
⚡ test_feature[encoding-chunks] 20.2 ms 18.3 ms +9.97%
⚡ test_feature[select-relative-sibling] 43.6 µs 41.3 µs +5.51%
👁 test_feature[linkify-traversal-wide-callbacks] 2.5 ms 2.7 ms -6.73%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing gaborbernat:fix/encoding-stream-candidate (3505198) with main (e0cb89b)

Open in CodSpeed

Footnotes

  1. 32 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

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.
@gaborbernat
gaborbernat enabled auto-merge (squash) November 6, 2026 18:38
@gaborbernat
gaborbernat merged commit 7c8ea5d into tox-dev:main Nov 6, 2026
62 checks passed
gaborbernat added a commit that referenced this pull request Nov 20, 2026
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`.
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.

1 participant