From 80f896210a6871c9bc3b258f30e318fca2a2cd7a Mon Sep 17 00:00:00 2001 From: Akos Arinton Date: Mon, 19 Oct 2026 13:34:32 +0300 Subject: [PATCH] perf(compose): write band tiles as they finish, not after the band Parallel bands each collected their finished tiles until the band's last one was done, so with work stealing many bands could hold most of their tiles at once, close to a second copy of the output. Each tile is now put into its band behind a lock as soon as it is rendered; only the tiles in flight are held, as before the bands ran in parallel. --- crates/compose/src/lib.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/crates/compose/src/lib.rs b/crates/compose/src/lib.rs index 5d5a733e2bb..00cd5f4d00e 100644 --- a/crates/compose/src/lib.rs +++ b/crates/compose/src/lib.rs @@ -194,13 +194,16 @@ fn render_tiled_with(doc: &Document, rect: Rect, tile: i32, cx: &Ctx) -> Buffer }); } else { // Few, wide bands: the tiles of each band in parallel, and the bands too, so a band's - // slowest tile doesn't leave the other cores idle until the next band starts. + // slowest tile doesn't leave the other cores idle until the next band starts. Each + // tile is written as soon as it is done, so only the tiles in flight are held. out.px.par_chunks_mut(band_len).enumerate().for_each(|(i, band)| { let y0 = rect.y0 + i as i32 * tile; - let parts: Vec = band_tiles(y0).into_par_iter().map(run).collect(); - for part in &parts { - put(band, y0, part); - } + let band = std::sync::Mutex::new(band); + band_tiles(y0).into_par_iter().for_each(|t| { + let part = run(t); + let mut band = band.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + put(&mut band, y0, &part); + }); }); } }