From 4fef8a71929b989e1189736628ab9c186f23f3a5 Mon Sep 17 00:00:00 2001 From: pca006132 Date: Mon, 7 Sep 2020 16:13:08 +0800 Subject: [PATCH] libasync/executor: reduced reallocation for vector --- libasync/src/executor.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/libasync/src/executor.rs b/libasync/src/executor.rs index cfb5af4..b6904d3 100644 --- a/libasync/src/executor.rs +++ b/libasync/src/executor.rs @@ -72,6 +72,7 @@ impl Executor { pin_mut!(f); let ready = AtomicBool::new(true); let waker = wrap_waker(&ready); + let mut backup = Vec::new(); let val = loop { // advance the main task if ready.load(Ordering::Relaxed) { @@ -86,8 +87,8 @@ impl Executor { } // advance all tasks - let next_tasks = self.tasks.replace_with(|tasks| Vec::with_capacity(tasks.len())); - for mut task in next_tasks.into_iter() { + core::mem::swap(&mut *self.tasks.borrow_mut(), &mut backup); + for mut task in backup.drain(..) { // NOTE we don't need a CAS operation here because `wake` invocations that come from // interrupt handlers (the only source of 'race conditions' (!= data races)) are // "oneshot": they'll issue a `wake` and then disable themselves to not run again