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-13 19:15:08 -06:00
parent 0a2d2aa684
commit 0cc5e8f742
7 changed files with 53 additions and 12 deletions

32
Cargo.lock generated
View file

@ -498,6 +498,12 @@ dependencies = [
"cfg-if 1.0.0", "cfg-if 1.0.0",
] ]
[[package]]
name = "crossbeam"
version = "0.2.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd66663db5a988098a89599d4857919b3acf7f61402e61365acfd3919857b9be"
[[package]] [[package]]
name = "crossbeam-channel" name = "crossbeam-channel"
version = "0.4.4" version = "0.4.4"
@ -924,6 +930,7 @@ dependencies = [
"rand 0.8.3", "rand 0.8.3",
"replace_with", "replace_with",
"resvg", "resvg",
"scoped-pool",
"serde", "serde",
"serde_json", "serde_json",
"simplelog", "simplelog",
@ -1065,7 +1072,7 @@ version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd96ffd135b2fd7b973ac026d28085defbe8983df057ced3eb4f2130b0831312" checksum = "dd96ffd135b2fd7b973ac026d28085defbe8983df057ced3eb4f2130b0831312"
dependencies = [ dependencies = [
"scopeguard", "scopeguard 1.1.0",
] ]
[[package]] [[package]]
@ -1674,12 +1681,29 @@ dependencies = [
"winapi-util", "winapi-util",
] ]
[[package]]
name = "scoped-pool"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "817a3a15e704545ce59ed2b5c60a5d32bda4d7869befb8b36667b658a6c00b43"
dependencies = [
"crossbeam",
"scopeguard 0.1.2",
"variance",
]
[[package]] [[package]]
name = "scoped-tls" name = "scoped-tls"
version = "1.0.0" version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea6a9290e3c9cf0f18145ef7ffa62d68ee0bf5fcd651017e586dc7fd5da448c2" checksum = "ea6a9290e3c9cf0f18145ef7ffa62d68ee0bf5fcd651017e586dc7fd5da448c2"
[[package]]
name = "scopeguard"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59a076157c1e2dc561d8de585151ee6965d910dd4dcb5dabb7ae3e83981a6c57"
[[package]] [[package]]
name = "scopeguard" name = "scopeguard"
version = "1.1.0" version = "1.1.0"
@ -2058,6 +2082,12 @@ dependencies = [
"ctor", "ctor",
] ]
[[package]]
name = "variance"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3abfc2be1fb59663871379ea884fd81de80c496f2274e021c01d6fe56cd77b05"
[[package]] [[package]]
name = "vec-arena" name = "vec-arena"
version = "1.0.0" version = "1.0.0"

View file

@ -18,6 +18,7 @@ pathfinder_geometry = "0.5"
rand = "0.8.3" rand = "0.8.3"
replace_with = "0.1.7" replace_with = "0.1.7"
resvg = "0.14" resvg = "0.14"
scoped-pool = "1.0.0"
serde = "1.0.125" serde = "1.0.125"
serde_json = "1.0.64" serde_json = "1.0.64"
smallvec = "1.6.1" smallvec = "1.6.1"

View file

@ -413,6 +413,7 @@ impl MutableAppContext {
windows: HashMap::new(), windows: HashMap::new(),
ref_counts: Arc::new(Mutex::new(RefCounts::default())), ref_counts: Arc::new(Mutex::new(RefCounts::default())),
background: Arc::new(executor::Background::new()), background: Arc::new(executor::Background::new()),
scoped_pool: scoped_pool::Pool::new(num_cpus::get()),
}, },
actions: HashMap::new(), actions: HashMap::new(),
global_actions: HashMap::new(), global_actions: HashMap::new(),
@ -1315,6 +1316,7 @@ pub struct AppContext {
windows: HashMap<usize, Window>, windows: HashMap<usize, Window>,
background: Arc<executor::Background>, background: Arc<executor::Background>,
ref_counts: Arc<Mutex<RefCounts>>, ref_counts: Arc<Mutex<RefCounts>>,
scoped_pool: scoped_pool::Pool,
} }
impl AppContext { impl AppContext {
@ -1353,6 +1355,10 @@ impl AppContext {
pub fn background_executor(&self) -> &Arc<executor::Background> { pub fn background_executor(&self) -> &Arc<executor::Background> {
&self.background &self.background
} }
pub fn scoped_pool(&self) -> &scoped_pool::Pool {
&self.scoped_pool
}
} }
impl ReadModel for AppContext { impl ReadModel for AppContext {

View file

@ -27,3 +27,4 @@ pub use presenter::{
AfterLayoutContext, Axis, DebugContext, EventContext, LayoutContext, PaintContext, AfterLayoutContext, Axis, DebugContext, EventContext, LayoutContext, PaintContext,
SizeConstraint, Vector2FExt, SizeConstraint, Vector2FExt,
}; };
pub use scoped_pool;

View file

@ -347,8 +347,9 @@ impl FileFinder {
fn spawn_search(&mut self, query: String, ctx: &mut ViewContext<Self>) { fn spawn_search(&mut self, query: String, ctx: &mut ViewContext<Self>) {
let worktrees = self.worktrees(ctx.as_ref()); let worktrees = self.worktrees(ctx.as_ref());
let search_id = util::post_inc(&mut self.search_count); 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 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) (search_id, matches)
}); });

