Repository navigation
⚡ perf(encoding): scan encoded output in SSE2 blocks - #2347
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Encoding with
meta_charsetchecks 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.
latin-1,meta_charset=Trueiso-8859-2,meta_charset=Trueutf-8,meta_charset=Truelatin-1, nometa_charsetserialize,meta_charset=TrueThe 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.