Skip to content

🐛 fix(encoding): skip ASCII prefix like chardetng - #2473

Merged
gaborbernat merged 4 commits into
tox-dev:mainfrom
gaborbernat:fix/detect-ascii-prefix
Nov 18, 2026
Merged

gaborbernat merged 4 commits into
tox-dev:mainfrom
gaborbernat:fix/detect-ascii-prefix

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Encoding detection scored the whole ASCII run before the first non-ASCII (or ESC) byte, while chardetng starts its candidates two bytes before that byte. The extra context changed single-byte scores, most visibly the ordinal bonus for 0xAA/0xBA: b"a1111\xaa " detected as ISO-8859-4 where chardetng says windows-1252.

Detection now starts the single-byte and CJK candidates two bytes before the first non-ASCII or ESC byte, in one-shot and streamed detection alike. The stream caps the saved context so a first event at chunk offset 1 sees the same two bytes as one-shot, which keeps EncodingDetector equal to detect() of the concatenated bytes at every chunk size.

Over 75,903 non-ASCII inputs without a BOM, main disagreed with the chardetng oracle on 125 and this branch on none; exactly those 125 answers change and no answer that matched chardetng moves. Confidence changes on 183 further inputs, because the winner's raw score now equals chardetng's find_score; with a threshold this can turn a None result into an encoding. Seven minimized inputs are pinned one-shot and at six chunk sizes (all fail on main), and a 20,000-input seeded corpus runs against the oracle in the conformance job.

Skipping the prefix also makes detection of long ASCII-prefixed input cheaper: 64 KiB ASCII in 4 KiB chunks drops from 105.5M to 0.92M instructions under callgrind.

@gaborbernat gaborbernat added the bug Something isn't working label Nov 18, 2026
@gaborbernat
gaborbernat force-pushed the fix/detect-ascii-prefix branch from 9d3270e to 47d1f35 Compare November 18, 2026 17:35
@codspeed

codspeed Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Merging this PR will improve performance by ×4.1

2 warnings

⚠️ 5 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚠️ 5 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

⚡ 5 improved benchmarks
✅ 575 untouched benchmarks
⏩ 91 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_feature[encoding-chunks-ascii] 75,571.8 µs 523.4 µs ×140
⚡ test_feature[encoding-result-stream-ascii] 822.6 µs 317.8 µs ×2.6
⚡ test_feature[node-equals] 536.9 µs 251.5 µs ×2.1
⚡ test_feature[node-equals-reversed] 531.6 µs 415.2 µs +28.04%
⚡ test_feature[xpath-distinct-unique] 988.7 µs 918.3 µs +7.67%

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/detect-ascii-prefix (ad40f9f) with main (a67525d)2

Open in CodSpeed

Footnotes

  1. 91 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. ↩

  2. No successful run was found on main (e118657) during the generation of this report, so a67525d was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@gaborbernat
gaborbernat force-pushed the fix/detect-ascii-prefix branch 2 times, most recently from 7758d5a to 02c8fa3 Compare November 18, 2026 22:45
gaborbernat and others added 4 commits November 18, 2026 14:47
Detection scored an ASCII run that comes before the first non-ASCII byte,
and that run changed the candidates' state at the first non-ASCII byte.
chardetng's feed does not score the run: it starts the candidates two bytes
before the first non-ASCII or ESC byte. The one-shot path and the stream
now start the same way, so input that opens with an ASCII run can detect
differently than before. Example: b"a1111\xaa " detected as ISO-8859-4, and
chardetng answers windows-1252. Input whose first byte is non-ASCII is
unchanged.

Checked against the chardetng detector oracle on 88,000 mixed inputs, of
which 75,903 have non-ASCII bytes and no byte-order mark. upstream/main
disagreed with chardetng on 125 of them. This branch disagrees on none,
one-shot, and streamed at chunk sizes 1, 2, 3, 5, 16 and 4096. All 125
encodings change, and no answer that matched chardetng changes. Confidence
also changes on 183 more inputs whose encoding stays the same; the oracle
reports only encodings, so those values are not checked against chardetng.
On a seeded corpus of 20,000 ASCII-prefixed inputs, upstream/main disagreed
on 11 one-shot and 33 streamed answers; this branch disagrees on none.

Pinned regression cases in tests/encoding/test_encoding_ascii_prefix.py
cover minimized disagreements, one-shot and streamed. The seeded corpus is
in tests/conformance, where the oracle runs.

Callgrind instructions per call, upstream/main to this branch:

- encoding-chunks, 64 KiB ASCII, 4 KiB chunks: 105.5 M to 0.54 M
The first-event start made the candidate feed a call with four sites, so
the compiler outlined it and every chunk paid for the call. The feed now
checks started and calls the candidates once, the start lives in its own
helper, the first event is a byte loop, and the zeroed started and
tail_len fields need no init stores.
@gaborbernat
gaborbernat force-pushed the fix/detect-ascii-prefix branch from 02c8fa3 to ad40f9f Compare November 18, 2026 23:05
@gaborbernat
gaborbernat merged commit 222d46f into tox-dev:main Nov 18, 2026
43 of 56 checks passed
gaborbernat added a commit that referenced this pull request Nov 18, 2026
Once encoding detection has seen a non-ASCII byte, every candidate
decoder still walked each following ASCII byte one at a time, so long
ASCII stretches dominated the cost of detecting mostly-ASCII pages.

Single-byte candidates now jump from the second byte of an ASCII run to
its last space and reset their state as that space would; CJK candidates
jump to the run's last byte when their decoder is idle. No ASCII byte
can disqualify a single-byte candidate and CJK decoders always sit on a
character boundary at the jump, so scores are unchanged.

Callgrind, instructions per call on current main: Japanese prefix with
64 KiB ASCII suffix 25.5M → 0.36M; 16-byte chunks 0.95M → 0.65M; 64 KiB
ASCII in 4 KiB chunks 0.92M → 0.92M, since main's ASCII-prefix skip
(#2473) already covers that input. Short legacy-byte detections stay
within 0.5% of main.
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