-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add a way to batch spawn tasks
For some workloads many tasks are spawned at a time. This requires locking and unlocking the executor's inner lock every time you spawn a task. If you spawn many tasks this can be expensive. This commit exposes a new "spawn_batch" method on both types. This method allows the user to spawn an entire set of tasks at a time. Closes #91 Signed-off-by: John Nunley <[email protected]>
- Loading branch information
Showing
4 changed files
with
244 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use async_executor::{Executor, LocalExecutor}; | ||
use futures_lite::future; | ||
|
||
#[cfg(not(miri))] | ||
const READY_COUNT: usize = 50_000; | ||
#[cfg(miri)] | ||
const READY_COUNT: usize = 505; | ||
|
||
#[test] | ||
fn spawn_many() { | ||
future::block_on(async { | ||
let ex = Executor::new(); | ||
|
||
// Spawn a lot of tasks. | ||
let mut tasks = vec![]; | ||
ex.spawn_many((0..READY_COUNT).map(future::ready), &mut tasks); | ||
|
||
// Run all of the tasks in parallel. | ||
ex.run(async move { | ||
for (i, task) in tasks.into_iter().enumerate() { | ||
assert_eq!(task.await, i); | ||
} | ||
}) | ||
.await; | ||
}); | ||
} | ||
|
||
#[test] | ||
fn spawn_many_local() { | ||
future::block_on(async { | ||
let ex = LocalExecutor::new(); | ||
|
||
// Spawn a lot of tasks. | ||
let mut tasks = vec![]; | ||
ex.spawn_many((0..READY_COUNT).map(future::ready), &mut tasks); | ||
|
||
// Run all of the tasks in parallel. | ||
ex.run(async move { | ||
for (i, task) in tasks.into_iter().enumerate() { | ||
assert_eq!(task.await, i); | ||
} | ||
}) | ||
.await; | ||
}); | ||
} |