shiv 0.1.0-alpha.10

A simple modern Entity Component System
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
use ahash::HashSet;
use downcast_rs::{impl_downcast, Downcast};
use hyena::TaskPool;

use crate::{
    hash_map::HashMap,
    schedule::SystemLabelId,
    world::{World, WorldId},
};

use super::{
    IntoRunCriteria, IntoSystemDescriptor, ParallelExecutor, RunCriteria, Schedule,
    SequentialExecutor, ShouldRun, SystemContainer, SystemExecutor, SystemLabel,
};

pub trait Stage: Downcast + Send + Sync {
    fn run(&mut self, world: &mut World);
}

impl std::fmt::Debug for dyn Stage {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if let Some(system_stage) = self.downcast_ref::<SystemStage>() {
            write!(f, "{system_stage:?}")
        } else if let Some(schedule) = self.downcast_ref::<Schedule>() {
            write!(f, "{schedule:?}")
        } else {
            write!(f, "{{Custom Stage}}")
        }
    }
}

impl_downcast!(Stage);

#[derive(Debug)]
pub struct SystemStage {
    world_id: Option<WorldId>,
    executor: Box<dyn SystemExecutor>,
    run_criteria: RunCriteria,
    exclusive_systems: Vec<SystemContainer>,
    parallel_systems: Vec<SystemContainer>,
    uninitialized_exclusive: Vec<usize>,
    uninitialized_parallel: Vec<usize>,
    systems_modified: bool,
    executor_modified: bool,
}

impl SystemStage {
    pub fn new(executor: impl SystemExecutor) -> Self {
        Self {
            world_id: None,
            executor: Box::new(executor),
            run_criteria: RunCriteria::default(),
            exclusive_systems: Vec::new(),
            parallel_systems: Vec::new(),
            uninitialized_exclusive: Vec::new(),
            uninitialized_parallel: Vec::new(),
            systems_modified: true,
            executor_modified: true,
        }
    }

    pub fn sequential() -> Self {
        Self::new(SequentialExecutor)
    }

    pub fn parallel() -> Self {
        Self::new(ParallelExecutor::new())
    }

    pub fn parallel_with_task_pool(task_pool: TaskPool) -> Self {
        Self::new(ParallelExecutor::new_with_task_pool(task_pool))
    }

    pub fn add_system<Params>(&mut self, system: impl IntoSystemDescriptor<Params>) {
        let descriptor = system.into_descriptor();
        let container = SystemContainer::from_descriptor(descriptor);

        if container.system().is_exclusive() {
            let index = self.exclusive_systems.len();
            self.exclusive_systems.push(container);
            self.uninitialized_exclusive.push(index);
        } else {
            let index = self.parallel_systems.len();
            self.parallel_systems.push(container);
            self.uninitialized_parallel.push(index);
        }

        self.systems_modified = true;
    }

    #[must_use]
    pub fn with_system<Params>(mut self, system: impl IntoSystemDescriptor<Params>) -> Self {
        self.add_system(system);
        self
    }

    pub fn has_system(&self, label: impl SystemLabel) -> bool {
        let label = label.label();
        self.has_exclusive_system(&label) || self.has_parallel_system(&label)
    }

    fn has_exclusive_system(&self, label: &SystemLabelId) -> bool {
        self.exclusive_systems
            .iter()
            .any(|system| system.labels().contains(label))
    }

    fn has_parallel_system(&self, label: &SystemLabelId) -> bool {
        self.parallel_systems
            .iter()
            .any(|system| system.labels().contains(label))
    }

    pub fn set_run_criteria<Marker>(&mut self, run_criteria: impl IntoRunCriteria<Marker>) {
        self.run_criteria = run_criteria.into_run_criteria();
    }

    #[must_use]
    pub fn with_run_criteria<Marker>(mut self, run_criteria: impl IntoRunCriteria<Marker>) -> Self {
        self.set_run_criteria(run_criteria);
        self
    }

    pub fn apply_buffers(&mut self, world: &mut World) {
        for container in self.parallel_systems.iter_mut() {
            #[cfg(feature = "tracing")]
            let _guard = tracing::info_span!("apply", name = container.name()).entered();

            container.system_mut().apply(world);
        }
    }

    #[inline]
    pub fn parallel_systems(&self) -> &[SystemContainer] {
        &self.parallel_systems
    }

