bug: the canvas limit is 128 MP, but rendering one needs about 4 GB #9

Open
opened 2026-11-16 14:26:49 +00:00 by ben · 0 comments
Owner

Version: 0.1.2 (main @ 41c7fc5)
System: Linux, X11 (XWayland), 16 cores. Measured peak RSS.

Steps to reproduce

  1. Render pictures of growing size and watch the peak memory:
    opendither big.png --render out.png

What should have happened

A document either fits in memory — and then it renders — or it is refused with a message, like a canvas that is too large already is.

What happened instead

Measured peak RSS for a single --render of a plain PNG:

picture pixels peak RSS
1920×1080 2.1 MP 163 MB
4000×3000 12 MP 461 MB
6000×4000 24 MP 836 MB

That is a marginal cost of about 31 MB per megapixel, and it is what the pipeline inherently holds: the composited RGBA frame, the [f32; 3] working buffer engine::process builds (12 bytes per pixel), a clone of it for error diffusion, the packed RGBA result and the output. MAX_CANVAS_PIXELS allows 128 MP (app/src/layers.rs:22, canvas_fits), so a document the app accepts needs roughly 4 GB to render — on a machine that cannot provide it the process is killed by the OOM killer instead of being told no.

The layer stack has no budget at all: every layer keeps a full RGBA image for its lifetime, fn add_layer (main.rs:971) stores the whole composite as a new layer, and duplicate_layer copies it again. Ten layers on a 24 MP canvas is 1 GB of pixels before anything renders.

Suggested fix

  • Express the limit in bytes, derived from the real peak: MAX_CANVAS_PIXELS becomes something like budget_bytes / 31 MB per MP with a documented factor and a comment pointing at the measurement (the repository already works this way elsewhere). Keep the existing refusal message, so nothing else has to change.
  • Give the stack a budget too: sum(layer pixels) + composite + buffers <= budget, checked when a layer is added or duplicated, with a status-line refusal instead of a kill.
  • Long term, the [f32; 3] working buffer is the one worth removing: the tone stage is a 256-entry LUT on bytes and the spatial filters could work in a tiled f32 window with a band halo.

A check that would catch it

--audit can assert the arithmetic (peak_estimate_pixels * 31 MB against the budget constant) and that a canvas one pixel above the derived limit is refused. A test that only refuses one specific number will be wrong again the next time a buffer is added, so the estimate and the buffers should be derived from the same named constants.

Version: 0.1.2 (`main` @ 41c7fc5) System: Linux, X11 (XWayland), 16 cores. Measured peak RSS. ## Steps to reproduce 1. Render pictures of growing size and watch the peak memory: `opendither big.png --render out.png` ## What should have happened A document either fits in memory — and then it renders — or it is refused with a message, like a canvas that is too large already is. ## What happened instead Measured peak RSS for a single `--render` of a plain PNG: | picture | pixels | peak RSS | |---|---|---| | 1920×1080 | 2.1 MP | 163 MB | | 4000×3000 | 12 MP | 461 MB | | 6000×4000 | 24 MP | 836 MB | That is a marginal cost of about **31 MB per megapixel**, and it is what the pipeline inherently holds: the composited RGBA frame, the `[f32; 3]` working buffer `engine::process` builds (12 bytes per pixel), a clone of it for error diffusion, the packed RGBA result and the output. `MAX_CANVAS_PIXELS` allows **128 MP** (`app/src/layers.rs:22`, `canvas_fits`), so a document the app accepts needs roughly **4 GB** to render — on a machine that cannot provide it the process is killed by the OOM killer instead of being told no. The layer stack has no budget at all: every layer keeps a full RGBA image for its lifetime, `fn add_layer` (`main.rs:971`) stores the whole composite as a new layer, and `duplicate_layer` copies it again. Ten layers on a 24 MP canvas is 1 GB of pixels before anything renders. ## Suggested fix - Express the limit in bytes, derived from the real peak: `MAX_CANVAS_PIXELS` becomes something like `budget_bytes / 31 MB per MP` with a documented factor and a comment pointing at the measurement (the repository already works this way elsewhere). Keep the existing refusal message, so nothing else has to change. - Give the stack a budget too: `sum(layer pixels) + composite + buffers <= budget`, checked when a layer is added or duplicated, with a status-line refusal instead of a kill. - Long term, the `[f32; 3]` working buffer is the one worth removing: the tone stage is a 256-entry LUT on bytes and the spatial filters could work in a tiled `f32` window with a band halo. ## A check that would catch it `--audit` can assert the arithmetic (`peak_estimate_pixels * 31 MB` against the budget constant) and that a canvas one pixel above the derived limit is refused. A test that only refuses one specific number will be wrong again the next time a buffer is added, so the estimate and the buffers should be derived from the same named constants.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
ben/openDither#9
No description provided.