Skip to content

⚡ perf(encoding): scan encoded output in SSE2 blocks - #2347

Merged
gaborbernat merged 2 commits into
tox-dev:mainfrom
gaborbernat:perf/encoding-readback
Nov 16, 2026
Merged

gaborbernat merged 2 commits into
tox-dev:mainfrom
gaborbernat:perf/encoding-readback

Conversation

@gaborbernat

Copy link
Copy Markdown
Member

Encoding with meta_charset checks that the output reads back as the declared label, and that check roughly doubled the cost of Latin-1 output after #2282. The check now looks only at the bytes the codec can write differently under the declared label. A scan of those bytes runs in SSE2 blocks, and the full read-back runs only when a byte could misread. Output that cannot misread, including every UTF-8 and ISO-8859-2 document, skips the read-back as before, with a single lookup. Fixes #2336.

Performance

Callgrind instructions per call against the parent of #2282 (20fffbe), measured once on each side. The benchmark document is 400 paragraphs of mixed ASCII and Latin-1 text, about 18 KB of output.

Encode Parent This PR Delta
latin-1, meta_charset=True 373,340 379,470 +1.65%
iso-8859-2, meta_charset=True 373,301 373,613 +0.08% (cache lookup)
utf-8, meta_charset=True 373,127 373,151 +0.00%
latin-1, no meta_charset 367,315 367,335 +0.01%
serialize, meta_charset=True 372,757 372,773 +0.00%

The remaining cost on Latin-1 output is the byte scan itself, about 6,000 instructions per 18 KB, roughly 0.3 per byte. The earlier version decoded the whole output, which cost about 146,000 instructions per call, and encoded the output twice, which the move into the encode path removed. Two alternatives were measured and left out: a scalar loop, which vectorized poorly in the release build at about 2.5 instructions per byte, and a 32-byte block with two compares per block, which cost about 0.5 per byte.

The read-back added by the encoding fix decoded the whole output and
compared it with the text on every encode under meta_charset. For a
single-byte label, a byte the codec writes decodes the same under the
label unless the byte falls in the range where the two tables disagree.
The disagreeing range is computed once per codec name, and the decode
runs only when an output byte falls inside it.

The encoded output is now checked once, in node_encode, instead of
being encoded a second time.
The byte range check that decides whether a meta_charset read-back is
needed ran as one scalar-shaped loop over the encoded output, costing
about 2.5 instructions per byte. The range test now runs in 64-byte SSE2
blocks with a scalar tail, and the scan sits in its own function so the
encode path keeps its shape. Each lane keeps the least saturated offset
from the range start, so one compare ends the loop.
@gaborbernat gaborbernat added the enhancement New feature or request label Nov 16, 2026
@gaborbernat
gaborbernat merged commit 61390c9 into tox-dev:main Nov 16, 2026
1 of 48 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.

encode() under meta_charset roughly doubles the cost of Latin-1 output

1 participant