    fn validate_world(&mut self, world: &mut World) {
        if let Some(ref mut world_id) = self.world_id {
            if *world_id != world.id() {
                *world_id = world.id();

                self.reinitialize_systems(world);
            }
        } else {
            self.world_id = Some(world.id());
        }
    }

    fn initialize_systems(&mut self, world: &mut World) {
        for index in self.uninitialized_exclusive.drain(..) {
            let container = &mut self.exclusive_systems[index];
            container.system_mut().init(world);
        }

        for index in self.uninitialized_parallel.drain(..) {
            let container = &mut self.parallel_systems[index];
            container.system_mut().init(world);
        }
    }

    fn reinitialize_systems(&mut self, world: &mut World) {
        for container in self.exclusive_systems.iter_mut() {
            container.system_mut().init(world);
        }

        for container in self.parallel_systems.iter_mut() {
            container.system_mut().init(world);
        }

        self.uninitialized_exclusive.clear();
        self.uninitialized_parallel.clear();
    }

    fn check_change_ticks(&mut self, world: &World) {
        let change_tick = world.change_tick();

        for container in self.exclusive_systems.iter_mut() {
            container.system_mut().check_change_tick(change_tick);
        }

        for container in self.parallel_systems.iter_mut() {
            container.system_mut().check_change_tick(change_tick);
        }
    }

    fn rebuild_systems(&mut self) {
        Self::rebuild_dependency_graph(&mut self.parallel_systems);
        Self::rebuild_dependency_graph(&mut self.exclusive_systems);
    }

    fn rebuild_dependency_graph(systems: &mut Vec<SystemContainer>) {
        let mut labels = HashMap::<SystemLabelId, Vec<usize>>::default();

        for (index, container) in systems.iter().enumerate() {
            for &label in container.labels() {
                labels.entry(label).or_default().push(index);
            }
        }

        let mut graph = HashMap::<usize, HashSet<usize>>::default();

        for (index, container) in systems.iter().enumerate() {
            let dependencies = graph.entry(index).or_default();

            for &label in container.after() {
                for &dependency in labels.get(&label).unwrap_or(&Vec::new()) {
                    dependencies.insert(dependency);
                }
            }

            for &label in container.before() {
                for &dependant in labels.get(&label).unwrap_or(&Vec::new()) {
                    graph.entry(dependant).or_default().insert(index);
                }
            }
        }

        fn visit(
            node: usize,
            graph: &HashMap<usize, HashSet<usize>>,
            sorted: &mut Vec<usize>,
            current: &mut Vec<usize>,
            unvisited: &mut HashSet<usize>,
        ) -> bool {
            if current.contains(&node) {
                return true;
            } else if !unvisited.remove(&node) {
                return false;
            }

            current.push(node);

            for &dependency in graph.get(&node).unwrap() {
                if visit(dependency, graph, sorted, current, unvisited) {
                    return true;
                }
            }

            sorted.push(node);
            current.pop();

            false
        }

        let mut sorted = Vec::with_capacity(graph.len());
        let mut current = Vec::with_capacity(graph.len());
        let mut unvisited = graph.keys().copied().collect::<HashSet<_>>();

        while let Some(index) = unvisited.iter().next().copied() {
            if visit(index, &graph, &mut sorted, &mut current, &mut unvisited) {
                let names = current
                    .iter()
                    .map(|&index| systems[index].meta().name())
                    .collect::<Vec<_>>()
                    .join(", ");

                panic!(
                    "SystemStage contains a dependency cycle between systems: {}",
                    names
                );
            }
        }

        for (index, system) in systems.iter_mut().enumerate() {
            system.dependencies_mut().clear();

            for &dependency in graph.get(&index).unwrap() {
                let dependency = sorted.iter().position(|&i| i == dependency).unwrap();
                system.dependencies_mut().push(dependency);
            }
        }

        let mut temp = systems.drain(..).map(Some).collect::<Vec<_>>();

        for index in sorted {
            systems.push(temp[index].take().unwrap());
        }
    }
}

