performance: the effect stack copies the whole picture for every effect #8

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.

Steps to reproduce

  1. opendither picture.png with a 4000×3000 picture.
  2. Add one effect (tint), then read the status line: rendered in … ms.
  3. Add a second effect and repeat.

What should have happened

Adding an effect costs the time of that effect, and the stack reuses buffers between effects.

What happened instead

app/src/fx.rs:533 fn apply_at starts with a full copy of the picture, and every effect then allocates another full picture for its own output:

pub fn apply_at(src: &RgbaImage, stack: &[Fx], time: f32) -> RgbaImage {
    let mut img = src.clone();
    for fx in stack.iter().filter(|f| f.enabled && f.active) {
        img = run(&img, fx, time);
    }
    img
}

At 24 MP that is 96 MB per copy: a ten-effect stack allocates (and frees) about 1 GB per render, on top of the ~3 buffers engine::process already holds. The stack is also strictly sequential and single-threaded, while the dither, the tone stage and the composite next to it are now split over the cores (src/par.rs) — so with many effects the effect stack becomes the only thing running on one core.

Two copies are avoidable without touching a single algorithm: the first effect can write into a buffer it is handed instead of cloning src, and each effect can consume the buffer the previous one produced (ping-pong between two buffers) instead of allocating a new RgbaImage per step. Both keep the picture bit-identical.

Suggested fix

  1. apply_at takes an owned scratch buffer and ping-pongs: effect n reads buffer A, writes buffer B, swaps.
  2. Effects whose body is a per-pixel pass with no neighbour reads (tint, post-process, chromatic, lens curve, neo glow's accumulation, …) can take a row-band split like the dither passes — the same par::rows applies, since each of them writes only its own rows.
  3. The two that need neighbours (glow, orbs, stress relief) stay whole, or get their own tiling.

A check that would catch it

--timing only measures the dither. A --timing --effects row that renders a 1200×900 picture with 1, 2, 4, 8 effects and prints the ms per effect would make a regression visible; the numbers per effect should stay flat as the stack grows.

Version: 0.1.2 (`main` @ 41c7fc5) System: Linux, X11 (XWayland), 16 cores. ## Steps to reproduce 1. `opendither picture.png` with a 4000×3000 picture. 2. Add one effect (`tint`), then read the status line: `rendered in … ms`. 3. Add a second effect and repeat. ## What should have happened Adding an effect costs the time of that effect, and the stack reuses buffers between effects. ## What happened instead `app/src/fx.rs:533` `fn apply_at` starts with a full copy of the picture, and every effect then allocates another full picture for its own output: ```rust pub fn apply_at(src: &RgbaImage, stack: &[Fx], time: f32) -> RgbaImage { let mut img = src.clone(); for fx in stack.iter().filter(|f| f.enabled && f.active) { img = run(&img, fx, time); } img } ``` At 24 MP that is 96 MB per copy: a ten-effect stack allocates (and frees) about 1 GB per render, on top of the ~3 buffers `engine::process` already holds. The stack is also strictly sequential and single-threaded, while the dither, the tone stage and the composite next to it are now split over the cores (`src/par.rs`) — so with many effects the effect stack becomes the only thing running on one core. Two copies are avoidable without touching a single algorithm: the first effect can write into a buffer it is handed instead of cloning `src`, and each effect can consume the buffer the previous one produced (ping-pong between two buffers) instead of allocating a new `RgbaImage` per step. Both keep the picture bit-identical. ## Suggested fix 1. `apply_at` takes an owned scratch buffer and ping-pongs: effect *n* reads buffer A, writes buffer B, swaps. 2. Effects whose body is a per-pixel pass with no neighbour reads (tint, post-process, chromatic, lens curve, neo glow's accumulation, …) can take a row-band split like the dither passes — the same `par::rows` applies, since each of them writes only its own rows. 3. The two that need neighbours (glow, orbs, stress relief) stay whole, or get their own tiling. ## A check that would catch it `--timing` only measures the dither. A `--timing --effects` row that renders a 1200×900 picture with 1, 2, 4, 8 effects and prints the ms per effect would make a regression visible; the numbers per effect should stay flat as the stack grows.
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#8
No description provided.