Skip to content

perf(rendering): keep box-filter footprints across rows and compose JPX rows directly (#509) - #510

Merged
michaelstonis merged 2 commits into
spec/33-broadside-v1from
509-image-resampling
Nov 12, 2026
Merged

michaelstonis merged 2 commits into
spec/33-broadside-v1from
509-image-resampling

Conversation

@michaelstonis

@michaelstonis michaelstonis commented Nov 12, 2026 •

Copy link
Copy Markdown
Owner

Closes #509. Follow-up to #352 (and #475, #508).

Summary

After #508, painting a reduced image spent most of its time on per-pixel setup, not on the average. For every device pixel, ImagePainter worked out the footprint's columns again (Axis.Create and Fill). It then reached BoxFilter through a chain of calls that copied several spans. With the average itself removed, painting still cost about 6 ns per device pixel. The second hotspot was JpxComposer: every sample went through the column map and a precision branch into an int row, and a second pass narrowed that row to bytes. Issue #509 has the profile.

  • ImageFootprints (new, internal). When u does not change down the image (C = 0, an upright image), every row whose span starts at the same pixel and the same u gets the same columns, pixel for pixel. The painter works out each span's columns once and reads them back on later rows. A row usually arrives as a few spans that repeat on every row: a partial edge pixel, then the rest. So up to four spans are kept, in pooled arrays that grow to fit and go back to the pool when the backend is disposed. A span is found only by its first pixel and its u, compared as the same double. Its columns are worked out with u stepped exactly as the painter steps it.
  • ImagePainter.SpanKept. For an upright image without a mask of its own, a tight per-span loop calls BoxFilter.Average directly with the span's rows and the kept columns. The other cases (skewed, rotated, masked) keep the per-pixel loop, and also read kept columns when C = 0.
  • JpxComposer. When every colour channel is a direct, non-sub-sampled 8-bit component (no palette) and no colour conversion applies, the output row is the component rows interleaved straight into the image row. Every other case is unchanged.
  • BoxFilter is unchanged. No new SIMD kernel.

Output: byte-identical

The same averages come from the same operations in the same order, from the same u values.

  • Renders of issue5567, issue5481, issue5549 and 123 at 195, 390 and 1206 px are byte-identical to base (cmp).
  • ImageFootprintsTests paints a random premultiplied image both ways and asserts identical bytes. It covers 7 transforms (reductions of several ratios, flipped, skewed down, rotated), with and without Interpolate, with and without a mask, Normal and Multiply at alpha 0.6, and rows split into 2 or 3 spans at varying pixels. Changing the step by a relative 1e-10 fails 3 of the 7 cases.
  • No golden changed. The full Broadside.Core.slnf suite passes locally (17,594 tests).

Numbers

Apple M4 Max. Best of 9 (browser: best of 5), two interleaved base/head rounds, best of both rounds. Times are in ms.

Paint only. The document is open and the image decoded and cached; SoftwareRasterizer.Render of page 1 at the given width.

File Width JIT base -> head Native AOT base -> head Browser AOT (Node 22) base -> head
123 390 5.68 -> 3.90 (-31%) 5.85 -> 3.97 (-32%) 10.32 -> 7.39 (-28%)
123 1206 24.12 -> 11.77 (-51%) 27.98 -> 15.26 (-45%) 53.49 -> 29.91 (-44%)
issue5567 390 3.67 -> 2.44 (-34%) 3.74 -> 2.63 (-30%) 6.05 -> 4.69 (-22%)
issue5567 1206 14.66 -> 8.66 (-41%) 16.63 -> 10.46 (-37%) 32.74 -> 19.91 (-39%)
issue5481 390 2.31 -> 1.48 (-36%) 2.55 -> 1.66 (-35%) 4.22 -> 3.04 (-28%)
issue5481 1206 9.95 -> 8.67 (noise) 3.73 -> 3.81 (noise) 6.95 -> 6.76 (noise)

issue5481 at 1206 px is enlarged (0.9 samples per pixel), so it does not take the box filter path. JIT runs of that case vary between about 4 and 10 ms in both builds.

Cold document. List build, decode and render of page 1, software tile backend, k = 2, the methodology of #508.

File Width JIT base -> head Native AOT base -> head Browser AOT (Node 22) base -> head
123 390 28.8 -> 25.5 (-11%) 29.7 -> 25.0 (-16%) 56.8 -> 47.5 (-16%)
123 1206 45.6 -> 31.0 (-32%) 51.4 -> 36.3 (-29%) 98.4 -> 69.7 (-29%)
issue5567 390 40.7 -> 38.3 (-6%) 40.6 -> 36.9 (-9%) 244.9 -> 237.8 (-3%)
issue5567 1206 55.6 -> 47.8 (-14%) 53.3 -> 44.9 (-16%) 271.4 -> 253.5 (-7%)
issue5481 390 51.5 -> 49.5 (-4%) 50.8 -> 48.4 (-5%) 107.6 -> 102.8 (-4%)
issue5481 1206 53.2 -> 51.2 (-4%) 52.1 -> 50.9 (-2%) 110.4 -> 107.8 (-2%)

Two ideas were measured and dropped:

  • A shortcut for footprints whose samples are all equal (provably bit-exact). It made the paint slower: the lossy JPX pages are almost never exactly uniform, so the check only added cost.
  • A row-at-a-time BoxFilter kernel. Inlining the average into a row loop came out slower than calling BoxFilter.Average per pixel from the tight loop.

Allocations

Nothing is allocated per scanline or pixel. The footprint arrays come from ArrayPool<T>.Shared. Each backend allocates the ImageFootprints object once, the first time it paints an image; the span records are an inline array, so this is one object. The existing RasterizerAllocationTests (images and masks at several DPIs) still read 0. Allocation gate. One baseline entry changes, by +8 B per call: FirstPaintBenchmarks.FirstPaint(Page: "text-standard14.pdf#0"), 492,056 B -> 492,064 B. The software backend gained one reference field (_footprints), and that benchmark creates a backend on every call. The change is deterministic, and the figure comes from this PR's CI allocation run. Every other case of 659 is exact and unchanged.

Timing gate (CI, x64, against the base commit). No case slower beyond tolerance. Faster cases include:

Case Base -> head
ScaledImageBenchmarks.Replay(Width: 1206, Scaled: False) 0.66x
PhoneTileBenchmarks.Tiles(Page: "image-photo", Backend: "Software") 0.65x
ScaledImageBenchmarks.Page(Width: 1206, Scaled: False/True) 0.80x
ImageRenderingBenchmarks.Replay(Dpi: 72) 0.82x

Conformance

  • ISO 32000-2 §8.9.5.3 row: ImageFootprints added to the types, ImageFootprintsTests to the tests, and a note in Notes.

Public API changes

None. All new types are internal.

Verification

  • dotnet build Broadside.Core.slnf -warnaserror: 0 warnings
  • dotnet format Broadside.Core.slnf --verify-no-changes: clean
  • dotnet test Broadside.Core.slnf -c Release: 17,594 passed
  • dotnet run --project tools/ConformanceCheck -- --root . --summary: 0 violations
  • Renders byte-identical to base for 4 files at 3 widths

Follow-up candidates (not in this PR)

  • The box average itself (BoxFilter.AverageVector) is now the largest part of the paint, at about 1.3 ns per sample. Its per-pixel tail (the division and four conversions) and its short loops dominate for 2- to 3-tap footprints.
  • On JPX decodes, the sampler attributes a large share of helper-thread CPU to Buffer.ZeroMemoryInternal, under JpxBlockJob and JpxTileDecoder.DecodeBlocks. That looks too large to be the state clears alone and is worth investigating.

…PX rows directly (#509)

For an upright reduced image, every row whose span starts at the same
pixel and u takes the same footprint columns, so ImageFootprints keeps
them and the painter reads them back instead of working them out per
pixel. A tight per-span loop calls BoxFilter.Average directly. The same
averages are computed in the same order from the same u values, so
renders are byte-identical.

JpxComposer interleaves 8-bit component rows straight into the output
row when every colour channel is a direct, non-sub-sampled 8-bit
component and no conversion applies.

Signed-off-by: Michael Stonis <120685+michaelstonis@users.noreply.github.com>
…baseline (#509)

The software backend gains one reference field (its ImageFootprints), so
each backend FirstPaint creates is 8 bytes larger.

Signed-off-by: Michael Stonis <120685+michaelstonis@users.noreply.github.com>
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