View file

@ -1,4 +1,4 @@
use easy_parallel::Parallel; use gpui::scoped_pool;
use super::char_bag::CharBag; use super::char_bag::CharBag;
@ -54,6 +54,7 @@ pub fn match_paths(
include_ignored: bool, include_ignored: bool,
smart_case: bool, smart_case: bool,
max_results: usize, max_results: usize,
pool: scoped_pool::Pool,
) -> Vec<PathMatch> { ) -> Vec<PathMatch> {
let lowercase_query = query.to_lowercase().chars().collect::<Vec<_>>(); let lowercase_query = query.to_lowercase().chars().collect::<Vec<_>>();
let query = query.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 segment_size = (path_count + cpus - 1) / cpus;
let mut segment_results = (0..cpus).map(|_| BinaryHeap::new()).collect::<Vec<_>>(); let mut segment_results = (0..cpus).map(|_| BinaryHeap::new()).collect::<Vec<_>>();
Parallel::new() pool.scoped(|scope| {
.each( for (segment_idx, results) in segment_results.iter_mut().enumerate() {
segment_results.iter_mut().enumerate(), scope.execute(move || {
|(segment_idx, results)| {
let segment_start = segment_idx * segment_size; let segment_start = segment_idx * segment_size;
let segment_end = segment_start + segment_size; let segment_end = segment_start + segment_size;
@ -115,9 +115,9 @@ pub fn match_paths(
} }
tree_start = tree_end; tree_start = tree_end;
} }
}, })
) }
.run(); });
let mut results = segment_results let mut results = segment_results
.into_iter() .into_iter()

View file

@ -11,7 +11,7 @@ use crate::{
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use crossbeam_channel as channel; use crossbeam_channel as channel;
use easy_parallel::Parallel; 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 ignore::dir::{Ignore, IgnoreBuilder};
use parking_lot::RwLock; use parking_lot::RwLock;
use smol::prelude::*; use smol::prelude::*;
@ -606,6 +606,7 @@ pub fn match_paths(
include_ignored: bool, include_ignored: bool,
smart_case: bool, smart_case: bool,
max_results: usize, max_results: usize,
pool: scoped_pool::Pool,
) -> Vec<PathMatch> { ) -> Vec<PathMatch> {
let tree_states = trees.iter().map(|tree| tree.0.read()).collect::<Vec<_>>(); let tree_states = trees.iter().map(|tree| tree.0.read()).collect::<Vec<_>>();
fuzzy::match_paths( fuzzy::match_paths(
@ -634,6 +635,7 @@ pub fn match_paths(
include_ignored, include_ignored,
smart_case, smart_case,
max_results, max_results,
pool,
) )
} }
@ -674,7 +676,7 @@ mod test {
app.read(|ctx| { app.read(|ctx| {
let tree = tree.read(ctx); let tree = tree.read(ctx);
assert_eq!(tree.file_count(), 4); 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() .iter()
.map(|result| tree.entry_path(result.entry_id)) .map(|result| tree.entry_path(result.entry_id))
.collect::<Result<Vec<PathBuf>, _>>() .collect::<Result<Vec<PathBuf>, _>>()