Repository navigation
perf(rendering): keep box-filter footprints across rows and compose JPX rows directly (#509) - #510
Merged
Merged
Conversation
…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>
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 #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,
ImagePainterworked out the footprint's columns again (Axis.CreateandFill). It then reachedBoxFilterthrough 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 wasJpxComposer: every sample went through the column map and a precision branch into anintrow, 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 callsBoxFilter.Averagedirectly 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.BoxFilteris 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.
cmp).ImageFootprintsTestspaints 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.Broadside.Core.slnfsuite 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.Renderof page 1 at the given width.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.
Two ideas were measured and dropped:
BoxFilterkernel. Inlining the average into a row loop came out slower than callingBoxFilter.Averageper pixel from the tight loop.Allocations
Nothing is allocated per scanline or pixel. The footprint arrays come from
ArrayPool<T>.Shared. Each backend allocates theImageFootprintsobject once, the first time it paints an image; the span records are an inline array, so this is one object. The existingRasterizerAllocationTests(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:
ScaledImageBenchmarks.Replay(Width: 1206, Scaled: False)PhoneTileBenchmarks.Tiles(Page: "image-photo", Backend: "Software")ScaledImageBenchmarks.Page(Width: 1206, Scaled: False/True)ImageRenderingBenchmarks.Replay(Dpi: 72)Conformance
ImageFootprintsadded to the types,ImageFootprintsTeststo the tests, and a note in Notes.Public API changes
None. All new types are internal.
Verification
dotnet build Broadside.Core.slnf -warnaserror: 0 warningsdotnet format Broadside.Core.slnf --verify-no-changes: cleandotnet test Broadside.Core.slnf -c Release: 17,594 passeddotnet run --project tools/ConformanceCheck -- --root . --summary: 0 violationsFollow-up candidates (not in this PR)
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.Buffer.ZeroMemoryInternal, underJpxBlockJobandJpxTileDecoder.DecodeBlocks. That looks too large to be the state clears alone and is worth investigating.