Replace easy-parallel with scoped-pool for path searches

The easy-parallel crate spawned new threads on each call, which was resulting in way too many threads.

Co-Authored-By: Brooks Swinnerton <934497+bswinnerton@users.noreply.github.com>
This commit is contained in:
Nathan Sobo
2021-04-14 09:08:52 -06:00
co-authored by Brooks Swinnerton
parent f455355c78
commit 4cef25eff8
7 changed files with 54 additions and 13 deletions
+2 -1
View File
@@ -347,8 +347,9 @@ impl FileFinder {
fn spawn_search(&mut self, query: String, ctx: &mut ViewContext<Self>) {
let worktrees = self.worktrees(ctx.as_ref());
let search_id = util::post_inc(&mut self.search_count);
let pool = ctx.app().scoped_pool().clone();
let task = ctx.background_executor().spawn(async move {
let matches = match_paths(worktrees.as_slice(), &query, false, false, 100);
let matches = match_paths(worktrees.as_slice(), &query, false, false, 100, pool);
(search_id, matches)
});
+8 -8
View File
@@ -1,4 +1,4 @@
use easy_parallel::Parallel;
use gpui::scoped_pool;
use super::char_bag::CharBag;
@@ -54,6 +54,7 @@ pub fn match_paths(
include_ignored: bool,
smart_case: bool,
max_results: usize,
pool: scoped_pool::Pool,
) -> Vec<PathMatch> {
let lowercase_query = query.to_lowercase().chars().collect::<Vec<_>>();
let query = query.chars().collect::<Vec<_>>();
@@ -68,10 +69,9 @@ pub fn match_paths(
let segment_size = (path_count + cpus - 1) / cpus;
let mut segment_results = (0..cpus).map(|_| BinaryHeap::new()).collect::<Vec<_>>();
Parallel::new()
.each(
segment_results.iter_mut().enumerate(),
|(segment_idx, results)| {
pool.scoped(|scope| {
for (segment_idx, results) in segment_results.iter_mut().enumerate() {
scope.execute(move || {
let segment_start = segment_idx * segment_size;
let segment_end = segment_start + segment_size;
@@ -115,9 +115,9 @@ pub fn match_paths(
}
tree_start = tree_end;
}
},
)
.run();
})
}
});
let mut results = segment_results
.into_iter()
+4 -2
View File
@@ -11,7 +11,7 @@ use crate::{
use anyhow::{anyhow, Result};
use crossbeam_channel as channel;
use easy_parallel::Parallel;
use gpui::{AppContext, Entity, ModelContext, ModelHandle, Task};
use gpui::{scoped_pool, AppContext, Entity, ModelContext, ModelHandle, Task};
use ignore::dir::{Ignore, IgnoreBuilder};
use parking_lot::RwLock;
use smol::prelude::*;
@@ -606,6 +606,7 @@ pub fn match_paths(
include_ignored: bool,
smart_case: bool,
max_results: usize,
pool: scoped_pool::Pool,
) -> Vec<PathMatch> {
let tree_states = trees.iter().map(|tree| tree.0.read()).collect::<Vec<_>>();
fuzzy::match_paths(
@@ -634,6 +635,7 @@ pub fn match_paths(
include_ignored,
smart_case,
max_results,
pool,
)
}
@@ -674,7 +676,7 @@ mod test {
app.read(|ctx| {
let tree = tree.read(ctx);
assert_eq!(tree.file_count(), 4);
let results = match_paths(&[tree.clone()], "bna", false, false, 10)
let results = match_paths(&[tree.clone()], "bna", false, false, 10, ctx.scoped_pool().clone())
.iter()
.map(|result| tree.entry_path(result.entry_id))
.collect::<Result<Vec<PathBuf>, _>>()