Crates.io | choir |
lib.rs | choir |
version | 0.7.0 |
source | src |
created_at | 2022-04-08 06:05:06.540267 |
updated_at | 2023-11-08 06:49:49.434224 |
description | Task Orchestration Framework |
homepage | |
repository | https://github.com/kvark/choir |
max_upload_size | |
id | 564045 |
size | 50,837 |
Choir is a task orchestration framework. It helps you to organize all the CPU workflow in terms of tasks.
let mut choir = choir::Choir::new();
let _worker = choir.add_worker("worker");
let task1 = choir.spawn("foo").init_dummy().run();
let mut task2 = choir.spawn("bar").init(|_| { println!("bar"); });
task2.depend_on(&task1);
task2.run().join();
What makes Choir elegant? Generally when we need to encode the semantics of "wait for dependencies", we think of some sort of a counter. Maybe an atomic, for the dependency number. When it reaches zero (or one), we schedule a task for execution. In Choir, the internal data for a task (i.e. the functor itself!) is placed in an Arc
. Whenever we are able to extract it from the Arc
(which means there are no other dependencies), we move it to a scheduling queue. I think Rust type system shows its best here.
Note: it turns out Arc
doesn't fully support such a "linear" usage as required here, and it's impossible to control where the last reference gets destructed (without logic in drop()
). For this reason, we introduce our own Linearc
to be used internally.
You can also add or remove workers at any time to balance the system load, which may be running other applications at the same time.
General workflow is about creating tasks and setting up dependencies between them. There is a few different kinds of tasks:
init()
and represented as FnOnce()
init_dummy()
, and having no function bodyFn(SubIndex)
, and initialized with init_multi()
Fn(T)
, and initialized with init_iter()
Just calling run()
is done automatically on IdleTask::drop()
if not called explicitly.
This object also allows adding dependencies before scheduling the task. The running task can be also used as a dependency for others.
Note that all tasks are pre-empted at the Fn()
execution boundary. Thus, for example, a long-running multi task will be pre-empted by any incoming single-run tasks.
Blade heavily relies on Choir for parallelizing the asset loading. See blade-asset talk at Rust Gamedev meetup for details.
Machine: MBP 2016, 3.3 GHz Dual-Core Intel Core i7
spawn()+init()
(optimized): 237nsExecuting 100k empty tasks:
With Tracy: Add this line to the start of the benchmark:
let _ = profiling::tracy_client::Client::start();
Then run in command prompt:
cargo bench --features "profiling/profile-with-tracy"