Repository navigation
⚡️ perf(encoding): return settled detections early - #2471
Conversation
A byte-order mark in the stream's prefix settles the answer, yet every later feed still fed the scoring candidates, and close() fed them an end-of-stream chunk even when a mark or a <meta> declaration decided the result. Once a mark is in the prefix, feed() now returns before the candidates see the chunk, and close() feeds the end of stream only when no declaration decided the answer. The Python side rebuilt an EncodingMatch from the ranker on every call. A settled answer is fixed by its winner, certainty and mark flag, so default-option detectors now share one match per such triple. The cache is keyed on those three values only, so it holds a few hundred entries and never a caller's options object; other options take the old path. The answers do not change. Streams and one-shot calls match upstream/main on the differential fuzz, on a targeted run over mark and <meta> prefixes split at every chunk size, and under three option sets. Callgrind instructions per call, upstream/main to this branch: - encoding-result, ASCII, 62 detections: 1.14 M to 0.38 M - encoding-result, UTF-8 byte-order mark, 64 detections: 1.14 M to 0.38 M - encoding-result-stream, UTF-8 byte-order mark, 24 detections: 1.17 M to 0.42 M - encoding-result-stream, ASCII, 18 detections: 1.19 M to 0.96 M The ASCII stream row keeps its candidate feed here; skipping the ASCII prefix before the candidates is a separate change.
2ba2c88 to
24720dc
Compare
Merging this PR will regress 7 benchmarks1 warning
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing Footnotes
|
A byte order mark or a declared charset settles detection, yet the detector still fed the remaining bytes and the end-of-input step to every candidate, and
detect()built a fresh result object each time.A BOM now returns as soon as it is complete, the end-of-input feed is skipped when a declaration already decided the result, and
detect()with default options reuses one frozen result per (winner, certain, BOM) outcome.Callgrind, instructions per call:
detecton ASCII 1.14M → 0.38M, on a BOM 1.14M → 0.38M; streamed BOM 1.17M → 0.42M, streamed ASCII 1.19M → 0.96M. A differential over 20,400 rows under eight option sets (detect,detect_all,EncodingDetector.close()at five chunk sizes, per-feeddoneflags) matches main.