Use an executor::Background in AppContext::thread_pool

This commit is contained in:
Antonio Scandurra
2021-07-13 14:06:38 +02:00
parent f58ef25fc5
commit f836a25500
11 changed files with 54 additions and 265 deletions
+6 -8
View File
@@ -403,7 +403,7 @@ impl FileFinder {
self.cancel_flag.store(true, atomic::Ordering::Relaxed);
self.cancel_flag = Arc::new(AtomicBool::new(false));
let cancel_flag = self.cancel_flag.clone();
let background_task = cx.background_executor().spawn(async move {
Some(cx.spawn(|this, mut cx| async move {
let include_root_name = snapshots.len() > 1;
let matches = match_paths(
snapshots.iter(),
@@ -414,14 +414,12 @@ impl FileFinder {
100,
cancel_flag.clone(),
pool,
);
)
.await;
let did_cancel = cancel_flag.load(atomic::Ordering::Relaxed);
(search_id, did_cancel, query, matches)
});
Some(cx.spawn(|this, mut cx| async move {
let matches = background_task.await;
this.update(&mut cx, |this, cx| this.update_matches(matches, cx));
this.update(&mut cx, |this, cx| {
this.update_matches((search_id, did_cancel, query, matches), cx)
});
}))
}
+29 -29
View File
@@ -550,15 +550,11 @@ impl Worktree {
let (mut tree, scan_states_tx) = LocalWorktree::new(path, languages, fs.clone(), cx);
let abs_path = tree.snapshot.abs_path.clone();
let background_snapshot = tree.background_snapshot.clone();
let background = if fs.is_fake() {
cx.background().clone()
} else {
Arc::new(executor::Background::new())
};
let thread_pool = cx.thread_pool().clone();
tree._background_scanner_task = Some(cx.background().spawn(async move {
let events = fs.watch(&abs_path, Duration::from_millis(100)).await;
let scanner =
BackgroundScanner::new(background_snapshot, scan_states_tx, fs, background);
BackgroundScanner::new(background_snapshot, scan_states_tx, fs, thread_pool);
scanner.run(events).await;
}));
Worktree::Local(tree)
@@ -3017,36 +3013,40 @@ mod tests {
cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
.await;
cx.read(|cx| {
let snapshot = cx.read(|cx| {
let tree = tree.read(cx);
assert_eq!(tree.file_count(), 5);
assert_eq!(
tree.inode_for_path("fennel/grape"),
tree.inode_for_path("finnochio/grape")
);
let results = match_paths(
Some(tree.snapshot()).iter(),
"bna",
false,
false,
false,
10,
Default::default(),
cx.thread_pool().clone(),
)
.into_iter()
.map(|result| result.path)
.collect::<Vec<Arc<Path>>>();
assert_eq!(
results,
vec![
PathBuf::from("banana/carrot/date").into(),
PathBuf::from("banana/carrot/endive").into(),
]
);
})
tree.snapshot()
});
let results = cx
.read(|cx| {
match_paths(
Some(&snapshot).into_iter(),
"bna",
false,
false,
false,
10,
Default::default(),
cx.thread_pool().clone(),
)
})
.await;
assert_eq!(
results
.into_iter()
.map(|result| result.path)
.collect::<Vec<Arc<Path>>>(),
vec![
PathBuf::from("banana/carrot/date").into(),
PathBuf::from("banana/carrot/endive").into(),
]
);
}
#[gpui::test]
+8 -8
View File
@@ -1,6 +1,6 @@
use super::{char_bag::CharBag, EntryKind, Snapshot};
use crate::util;
use gpui::scoped_pool;
use gpui::executor;
use std::{
cmp::{max, min, Ordering},
path::Path,
@@ -51,7 +51,7 @@ impl Ord for PathMatch {
}
}
pub fn match_paths<'a, T>(
pub async fn match_paths<'a, T>(
snapshots: T,
query: &str,
include_root_name: bool,
@@ -59,7 +59,7 @@ pub fn match_paths<'a, T>(
smart_case: bool,
max_results: usize,
cancel_flag: Arc<AtomicBool>,
pool: scoped_pool::Pool,
pool: Arc<executor::Background>,
) -> Vec<PathMatch>
where
T: Clone + Send + Iterator<Item = &'a Snapshot> + 'a,
@@ -71,15 +71,14 @@ where
let query = &query;
let query_chars = CharBag::from(&lowercase_query[..]);
let cpus = num_cpus::get();
let path_count: usize = if include_ignored {
snapshots.clone().map(Snapshot::file_count).sum()
} else {
snapshots.clone().map(Snapshot::visible_file_count).sum()
};
let segment_size = (path_count + cpus - 1) / cpus;
let mut segment_results = (0..cpus)
let segment_size = (path_count + pool.threads() - 1) / pool.threads();
let mut segment_results = (0..pool.threads())
.map(|_| Vec::with_capacity(max_results))
.collect::<Vec<_>>();
@@ -87,7 +86,7 @@ where
for (segment_idx, results) in segment_results.iter_mut().enumerate() {
let snapshots = snapshots.clone();
let cancel_flag = &cancel_flag;
scope.execute(move || {
scope.spawn(async move {
let segment_start = segment_idx * segment_size;
let segment_end = segment_start + segment_size;
@@ -152,7 +151,8 @@ where
}
})
}
});
})
.await;
let mut results = Vec::new();
for segment_result in segment_results {