-
-
Notifications
You must be signed in to change notification settings - Fork 12
chore: use Vec instead of Slab to store waiting wakers in WaitSet #103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,31 +15,29 @@ | |
| use std::task::Context; | ||
| use std::task::Waker; | ||
|
|
||
| use slab::Slab; | ||
|
|
||
| #[derive(Debug)] | ||
| pub(crate) struct WaitSet { | ||
| waiters: Slab<Waker>, | ||
| waiters: Vec<Waker>, | ||
| } | ||
|
|
||
| impl WaitSet { | ||
| /// Construct a new, empty wait set. | ||
| pub const fn new() -> Self { | ||
| Self { | ||
| waiters: Slab::new(), | ||
| waiters: Vec::new(), | ||
| } | ||
| } | ||
|
|
||
| /// Construct a new, empty wait set with the specified capacity. | ||
| pub fn with_capacity(capacity: usize) -> Self { | ||
| Self { | ||
| waiters: Slab::with_capacity(capacity), | ||
| waiters: Vec::with_capacity(capacity), | ||
| } | ||
| } | ||
|
|
||
| /// Drain and wake up all waiters. | ||
| pub(crate) fn wake_all(&mut self) { | ||
| for w in self.waiters.drain() { | ||
| for w in self.waiters.drain(..) { | ||
| w.wake(); | ||
| } | ||
| } | ||
|
|
@@ -51,11 +49,11 @@ impl WaitSet { | |
| pub(crate) fn register_waker(&mut self, idx: &mut Option<usize>, cx: &mut Context<'_>) { | ||
| match *idx { | ||
| None => { | ||
| let key = self.waiters.insert(cx.waker().clone()); | ||
| *idx = Some(key); | ||
| self.waiters.push(cx.waker().clone()); | ||
| *idx = Some(self.waiters.len() - 1); | ||
| } | ||
| Some(key) => { | ||
| if self.waiters.contains(key) { | ||
| if key < self.waiters.len() { | ||
| if !self.waiters[key].will_wake(cx.waker()) { | ||
| self.waiters[key] = cx.waker().clone(); | ||
| } | ||
|
Comment on lines
+56
to
59
|
||
|
|
@@ -71,8 +69,8 @@ impl WaitSet { | |
| // | ||
| // Barrier holds the lock during check and register, so the race condition | ||
| // above won't happen. | ||
| let key = self.waiters.insert(cx.waker().clone()); | ||
| *idx = Some(key); | ||
| self.waiters.push(cx.waker().clone()); | ||
| *idx = Some(self.waiters.len() - 1); | ||
| } | ||
| } | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: when take the whole range of a vec, the following code should be more efficient
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wake_allcould be called many times,std::mem::takejust replaces theVecwith an empty one so that the capacity is lost.BTW, I noticed the capacity is only set in
Barrier::new. Is this necessary?