Skip to main content

dag_exec/
graph.rs

1use std::{collections::HashMap, sync::Arc};
2
3/// Dense node identifier used internally in the compiled DAG.
4#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
5pub struct NodeId(pub usize);
6
7/// Executor configuration for the parallel scheduler.
8#[derive(Debug, Clone)]
9pub struct ExecutorConfig {
10    pub max_workers: usize,
11    pub max_in_flight: usize,
12    /// Per-worker buffered tasks (not counting the running task).
13    pub worker_queue_cap: usize,
14}
15
16impl Default for ExecutorConfig {
17    fn default() -> Self {
18        let max_workers = std::thread::available_parallelism()
19            .map(|n| n.get())
20            .unwrap_or(4);
21        Self {
22            max_workers,
23            max_in_flight: max_workers * 2,
24            worker_queue_cap: 2,
25        }
26    }
27}
28
29pub(crate) type TaskFn<O, E> = dyn Fn(&[Arc<O>]) -> Result<O, E> + Send + Sync + 'static;
30
31pub(crate) enum NodeKind<O, E> {
32    Source(Arc<O>),
33    Task(Arc<TaskFn<O, E>>),
34}
35
36pub(crate) struct Node<K, O, E> {
37    pub key: K,
38    pub deps: Vec<NodeId>,
39    pub kind: NodeKind<O, E>,
40}
41
42/// A compiled DAG: nodes indexed densely and accessible by key.
43pub struct Dag<K, O, E> {
44    pub(crate) nodes: Vec<Node<K, O, E>>,
45    pub(crate) index: HashMap<K, NodeId>,
46}