Skip to content

perf(jpx): tier-1 decoding at 41 ns per sample (#255) - #502

Merged
michaelstonis merged 4 commits into
spec/33-broadside-v1from
255-jpx-tier1-speed
Nov 12, 2026
Merged

michaelstonis merged 4 commits into
spec/33-broadside-v1from
255-jpx-tier1-speed

Conversation

@michaelstonis

Copy link
Copy Markdown
Owner

Closes #255 (the tier-1 part; the vertical wavelet pass and the component transforms are #477, in parallel).

Summary

JPEG 2000 tier-1 (ITU-T T.800 Annex D) was almost all of the first build of a page with a large JPX image (#352). It is now about 2.1x faster with sample-identical output:

  • Packed state. One uint per stripe column holds the 3 x 6 neighborhood significance, the column's signs and the refined/coded-this-plane bits, so every context is a table lookup on bits of one word (Tables D.1 to D.4). Idle columns are skipped on one test. Vertically causal contexts (D.7) come from not propagating a stripe's first row upward.
  • No per-sample bounds checks in the passes (ref arithmetic over arrays sized for the block and its border).
  • MQ registers in locals. A new register path, MqDecoder.Decode(ref byte, ReadOnlySpan<byte>, ref uint, ref uint, ref int, ref int), inlines BYTEIN, renormalizes by the leading zeros of A, and is branch-free apart from the common no-renormalization MPS case. JBIG2 keeps the existing path.
  • Rows are unrolled; refinement walks set bits, with the Table D.4 context and the step computed without branches; reconstruction runs by stripe column.

Profile (Native AOT, sample, pdfjs/issue5567.pdf, 1500 x 1125 lossless, 5.06 M samples)

Stage Base Head
Tier-1 coding passes 76.2% 78.3% (MQ inlined)
MQ RENORMD/BYTEIN 10.5% (inlined)
Reconstruction 4.2% 4.4%
Inverse wavelet 7.1% 13.5%
Component transform and store 1.9% 3.6%

Target

Tier-1 at most 45 ns per sample on the heaviest corpus image (issue5567) on an Apple M4: measured 87 ns before and 41.5 ns after (Native AOT). The whole decode goes from 96.1 to 50.1 ns/sample (AOT) and from 91.5 to 47.1 (JIT); OpenJPEG 2.5.4 single-threaded takes 235 ms on the same image, against our 238 ms (JIT). JpxTier1Benchmarks (random-data blocks, decisions close to even, the hard case): Default 123.2 to 75.8 ns/sample, BypassCausal 97.0 to 56.3, Allocated -. I have no CI-runner figure; the issue's 40 ns on the CI runner is not claimed. The bench-gate run will show the runner's numbers.

Identical output

  • Fetched corpora: RealWorldJpxTests passes against jpx-corpus-references.txt, along with every OpenJPEG vector and reduced decode (JpxDecodeFilterTests, JpxScaledDecodeTests).
  • 217 SHA-256 hashes (every corpus JPX stream at reductions 0/1/2, every JpxSamples vector and the J.11 example, tests/Corpus JPX files, the moon memo sample, the fuzz seed) are identical before and after.
  • Whole-render page hashes are identical across all four builds, base and head.
  • MqDecoderTests compares the register path with DECODE decision by decision.
  • Full Broadside.Tests (14,827) and Broadside.Rendering.Tests (1,445) pass.

Allocations

Zero per code-block (JpxAllocationTests.Tier_1_allocates_nothing_per_code_block). Existing gate figures are unchanged: --gate-allocations gives the same bytes as base for every JPX case. The only baseline edit is two new lines, both 0, for JpxTier1Benchmarks. Contexts stay byte for that reason: keeping each context's whole table word would have been 3.5% faster but would change each decode's allocation.

Fuzz

--smoke filter-jpx 120: 182 corpus files passed, 49,393,583 mutated inputs, no failures (plus a 60 s rerun after the last change: 21.7 M, no failures).

Whole render (first render, 96 DPI, document reopened every pass, best, ms, M4 Max)

Document JIT Native AOT Browser interpreter Browser AOT
pdfjs/issue5567.pdf 487 → 257 (-47%) 503 → 271 (-46%) 3,711 → 2,395 (-35%) 581 → 296 (-49%)
pdfjs/issue5481.pdf 389 → 230 (-41%) 399 → 236 (-41%) 3,731 → 2,459 (-34%) 502 → 296 (-41%)
pdfjs/issue5549.pdf 198 → 111 (-44%) 202 → 115 (-43%) 1,576 → 1,122 (-29%) 255 → 145 (-43%)
pdfium-tests/fx/action/123.pdf 81 → 74 91 → 85 1,210 → 1,213 167 → 160
scan-moon-memo-jpx.pdf 731 → 585 (-20%) 817 → 680 (-17%) 6,745 → 6,483 (-4%) 1,388 → 1,192 (-14%)

The browser builds are a browser-wasm console app under Node 22, built with and without RunAOTCompilation. Neither browser build regresses. Details and method: docs/benchmarks/jpx-tier1.md.

Conformance

ISO 32000-2 7.4.9 row: adds JpxCodeBlockDecoder, the new tests and a tier-1 note (T.800 C.3.2 to C.3.4, D.1 to D.7). No public API change.

…tier-1 benchmark

Packed stripe-column state, MQ registers in locals with a mostly branch-free DECODE,
unrolled rows and a stripe-column reconstruction take tier-1 on issue5567 from 87 to
41.5 ns per sample (Native AOT, M4). Samples are identical on every corpus image and
vector. Adds JpxTier1Benchmarks, MQ register-path tests, a per-code-block allocation
test, the conformance row and docs/benchmarks/jpx-tier1.md.
@michaelstonis michaelstonis added the perf Compare benchmark times in the benchmark gate (bench-gate.yml) label Nov 11, 2026
…px-tier1-speed

# Conflicts:
#	bench/baselines/gate/allocations.json
#	docs/conformance/iso-32000-2.md
@michaelstonis

Copy link
Copy Markdown
Owner Author

Merged spec/33-broadside-v1 at 13233b6 (#477's vectorized wavelet and the RCT/ICT changes). I kept both sides of allocations.json and of conformance row 7.4.9. On the merged tree the 217 sample hashes are unchanged, and the JPX and MQ tests pass (150 and 5). Whole decode of issue5567 is now 215.0 ms, 42.5 ns per sample on JIT; it was 238.3 ms with the tier-1 change alone.

@michaelstonis
michaelstonis merged commit ec0b6af into spec/33-broadside-v1 Nov 12, 2026
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

perf Compare benchmark times in the benchmark gate (bench-gate.yml)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant