Use the same background executor for spawning CPU intensive tasks

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
Co-Authored-By: Max Brunsfeld <max@zed.dev>
This commit is contained in:
Antonio Scandurra
2021-07-13 18:13:25 +02:00
co-authored by Nathan Sobo Max Brunsfeld
parent 6abac39eaa
commit c4e37dc47c
6 changed files with 83 additions and 101 deletions
-15
View File
@@ -123,7 +123,6 @@ impl App {
let cx = Rc::new(RefCell::new(MutableAppContext::new(
foreground,
Arc::new(executor::Background::new()),
Arc::new(executor::Background::new()),
Arc::new(platform),
Rc::new(foreground_platform),
(),
@@ -140,7 +139,6 @@ impl App {
let app = Self(Rc::new(RefCell::new(MutableAppContext::new(
foreground,
Arc::new(executor::Background::new()),
Arc::new(executor::Background::new()),
platform.clone(),
foreground_platform.clone(),
asset_source,
@@ -247,7 +245,6 @@ impl TestAppContext {
pub fn new(
foreground: Rc<executor::Foreground>,
background: Arc<executor::Background>,
thread_pool: Arc<executor::Background>,
first_entity_id: usize,
) -> Self {
let platform = Arc::new(platform::test::platform());
@@ -255,7 +252,6 @@ impl TestAppContext {
let mut cx = MutableAppContext::new(
foreground.clone(),
background,
thread_pool,
platform,
foreground_platform.clone(),
(),
@@ -594,7 +590,6 @@ impl MutableAppContext {
fn new(
foreground: Rc<executor::Foreground>,
background: Arc<executor::Background>,
thread_pool: Arc<executor::Background>,
platform: Arc<dyn platform::Platform>,
foreground_platform: Rc<dyn platform::ForegroundPlatform>,
asset_source: impl AssetSource,
@@ -612,7 +607,6 @@ impl MutableAppContext {
values: Default::default(),
ref_counts: Arc::new(Mutex::new(RefCounts::default())),
background,
thread_pool,
font_cache: Arc::new(FontCache::new(fonts)),
},
actions: HashMap::new(),
@@ -1490,7 +1484,6 @@ pub struct AppContext {
values: RwLock<HashMap<(TypeId, usize), Box<dyn Any>>>,
background: Arc<executor::Background>,
ref_counts: Arc<Mutex<RefCounts>>,
thread_pool: Arc<executor::Background>,
font_cache: Arc<FontCache>,
}
@@ -1535,10 +1528,6 @@ impl AppContext {
&self.font_cache
}
pub fn thread_pool(&self) -> &Arc<executor::Background> {
&self.thread_pool
}
pub fn value<Tag: 'static, T: 'static + Default>(&self, id: usize) -> ValueHandle<T> {
let key = (TypeId::of::<Tag>(), id);
let mut values = self.values.write();
@@ -1721,10 +1710,6 @@ impl<'a, T: Entity> ModelContext<'a, T> {
&self.app.cx.background
}
pub fn thread_pool(&self) -> &Arc<executor::Background> {
&self.app.cx.thread_pool
}
pub fn halt_stream(&mut self) {
self.halt_stream = true;
}
+3 -9
View File
@@ -34,7 +34,6 @@ pub enum Background {
Deterministic(Arc<Deterministic>),
Production {
executor: Arc<smol::Executor<'static>>,
threads: usize,
_stop: channel::Sender<()>,
},
}
@@ -324,9 +323,8 @@ impl Background {
pub fn new() -> Self {
let executor = Arc::new(Executor::new());
let stop = channel::unbounded::<()>();
let threads = num_cpus::get();
for i in 0..threads {
for i in 0..2 * num_cpus::get() {
let executor = executor.clone();
let stop = stop.1.clone();
thread::Builder::new()
@@ -337,16 +335,12 @@ impl Background {
Self::Production {
executor,
threads,
_stop: stop.0,
}
}
pub fn threads(&self) -> usize {
match self {
Self::Deterministic(_) => 1,
Self::Production { threads, .. } => *threads,
}
pub fn num_cpus(&self) -> usize {
num_cpus::get()
}
pub fn spawn<T, F>(&self, future: F) -> Task<T>