diff --git a/apps/desktop-gpui/src/library.rs b/apps/desktop-gpui/src/library.rs index 921cc185ff7..fd8f99cfb8c 100644 --- a/apps/desktop-gpui/src/library.rs +++ b/apps/desktop-gpui/src/library.rs @@ -817,8 +817,10 @@ const MAX_DECODE_WORKERS: usize = 8; /// results come back over the receiver in completion order. /// /// Callers keep the returned tasks alive while draining the receiver -- -/// dropping them cancels whatever has not started, the same cancel-on-reassign -/// contract the sequential await-per-item loops this replaced had. +/// dropping them cancels whatever has not started, and dropping the receiver +/// stops each running worker after its current decode, the same +/// cancel-on-reassign contract the sequential await-per-item loops this +/// replaced had. pub fn spawn_decode_pool( executor: &gpui::BackgroundExecutor, jobs: Vec, @@ -857,18 +859,26 @@ where let job_rx = job_rx.clone(); let result_tx = result_tx.clone(); let decode = decode.clone(); - executor.spawn(async move { - while let Ok(job) = job_rx.try_recv() { - if let Some(result) = decode(job) { - let _ = result_tx.send(result); - } - } - }) + executor.spawn(async move { drain_decode_jobs(&job_rx, &result_tx, decode) }) }) .collect(); (tasks, result_rx) } +fn drain_decode_jobs( + jobs: &flume::Receiver, + results: &flume::Sender, + decode: impl Fn(J) -> Option, +) { + while !results.is_disconnected() + && let Ok(job) = jobs.try_recv() + { + if let Some(result) = decode(job) { + let _ = results.send(result); + } + } +} + /// Where the shared downscaled-thumbnail cache lives: the OS cache dir under /// this app's own identifier (cache data is regenerable, so it belongs where /// backups and migration skip it), not the `so.cap.desktop` app-data dir both @@ -1217,6 +1227,28 @@ pub fn bundle_thumbnail_path(project_dir: &Path) -> PathBuf { mod tests { use super::*; + #[test] + fn decode_jobs_stop_once_nobody_takes_the_results() { + let (job_tx, job_rx) = flume::unbounded(); + for job in 0..6_u32 { + job_tx.send(job).unwrap(); + } + let (result_tx, result_rx) = flume::unbounded(); + let receiver = std::cell::RefCell::new(Some(result_rx)); + let decoded = std::cell::RefCell::new(Vec::new()); + + drain_decode_jobs(&job_rx, &result_tx, |job| { + decoded.borrow_mut().push(job); + if job == 2 { + receiver.borrow_mut().take(); + } + (job != 1).then_some(job) + }); + + assert_eq!(*decoded.borrow(), [0, 1, 2]); + assert_eq!(job_rx.len(), 3); + } + /// A `.cap` bundle with a `recording-meta.json` the real parser accepts. /// `filetime` is not a dependency here, so the sort key -- the directory's /// own creation time -- is separated by creating the fixtures in order diff --git a/apps/desktop-gpui/src/settings_window.rs b/apps/desktop-gpui/src/settings_window.rs index b911bdcd041..b4714d6be49 100644 --- a/apps/desktop-gpui/src/settings_window.rs +++ b/apps/desktop-gpui/src/settings_window.rs @@ -867,7 +867,7 @@ impl SettingsWindow { } /// The `recordings` query: scan the library on the background executor, - /// then decode each thumbnail there too. + /// then decode the thumbnails there through [`library::spawn_decode_pool`]. /// /// Same two-stage shape as the main window's Recents (`refresh_recents`): /// the list lands first so the rows can paint with their grey placeholder, @@ -893,36 +893,36 @@ impl SettingsWindow { return; }; - for (index, path) in thumbnails { - let image = cx - .background_executor() - .spawn({ - let path = path.clone(); - async move { library::decode_thumbnail(&path) } - }) - .await; - let Some(image) = image else { continue }; - + let (_decodes, results) = library::spawn_decode_pool( + cx.background_executor(), + thumbnails, + |(index, path)| library::decode_thumbnail(&path).map(|image| (index, path, image)), + ); + while let Ok(first) = results.recv_async().await { + let mut batch = vec![first]; + batch.extend(results.try_iter()); if this .update_in(cx, |this, window, cx| { - let Some(row) = this - .recordings - .items - .as_mut() - .and_then(|rows| rows.get_mut(index)) - else { - return; - }; - // A refresh may have landed while this decode was in - // flight, and the row at this index may now be a - // different recording. - if row.item.thumbnail.as_deref() != Some(path.as_path()) { - return; - } - if let Some(old) = row.thumbnail.replace(image) { - let _ = window.drop_image(old); + for (index, path, image) in batch { + let Some(row) = this + .recordings + .items + .as_mut() + .and_then(|rows| rows.get_mut(index)) + else { + continue; + }; + // A refresh may have landed while this decode was in + // flight, and the row at this index may now be a + // different recording. + if row.item.thumbnail.as_deref() != Some(path.as_path()) { + continue; + } + if let Some(old) = row.thumbnail.replace(image) { + let _ = window.drop_image(old); + } + row.thumbnail_stale = false; } - row.thumbnail_stale = false; cx.notify(); // An unfocused window only repaints when asked. window.refresh(); @@ -1081,30 +1081,31 @@ impl SettingsWindow { return; }; - for (index, path) in thumbnails { - let image = cx - .background_executor() - .spawn({ - let path = path.clone(); - async move { library::decode_thumbnail(&path) } - }) - .await; - let Some(image) = image else { continue }; + let (_decodes, results) = library::spawn_decode_pool( + cx.background_executor(), + thumbnails, + |(index, path)| library::decode_thumbnail(&path).map(|image| (index, path, image)), + ); + while let Ok(first) = results.recv_async().await { + let mut batch = vec![first]; + batch.extend(results.try_iter()); if this .update_in(cx, |this, window, cx| { - let Some(row) = this - .screenshots - .items - .as_mut() - .and_then(|rows| rows.get_mut(index)) - else { - return; - }; - if row.item.thumbnail.as_deref() != Some(path.as_path()) { - return; - } - if let Some(old) = row.thumbnail.replace(image) { - let _ = window.drop_image(old); + for (index, path, image) in batch { + let Some(row) = this + .screenshots + .items + .as_mut() + .and_then(|rows| rows.get_mut(index)) + else { + continue; + }; + if row.item.thumbnail.as_deref() != Some(path.as_path()) { + continue; + } + if let Some(old) = row.thumbnail.replace(image) { + let _ = window.drop_image(old); + } } cx.notify(); window.refresh();