Skip to content

Commit 568a314

Browse files
authored
Avoid redundant lookups in the active slab when spawning new tasks (#96)
1 parent 7ffdf5b commit 568a314

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ impl<'a> Executor<'a> {
151151
let mut active = self.state().active.lock().unwrap();
152152

153153
// Remove the task from the set of active tasks when the future finishes.
154-
let index = active.vacant_entry().key();
154+
let entry = active.vacant_entry();
155+
let index = entry.key();
155156
let state = self.state().clone();
156157
let future = async move {
157158
let _guard = CallOnDrop(move || drop(state.active.lock().unwrap().try_remove(index)));
@@ -164,7 +165,7 @@ impl<'a> Executor<'a> {
164165
.propagate_panic(true)
165166
.spawn_unchecked(|()| future, self.schedule())
166167
};
167-
active.insert(runnable.waker());
168+
entry.insert(runnable.waker());
168169

169170
runnable.schedule();
170171
task
@@ -398,7 +399,8 @@ impl<'a> LocalExecutor<'a> {
398399
let mut active = self.inner().state().active.lock().unwrap();
399400

400401
// Remove the task from the set of active tasks when the future finishes.
401-
let index = active.vacant_entry().key();
402+
let entry = active.vacant_entry();
403+
let index = entry.key();
402404
let state = self.inner().state().clone();
403405
let future = async move {
404406
let _guard = CallOnDrop(move || drop(state.active.lock().unwrap().try_remove(index)));
@@ -411,7 +413,7 @@ impl<'a> LocalExecutor<'a> {
411413
.propagate_panic(true)
412414
.spawn_unchecked(|()| future, self.schedule())
413415
};
414-
active.insert(runnable.waker());
416+
entry.insert(runnable.waker());
415417

416418
runnable.schedule();
417419
task

0 commit comments

Comments
 (0)