Replace vec-arena with slab

This commit is contained in:
Taiki Endo 2021-04-18 22:39:34 +09:00
parent 0ca774230e
commit f25cd267ac
2 changed files with 22 additions and 12 deletions

View File

@ -17,7 +17,7 @@ concurrent-queue = "1.2.2"
fastrand = "1.3.4" fastrand = "1.3.4"
futures-lite = "1.11.0" futures-lite = "1.11.0"
once_cell = "1.4.1" once_cell = "1.4.1"
vec-arena = "1.0.0" slab = "0.4.2"
[dev-dependencies] [dev-dependencies]
async-channel = "1.4.1" async-channel = "1.4.1"

View File

@ -31,7 +31,7 @@ use std::task::{Poll, Waker};
use async_task::Runnable; use async_task::Runnable;
use concurrent_queue::ConcurrentQueue; use concurrent_queue::ConcurrentQueue;
use futures_lite::{future, prelude::*}; use futures_lite::{future, prelude::*};
use vec_arena::Arena; use slab::Slab;
#[doc(no_inline)] #[doc(no_inline)]
pub use async_task::Task; pub use async_task::Task;
@ -131,10 +131,16 @@ impl<'a> Executor<'a> {
let mut active = self.state().active.lock().unwrap(); let mut active = self.state().active.lock().unwrap();
// Remove the task from the set of active tasks when the future finishes. // Remove the task from the set of active tasks when the future finishes.
let index = active.next_vacant(); let index = active.vacant_entry().key();
let state = self.state().clone(); let state = self.state().clone();
let future = async move { let future = async move {
let _guard = CallOnDrop(move || drop(state.active.lock().unwrap().remove(index))); let _guard = CallOnDrop(move || {
// TODO: use try_remove once https://github.com/tokio-rs/slab/pull/89 merged
let mut active = state.active.lock().unwrap();
if active.contains(index) {
drop(active.remove(index));
}
});
future.await future.await
}; };
@ -257,10 +263,8 @@ impl Drop for Executor<'_> {
fn drop(&mut self) { fn drop(&mut self) {
if let Some(state) = self.state.get() { if let Some(state) = self.state.get() {
let mut active = state.active.lock().unwrap(); let mut active = state.active.lock().unwrap();
for i in 0..active.capacity() { for w in active.drain() {
if let Some(w) = active.remove(i) { w.wake();
w.wake();
}
} }
drop(active); drop(active);
@ -359,10 +363,16 @@ impl<'a> LocalExecutor<'a> {
let mut active = self.inner().state().active.lock().unwrap(); let mut active = self.inner().state().active.lock().unwrap();
// Remove the task from the set of active tasks when the future finishes. // Remove the task from the set of active tasks when the future finishes.
let index = active.next_vacant(); let index = active.vacant_entry().key();
let state = self.inner().state().clone(); let state = self.inner().state().clone();
let future = async move { let future = async move {
let _guard = CallOnDrop(move || drop(state.active.lock().unwrap().remove(index))); let _guard = CallOnDrop(move || {
// TODO: use try_remove once https://github.com/tokio-rs/slab/pull/89 merged
let mut active = state.active.lock().unwrap();
if active.contains(index) {
drop(active.remove(index));
}
});
future.await future.await
}; };
@ -475,7 +485,7 @@ struct State {
sleepers: Mutex<Sleepers>, sleepers: Mutex<Sleepers>,
/// Currently active tasks. /// Currently active tasks.
active: Mutex<Arena<Waker>>, active: Mutex<Slab<Waker>>,
} }
impl State { impl State {
@ -490,7 +500,7 @@ impl State {
wakers: Vec::new(), wakers: Vec::new(),
free_ids: Vec::new(), free_ids: Vec::new(),
}), }),
active: Mutex::new(Arena::new()), active: Mutex::new(Slab::new()),
} }
} }