Repository navigation
🐛 fix(encoding): skip ASCII prefix like chardetng - #2473
Conversation
9d3270e to
47d1f35
Compare
Merging this PR will improve performance by ×4.12 warnings
Performance Changes
Tip Curious why performance improved? Comment Comparing Footnotes
|
7758d5a to
02c8fa3
Compare
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.
02c8fa3 to
ad40f9f
Compare
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.
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
EncodingDetectorequal todetect()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 athresholdthis can turn aNoneresult 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.