Skip to content

⚡️ perf(encoding): return settled detections early - #2471

Merged
gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:perf/detect-settled-result
Nov 18, 2026
Merged

gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:perf/detect-settled-result

Conversation

@gaborbernat

Copy link
Copy Markdown
Member

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: detect on 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-feed done flags) matches main.

@gaborbernat gaborbernat added the enhancement New feature or request label Nov 18, 2026
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.
@gaborbernat
gaborbernat force-pushed the perf/detect-settled-result branch from 2ba2c88 to 24720dc Compare November 18, 2026 17:28
@codspeed

codspeed Bot commented Nov 18, 2026

Copy link
Copy Markdown

Merging this PR will regress 7 benchmarks

1 warning

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

⚡ 7 improved benchmarks
❌ 7 regressed benchmarks
✅ 566 untouched benchmarks
⏩ 91 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ test_feature[encoding-chunks-ascii] 69.1 ms 75 ms -7.86%
❌ test_feature[canonicalize] 6 ms 6.5 ms -7.17%
❌ test_mutating_feature[observe-registrations-unrelated] 1.3 ms 1.4 ms -6.46%
❌ test_feature[encode-inner] 6.7 ms 7.2 ms -6.42%
❌ test_feature[encode-inner-indent] 7.1 ms 7.5 ms -6.22%
❌ test_feature[serialize-inner] 5.5 ms 5.8 ms -5.27%
❌ test_feature[serialize-inner-indent] 5.9 ms 6.2 ms -5.05%
⚡ test_feature[encoding-result-stream-bom] 1,047.9 µs 398 µs ×2.6
⚡ test_feature[encoding-result-bom] 961.4 µs 375 µs ×2.6
⚡ test_feature[encoding-result-ascii] 959.8 µs 387.6 µs ×2.5
⚡ test_feature[encoding-result-stream-ascii] 990.2 µs 819.3 µs +20.86%
⚡ test_feature[encoding] 357.9 µs 329.2 µs +8.74%
⚡ test_mutating_feature[observe-registrations] 1.2 ms 1.1 ms +5.42%
⚡ test_feature[transform-text-literal] 5.8 ms 5.6 ms +5.06%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing gaborbernat:perf/detect-settled-result (24720dc) with main (01b4635)

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

@gaborbernat
gaborbernat merged commit 3df8ce1 into tox-dev:main Nov 18, 2026
64 of 65 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant