Skip to content

M5.6: a view slot renders one job at a time, so a drag's renders don't pile up - #1302

Merged
bflatastic merged 1 commit into
storytold:mainfrom
mattsivak:pool-one-render-per-slot
Oct 13, 2026
Merged

bflatastic merged 1 commit into
storytold:mainfrom
mattsivak:pool-one-render-per-slot

Conversation

@mattsivak

@mattsivak mattsivak commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Problem: JobPool::submit replaced a slot's queued job, but a newer job for a slot whose job was already running started beside it on another worker. A slider drag submits faster than renders finish, so one view (the loupe, its 1:1 zoom window) had several renders on the GPU at once, and about 30 % of finished renders were thrown away as superseded. Found while profiling #1228 with a per-frame display: in an 8 s 1:1 Exposure drag on a 61 MP raw, 194 of 645 and 248 of 719 finished renders were discarded.

Fix (crates/preview/src/pool.rs): the pool keeps the slots that have a job running (Shared::busy, locked inside the queue lock). take skips queued jobs for a busy slot. A newer submission still replaces the queued one, so the slot runs the newest request next. A drop guard frees the slot when the job ends, also when it panics, and wakes the workers. Other slots aren't held up; thumbnails each have their own slot and are unaffected.

Measured: release app, M4 Max, arw-sony-a7cr-compressed.arw (61 MP) at 1:1, 8 s scripted Exposure drags (ui.dragWidget), frame intervals logged by the per-frame display. The runs are noisy on a working machine, but:

  • thrown-away renders roughly halved (348 → 156 and 278 → 106 per run on one setup; 133 → 96 and 239 → 141 on the other);
  • with this change, frames over 20 ms were 0 % in each of 4 runs (p95 8–13 ms, worst 18–36 ms). Without it, one of two runs dropped 10 % of frames (p95 92 ms, worst 755 ms).

Tests (crates/preview/src/tests.rs):

  • a_slot_runs_one_job_at_a_time: a second and third submission for a busy slot wait (the third replaces the second), another slot runs meanwhile, and the newest runs once the slot frees. The concurrency high-water mark is 1. Fails without the fix ("a second job of slot 1 started beside the first").
  • a_panicking_job_frees_its_slot.
  • cargo test -p lightcraft-preview: 15 pass. cargo test -p lightcraft-ui-egui: 738 pass, 7 fail on main without this change too: tests_dialog_fit::export_dialog_is_unchanged_in_a_tall_window (known, see LR-VIEW-ZOOM: slider drags stay sharp where the GPU keeps up (#330) #1009) and 6 tests_model_setup tests, which I re-ran with the pool change reverted and they fail the same.
  • Clippy is clean for lightcraft-preview apart from the toolchain-1.99 chunks_exact_to_as_chunks lint in untouched code (Issue #989: clippy 1.99, fill_box reads its buffer with as_chunks::<3>() (item 4) #997).

Related: #1240 (renders as window textures, now opt-in), where the measurements behind this came from.

Refs #330 (slider drags too slow) and #1228.

…t pile up

The job pool replaced a slot's queued job, but a newer job for a slot whose job was
already running started beside it on another worker. A slider drag submits faster
than renders finish, so one view had several renders on the GPU at once and about
30 % of finished renders were thrown away (61 MP raw, 1:1 Exposure drag). Now a
slot's next job waits until its running one ends (a newer submission still
replaces it in the queue); other slots are not held up, and a job that panics
frees its slot.

Measured on a 61 MP raw at 1:1, 8 s Exposure drags (M4 Max): thrown-away renders
roughly halved, and frames over 20 ms 0 % in every one of 4 runs (one run of the
overlapping pool dropped 10 %).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@FredApps FredApps left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed and tested at d7037d1 on Linux. cargo test -p lightcraft-preview passes, including the two new tests.

The fix looks right. Lock order is the same everywhere (queue, then busy, in both take and finished). The Free guard frees the slot during unwinding, so a panicking job doesn't leave its slot stuck, and run_pending uses the guard too.

Small notes, none of them blocking:

  • busy is only ever touched while queue is held, so it could live inside the queue mutex, e.g. Mutex<(Vec<Entry>, Vec<S>)>. That would remove the second lock and the "locked inside queue" rule that only lives in a comment.
  • finished calls notify_all every time a job ends. That's fine at this pool size; notify_one would only be correct if a single waiter could always take the freed slot, so leaving it as is is reasonable.
  • It might help to add a line to the doc of is_queued: a job for a busy slot now stays queued while the slot's job runs, so callers that read is_queued as "about to start" see it as queued for longer.

Tests: cargo test -p lightcraft-preview passes (15/15).

@dev-null-undefined

Copy link
Copy Markdown
Contributor

One behaviour change to be aware of, measured while prototyping drag latency on a Sony ILCE-7RM6 (64 MP raws, RTX 4070 Ti SUPER, release build, a saturated 4-CPU quota, medians of 3).

With one job per slot, the loupe's render of the next photo waits for the running render of the photo you just stepped past, because both are Slot::Main. On a 64 MP raw that running render is a decode of a second or more.

  • Stepping back (settled on C, step to B, to A (not decoded yet), back to B, 150 ms apart; time until B's full render): main 1.3 s, this PR 2.2 s.
  • Stepping forward through photos that aren't decoded yet (C → B → A, 250 ms apart): main 2.1 s, this PR 3.3 s. Here B's queued render is replaced by A's and never runs, which is the point of the PR, but A still waits for the stepped-past render that is already running.

We tried keeping the busy check per (slot, photo) for view slots (JobPool::submit_in with a group): a drag's renders of one photo still run one at a time, and another photo's render starts at once. That brought stepping back to 0.9 s but made stepping forward slower (4.0 s), because the stepped-past decodes run beside the new one again, as on main. So we're not opening it. Cancelling the stepped-past decode would probably be the real fix; posting the numbers in case they help.

@FredApps

Copy link
Copy Markdown
Contributor

Thanks @dev-null-undefined, that's a useful measurement. I approved this without considering that case: with one job per Slot::Main, stepping back to a photo has to wait for the in-flight decode of the one just passed, and on 64 MP raws that's long enough to notice (1.3 s → 2.2 s). @mattsivak, would it be worth letting a newer Main request supersede or cancel a stale running one (or giving the next/previous photo its own slot), so drags keep the no-pile-up benefit without slowing down stepping between photos?

@bflatastic

Copy link
Copy Markdown
Member

Landed on main in #1431. Thank you, @mattsivak!


Generated by Claude Code

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.

4 participants