Skip to content

perf(rendering): decode build-time Flate images in slices (#566) - #579

Merged
michaelstonis merged 2 commits into
spec/33-broadside-v1from
566-sliced-build-image-decode
Nov 18, 2026
Merged

michaelstonis merged 2 commits into
spec/33-broadside-v1from
566-sliced-build-image-decode

Conversation

@michaelstonis

@michaelstonis michaelstonis commented Nov 18, 2026 •

Copy link
Copy Markdown
Owner

Resolves #566

What changed

A display list built in slices (PdfPage.BeginDisplayList, which the browser viewer uses for tiles and for the page model) no longer decodes a Flate image inside the Do that paints it. Before a top-level Do of an image XObject, the interpreter asks the builder to decode the image, then its soft mask, a slice at a time; when the slice's deadline passes the run pauses before the Do and reads it again on the next slice. The Do then takes the decoded samples.

  • StreamDecoder.ChainJob: the filter chain with resumable stages: Flate inflates 16 KB per step (FlateDecodeFilter.Inflation), the predictor undoes 16 rows per step (Predictor.Run). Other filters (LZW, CCITT, JBIG2, crypt, ASCII) run in one step, as do image codecs at the end of the chain (DCT and JPEG 2000 are kept encoded and already decode in slices at draw time, Smooth browser viewer without a second runtime: native image decode, sliceable decode, AOT demo #365).
  • Predictor.Run is shared by the one-step decode (a struct, no allocation; FilterAllocationTests unchanged).
  • PdfImage.LoadJob: Prepare in slices.
  • DisplayListBuilder.PrepareImage / ImagePrefetch: decodes the image and, only under the conditions Decode checks before decoding one, its soft mask; InternImage takes the result (and the epoch at which the decode started) or disposes it when the cache already had the image.
  • Not prefetched (one step as before): strict reading, printing, images with OC, image masks, images inside forms and patterns, explicit masks, inline images.
  • Hot AOT profiles re-recorded (publish-static.sh --aot-profiler, AotProfileTests.Record_the_hot_aot_profile) so the new methods are compiled in --hot-aot.
  • Fuzz target filter-sliced: the one-step and sliced decodes of any chain give the same bytes, outcome and diagnostics.

The unsliced path (CreateDisplayList, renders, extraction) is unchanged apart from Predictor.Decode now looping over Predictor.Run.

Measurements

ScrollFrameTests on catalogue.pdf, Playwright Chromium, M4 Max, load 8 to 11. Before = #564's branch merged with the spec/33 tip; after = that plus this PR (both local measurement trees, not pushed). Phone 390x844 @3x, CPU/4:

Build Worker Rendering the first view: max frame, over 50 ms Viewer's own work: max frame First view presented (ms)
interpreter no 233, 9 → 110, 11 231 → 94 6828 → 7014
interpreter yes 242, 8 → 115, 6 35 → 36 3882 → 3816
hot AOT no 102, 5 → 93, 5 104 → 60 3344 → 3318
hot AOT yes 132, 4 → 140, 3 26 → 41 2668 → 2677

Desktop rows unchanged (25 to 42 ms). Natively, the longest step of a sliced build of page 2 with a 2 ms budget falls from 12.7 to 16.0 ms to 2.4 to 3.1 ms (page 11: 12.4 to 16.1 → 2.4 to 2.5 ms). --hot-aot dotnet.native.wasm: 11,592,220 bytes (cap 16,000,000).

"Done when", item 2 is not met: the catalogue max frame with hot AOT on the phone stays at 93 ms (no worker) and 132 to 140 ms (worker, within the run-to-run noise the doc already records for that row). The Flate step is gone (interpreter frames halve, the viewer's own tile frame drops to 60 ms with hot AOT); with the worker the page thread draws no content, so what remains is the page model's other one-step items and the runtime (GC of a 290 to 590 MiB heap), not this decode. Written up in docs/benchmarks/smooth-browser-viewer.md ("Build-time Flate images (#566)").

Verification

  • SlicedImageBuildTests (12 Flate variants: PNG and TIFF predictors, truncated, corrupt, raw deflate, unknown tags, partial rows, LZW, ASCIIHex+Flate, too long, soft mask of another size): list, samples, soft-mask samples, pixels and diagnostics equal the list built whole; cancellation mid-decode; cache reuse.
  • SlicedChainDecodeTests (15 cases incl. decoded-length limit, empty, no filter): bytes, outcome and diagnostics equal.
  • SlicedPreparationTests, golden images (Golden*), Core solution tests. In the full local run 13 allocation tests in unrelated areas (colour, JPX, function, shading) failed in Debug with the Homebrew SDK; they pass in Release with the canonical SDK (same on the base).
  • dotnet run -c Release --project tests/Broadside.Fuzz -- --smoke filter-sliced 60: 190 corpus files passed, 6,778,772 mutated inputs in 60 s, no failures.
  • dotnet format --verify-no-changes on Core and Fuzz.

Notes for review

  • Diagnostics: the sliced build reports the same diagnostics in the same order for the variants tested. In damaged files where both the image's colour space and its Mask entry are malformed, MaskKind is now read once more before the Do (the sink de-duplicates it).
  • Known gate false alarms (Benchmark gate reports stable regressions when head and base code are identical #554: GrayToRgb ~1.33x; Replay ~1.4x AVX-512 off on Intel) may show up; they are unrelated.
  • Windows-only projects are not affected and were not run locally.

A display list built in slices (PdfPage.BeginDisplayList) now decodes the
image of a top-level Do, and its soft mask, before the Do: the Flate
filter inflates 16 KB and the predictor undoes 16 rows per step, and the
interpreter reads the Do again once the decode is done. The samples, the
list, the pixels and the diagnostics equal those of a list built whole.

- StreamDecoder.ChainJob: the filter chain with resumable Flate and
  predictor stages; other filters run in one step.
- Predictor.Run: the predictor's rows, shared by the one-step decode.
- PdfImage.LoadJob: Prepare in slices.
- DisplayListBuilder.PrepareImage: the image and soft-mask decode the
  Do takes.
- Fuzz target filter-sliced compares sliced and one-step decodes.
- Hot AOT profiles re-recorded for the new methods.

Signed-off-by: Michael Stonis <120685+michaelstonis@users.noreply.github.com>
Signed-off-by: Michael Stonis <120685+michaelstonis@users.noreply.github.com>
@michaelstonis

Copy link
Copy Markdown
Owner Author

Benchmark gate: the only finding is PixelPackingBenchmarks.GrayToRgb 1.33x on the Intel runner with AVX-512 on, the known false alarm tracked in #554 (this PR does not touch pixel packing). No allocation changes against bench/baselines/gate/allocations.json. Every other check is green.

@michaelstonis
michaelstonis merged commit 52c2c52 into spec/33-broadside-v1 Nov 18, 2026
52 of 53 checks passed
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