Skip to content

perf(filters): vectorize the 8 x 8 islow inverse DCT (#474) - #495

Merged
michaelstonis merged 6 commits into
spec/33-broadside-v1from
474-simd-idct
Nov 11, 2026
Merged

michaelstonis merged 6 commits into
spec/33-broadside-v1from
474-simd-idct

Conversation

@michaelstonis

@michaelstonis michaelstonis commented Nov 11, 2026 •

Copy link
Copy Markdown
Owner

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% of catalogue.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.Transform is now a dispatcher. TransformScalar is the existing port and the fallback. 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, because a row holds the same coefficient of four columns.
  • Its results are transposed: four 4 × 4 transposes built from constant byte shuffles and ConditionalSelect.
  • Pass 2's outputs hold one sample of four rows per vector. Multiplies pack them four to a 32-bit lane, which transposes them back into rows, and each row is written as one 8-byte store.
  • The range-limit table becomes arithmetic: sign-extend the low 10 bits, add 128, clamp.

The reduced (4 × 4, 2 × 2, 1 × 1) and 12-bit transforms are unchanged. The profiles never reach them.

  • Left shifts are multiplies. The first version used the shift operators everywhere (86 per block) and was 0.74x of scalar in the browser AOT build. Timed alone there, a 32-bit vector shift costs 0.87–1.05 ns, against about 0.4 ns for the other operations used. Every << n became * 2^n, which gives the same bits, wrapping included, and the range limit's >> 18, << 22, >> 22 became * 16, >> 22. That leaves 32 arithmetic right shifts per block, and the kernel is 1.34x there.
  • Shortcuts. libjpeg's column shortcut (dc << 2 for a column with zero AC coefficients) differs from the full arithmetic where dc << 13 wraps, 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 (SimdKernel trait, so CI reruns it with DOTNET_EnableHWIntrinsic=0; it also passes that way locally) compares the vector path and the dispatcher with the scalar port on:

  • every block with one non-zero coefficient (each of the 64 positions at every value from -2,048 to 2,047, alone and beside a DC term)
  • 50,000 sparse, photo-like blocks under two tables
  • 20,000 blocks of random 16-bit coefficients under random quantization up to 65,535, a third of them with seven DC-only columns whose arithmetic wraps
  • a block written into a wider stride, which leaves the bytes between rows alone

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):

Build Scalar Vector Speed-up
JIT 71.48 33.60 2.13x
Native AOT 67.96 33.10 2.05x
Browser interpreter, local (Node 22) 623.3 412.2 1.51x
Browser AOT, local (Node 22) 79.8 59.7 1.34x

CI blazor-wasm lines:

kernel aot idct-8x8: scalar 162.505 ns/item, vector 107.574 ns/item, speed-up 1.51x, equal yes, accelerated true
kernel interpreter idct-8x8: scalar 769.043 ns/item, vector 413.513 ns/item, speed-up 1.86x, equal yes, accelerated true

BenchmarkDotNet InverseDctBenchmarks (1,024 blocks, --job short): TransformScalar 71.89 µs, Transform 36.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:

Build Base (ms) Head (ms) Change
JIT 883.2 830.8 -6%
Native AOT 911.0 859.8 -6%
Browser AOT 1,777.2 1,738.3 -2%
Browser interpreter 28,667.3 28,160.6 -2%

New gate entries: InverseDctBenchmarks.Transform and TransformScalar at 0 B. The idct-8x8 case is in KernelBenchmarks, and the kernel sources are linked in the Blazor sample. The 7.4.8 conformance row and docs/benchmarks/simd-kernels.md are 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-latest runner. Best-of-three times, in ms:

Runs 4:4:4 managed WIC Ratio 4:2:0 managed WIC Ratio
Before (8 runs) 85.0–108.6 44.9–55.4 1.86–2.00x 71.5–92.2 39.9–60.7 1.49–1.79x
This PR 63.2–77.2 54.8–94.2 0.82x, 1.22x, 0.82x 63.2–77.4 43.1–58.6 1.12x, 1.47x, 1.47x

WIC_decodes_the_8_megapixel_photograph_faster_than_the_managed_decoder is 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. WicImageDecoder still 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.

- 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>
@michaelstonis
michaelstonis merged commit 9c2977f into spec/33-broadside-v1 Nov 11, 2026
31 checks passed
@michaelstonis
michaelstonis deleted the 474-simd-idct branch November 11, 2026 09:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant