Skip to content

Commit 346d25a

Browse files
authored
refactor(runtime): simplify RunnableQueue::run() (#485)
1 parent aa079d7 commit 346d25a

File tree

1 file changed

+24
-14
lines changed
  • compio-runtime/src/runtime

1 file changed

+24
-14
lines changed

compio-runtime/src/runtime/mod.rs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,23 +69,33 @@ impl RunnableQueue {
6969
/// SAFETY: call in the main thread
7070
pub unsafe fn run(&self, event_interval: usize) -> bool {
7171
let local_runnables = self.local_runnables.get_unchecked();
72-
for _i in 0..event_interval {
73-
let next_task = local_runnables.borrow_mut().pop_front();
74-
let has_local_task = next_task.is_some();
75-
if let Some(task) = next_task {
76-
task.run();
77-
}
78-
// Cheaper than pop.
79-
let has_sync_task = !self.sync_runnables.is_empty();
80-
if has_sync_task {
81-
if let Some(task) = self.sync_runnables.pop() {
82-
task.run();
72+
73+
for _ in 0..event_interval {
74+
let local_task = local_runnables.borrow_mut().pop_front();
75+
76+
// Perform an empty check as a fast path, since `pop()` is more expensive.
77+
let sync_task = if self.sync_runnables.is_empty() {
78+
None
79+
} else {
80+
self.sync_runnables.pop()
81+
};
82+
83+
match (local_task, sync_task) {
84+
(Some(local), Some(sync)) => {
85+
local.run();
86+
sync.run();
8387
}
84-
} else if !has_local_task {
85-
break;
88+
(Some(local), None) => {
89+
local.run();
90+
}
91+
(None, Some(sync)) => {
92+
sync.run();
93+
}
94+
(None, None) => break,
8695
}
8796
}
88-
!(local_runnables.borrow_mut().is_empty() && self.sync_runnables.is_empty())
97+
98+
!(local_runnables.borrow().is_empty() && self.sync_runnables.is_empty())
8999
}
90100
}
91101

0 commit comments

Comments
 (0)