impl Stage for SystemStage {
    fn run(&mut self, world: &mut World) {
        self.validate_world(world);

        match self.run_criteria.should_run(world) {
            ShouldRun::Yes => {}
            ShouldRun::No => return,
        }

        if self.systems_modified {
            self.systems_modified = false;
            self.executor_modified = false;

            self.initialize_systems(world);
            self.rebuild_systems();

            self.executor.systems_changed(&self.parallel_systems);
        } else if self.executor_modified {
            self.executor_modified = false;

            self.executor.systems_changed(&self.parallel_systems);
        }

        for container in self.exclusive_systems.iter_mut() {
            container.run_criteria_mut().run(world);
        }

        for container in self.parallel_systems.iter_mut() {
            container.run_criteria_mut().run(world);
        }

        // SAFETY:
        // - `world` was validated earlier
        unsafe { self.executor.run_systems(&mut self.parallel_systems, world) };

        self.apply_buffers(world);

        for container in self.exclusive_systems.iter_mut() {
            if container.should_run() {
                #[cfg(feature = "tracing")]
                let guard = tracing::info_span!("system", system = container.name()).entered();
                container.system_mut().run((), world);
                #[cfg(feature = "tracing")]
                drop(guard);

                #[cfg(feature = "tracing")]
                let _guard = tracing::info_span!("apply", system = container.name()).entered();
                container.system_mut().apply(world);
            }
        }

        self.check_change_ticks(world);
    }
}

#[cfg(test)]
mod tests {
    use std::sync::atomic::{AtomicUsize, Ordering};

    use crate as shiv;
    use crate::{
        query::Query,
        schedule::{IntoSystemDescriptor, SystemLabel},
        system::ResMut,
        world::World,
    };

    use super::{Stage, SystemStage};

    #[derive(SystemLabel)]
    enum TestSystem {
        A,
        B,
        C,
    }

    fn system_a(mut counter: ResMut<u32>) {
        assert_eq!(*counter, 0);
        *counter += 1;
    }

    fn system_b(mut counter: ResMut<u32>) {
        assert_eq!(*counter, 1);
        *counter += 1;
    }

    fn system_c(mut counter: ResMut<u32>) {
        assert_eq!(*counter, 2);
        *counter += 1;
    }

    #[test]
    fn run_before() {
        let mut world = World::new();
        world.init_resource::<u32>();

        let mut stage = SystemStage::sequential();
        stage.add_system(system_b.label(TestSystem::B));
        stage.add_system(system_a.label(TestSystem::A).before(TestSystem::B));

        stage.run(&mut world);
    }

    #[test]
    fn run_after() {
        let mut world = World::new();
        world.init_resource::<u32>();

        let mut stage = SystemStage::sequential();
        stage.add_system(system_b.label(TestSystem::B).after(TestSystem::A));
        stage.add_system(system_a.label(TestSystem::A));

        stage.run(&mut world);
    }

    #[test]
    fn run_ordered() {
        let mut world = World::new();
        world.init_resource::<u32>();

        let mut stage = SystemStage::sequential();
        stage.add_system(
            system_b
                .label(TestSystem::B)
                .before(TestSystem::C)
                .after(TestSystem::A),
        );
        stage.add_system(
            system_c
                .label(TestSystem::C)
                .after(TestSystem::B)
                .after(TestSystem::A),
        );
        stage.add_system(
            system_a
                .label(TestSystem::A)
                .before(TestSystem::B)
                .before(TestSystem::C),
        );

        stage.run(&mut world);
    }

    #[test]
    #[should_panic]
    fn fail_cycle() {
        let mut world = World::new();
        world.init_resource::<u32>();

        let mut stage = SystemStage::sequential();
        stage.add_system(system_a.label(TestSystem::A).before(TestSystem::B));
        stage.add_system(system_b.label(TestSystem::B).before(TestSystem::A));

        stage.run(&mut world);
    }

    #[test]
    fn parallel() {
        static COUNTER: AtomicUsize = AtomicUsize::new(0);

        fn read(query: Query<&i32>) {
            assert!(
                COUNTER.fetch_add(1, Ordering::SeqCst) < usize::MAX,
                "read running at the same time as write",
            );

            for i in query.iter() {
                let _ = *i;
            }

            assert!(COUNTER.fetch_sub(1, Ordering::SeqCst) > 0);
        }

        fn write(mut query: Query<&mut i32>) {
            assert_eq!(
                COUNTER.swap(usize::MAX, Ordering::SeqCst),
                0,
                "write wasn't executed exclusively"
            );

            for mut i in query.iter_mut() {
                *i += 1;
            }

            assert_eq!(
                COUNTER.swap(0, Ordering::SeqCst),
                usize::MAX,
                "write wasn't executed exclusively",
            );
        }

        let mut world = World::new();
        let mut stage = SystemStage::parallel();

        for i in 0..100 {
            world.spawn().insert(i);
        }

        stage.add_system(read);
        stage.add_system(write);
        stage.add_system(read);
        stage.add_system(write);

        stage.run(&mut world);
    }
}