Repository navigation
perf(filters): vectorize the 8 x 8 islow inverse DCT (#474) - #495
Merged
Merged
Conversation
- InverseDct.TransformVector runs libjpeg's jpeg_idct_islow four columns, then four rows, at a time in 32-bit Vector128 lanes: pass 1 works on the coefficient rows as loaded, its results are transposed with constant byte shuffles and selects, and pass 2's outputs are packed back into rows by multiplies. The range-limit table becomes a sign-extend and clamp. - Bit-exact with the scalar port, which stays as TransformScalar and the fallback: libjpeg's column shortcut is computed and selected per lane (the full arithmetic differs where the DC term wraps); its row shortcut equals the full arithmetic in the 10 bits the range limit keeps. - Left shifts are multiplies: the browser AOT build runs a vector shift at several times their cost. - InverseDctKernelTests (every single-coefficient block, sparse and full-range random blocks, a wider stride), InverseDctBenchmarks with allocation baseline entries at 0, and an idct-8x8 KernelBenchmarks case. Signed-off-by: Michael Stonis <120685+michaelstonis@users.noreply.github.com>
Signed-off-by: Michael Stonis <120685+michaelstonis@users.noreply.github.com>
Signed-off-by: Michael Stonis <120685+michaelstonis@users.noreply.github.com>
…pled chroma With the vector IDCT (#474) the managed decoder matches or beats WIC's pixels on the 8-megapixel 4:4:4 photograph (0.82x in two of three CI runs, 1.9x to 2.0x before). The 4:4:4 case is measured and logged; the default decoder choice is unchanged. Signed-off-by: Michael Stonis <120685+michaelstonis@users.noreply.github.com>
…imd-idct Signed-off-by: Michael Stonis <120685+michaelstonis@users.noreply.github.com> # Conflicts: # docs/benchmarks/simd-kernels.md # samples/Broadside.Viewer.Blazor.Sample/KernelBenchmarks.cs
…red values The eight SkiaFirstTileBenchmarks.FirstTile entries measured 288 B under the baseline in every gate run of this branch (identical values on four attempts, before and after merging #494); set them to those values. Signed-off-by: Michael Stonis <120685+michaelstonis@users.noreply.github.com>
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.
Closes #474.
Profile first
macOS
sample(1 kHz, Native AOT, 96 DPI, every page in a loop) on the integration branch: JPEG decoding is 28.5% ofcatalogue.pdf's render and the 8 × 8 IDCT 9.7%, a third (34%) of the decode. The brochure and newsletter decode no JPEG once their images are cached. After this PR the IDCT is 4.5% of the catalogue's render and 18% of its decode.Approach
InverseDct.Transformis now a dispatcher.TransformScalaris the existing port and the fallback.TransformVectorruns libjpeg'sjpeg_idct_islowfour columns, then four rows, at a time in 32-bitVector128lanes:ConditionalSelect.The reduced (4 × 4, 2 × 2, 1 × 1) and 12-bit transforms are unchanged. The profiles never reach them.
<< nbecame* 2^n, which gives the same bits, wrapping included, and the range limit's>> 18, << 22, >> 22became* 16, >> 22. That leaves 32 arithmetic right shifts per block, and the kernel is 1.34x there.dc << 2for a column with zero AC coefficients) differs from the full arithmetic wheredc << 13wraps, so pass 1 computes both and selects per lane. The row shortcut gives the same 10 bits as the full arithmetic even when it wraps, so pass 2 needs no select. A block with no AC coefficient is one fill, as in the scalar path.Bit-exact
InverseDctKernelTests(SimdKerneltrait, so CI reruns it withDOTNET_EnableHWIntrinsic=0; it also passes that way locally) compares the vector path and the dispatcher with the scalar port on:Mutation checks: changing the range-limit shift fails all four tests, and dropping the pass-1 select fails the wrap test. All 288 DCT tests pass, including byte-exact matches with libjpeg-turbo. Page 0 of every pdf.js corpus file (989 files, 970 render) at 72 DPI hashes the same before and after.
Speed
ns per block, sparse photo-like blocks (
idct-8x8):CI
blazor-wasmlines:BenchmarkDotNet
InverseDctBenchmarks(1,024 blocks,--job short):TransformScalar71.89 µs,Transform36.48 µs,Allocated-on both.DctBenchmarks.Decode, scalar IDCT against vector: testorig 137.3 → 105.7 µs (-23%), sampling-444 -27%, restart-blocks -23%, progressive -15%, arithmetic-progressive -10%, ycck -21%.Whole render,
catalogue.pdf, all pages at 96 DPI, base against head, M4 Max:New gate entries:
InverseDctBenchmarks.TransformandTransformScalarat 0 B. Theidct-8x8case is inKernelBenchmarks, and the kernel sources are linked in the Blazor sample. The 7.4.8 conformance row anddocs/benchmarks/simd-kernels.mdare updated. No public API changes.Managed against WIC at 4:4:4 (owner decision)
The vector IDCT makes the managed decoder match or beat WIC's pixels on the 8-megapixel 4:4:4 photograph on the
windows-latestrunner. Best-of-three times, in ms:WIC_decodes_the_8_megapixel_photograph_faster_than_the_managed_decoderis renamed..._with_subsampled_chroma_.... It still asserts that WIC is faster at 4:2:0, and it now only logs the 4:4:4 ratio.WicImageDecoderstill takes 4:4:4 by default; I did not change that. Owner decision: keep WIC for 4:4:4 on Windows, or leave it to the managed decoder. Note that the runner's WIC times vary more than the managed ones.