Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 41 additions & 9 deletions apps/desktop-gpui/src/library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<J, R>(
executor: &gpui::BackgroundExecutor,
jobs: Vec<J>,
Expand Down Expand Up @@ -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<J, R>(
jobs: &flume::Receiver<J>,
results: &flume::Sender<R>,
decode: impl Fn(J) -> Option<R>,
) {
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
Expand Down Expand Up @@ -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
Expand Down
101 changes: 51 additions & 50 deletions apps/desktop-gpui/src/settings_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)),
);
Comment thread
yashranaway marked this conversation as resolved.
Resolved
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();
Expand Down Expand Up @@ -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();
Expand Down
Loading