Skip to content

Commit

Permalink
channel: Do not call current_thread_id inside iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Mar 17, 2022
1 parent 5b4d808 commit 81fda54
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions crossbeam-channel/src/waker.rs
Expand Up @@ -77,26 +77,32 @@ impl Waker {
/// Attempts to find another thread's entry, select the operation, and wake it up.
#[inline]
pub(crate) fn try_select(&mut self) -> Option<Entry> {
self.selectors
.iter()
.position(|selector| {
// Does the entry belong to a different thread?
selector.cx.thread_id() != current_thread_id()
&& selector // Try selecting this operation.
.cx
.try_select(Selected::Operation(selector.oper))
.is_ok()
&& {
// Provide the packet.
selector.cx.store_packet(selector.packet);
// Wake the thread up.
selector.cx.unpark();
true
}
})
// Remove the entry from the queue to keep it clean and improve
// performance.
.map(|pos| self.selectors.remove(pos))
if self.selectors.is_empty() {
None
} else {
let thread_id = current_thread_id();

self.selectors
.iter()
.position(|selector| {
// Does the entry belong to a different thread?
selector.cx.thread_id() != thread_id
&& selector // Try selecting this operation.
.cx
.try_select(Selected::Operation(selector.oper))
.is_ok()
&& {
// Provide the packet.
selector.cx.store_packet(selector.packet);
// Wake the thread up.
selector.cx.unpark();
true
}
})
// Remove the entry from the queue to keep it clean and improve
// performance.
.map(|pos| self.selectors.remove(pos))
}
}

/// Returns `true` if there is an entry which can be selected by the current thread.
Expand Down

0 comments on commit 81fda54

Please sign in to comment.