| @@ -1,8 +1,9 @@ |
| 1 | 1 | //! Helpers for the compiler's integration tests, so each harness does not |
| 2 | 2 | //! repeat them: the pipeline from source to Core ([`compile`]), the |
| 3 | 3 | //! `tests/run` programs ([`run_programs`]) and their expectations |
| 4 | | −//! ([`Expectations`], format in `tests/README.md`), and the runtime |
| 5 | | −//! library native tests link ([`build_runtime`]). |
| 4 | +//! ([`Expectations`], format in `tests/README.md`), the runtime |
| 5 | +//! library native tests link ([`build_runtime`]), and a loop over them on |
| 6 | +//! every core ([`par_map`]). |
| 6 | 7 | //! |
| 7 | 8 | //! Only a dev-dependency: nothing here is part of the compiler. |
| 8 | 9 | |
| @@ -79,6 +80,45 @@ pub fn run_programs() -> Vec<PathBuf> { |
| 79 | 80 | programs("run") |
| 80 | 81 | } |
| 81 | 82 | |
| 83 | +/// `f` on every item, on every core, the results in `items`' order: a test |
| 84 | +/// that compiles each `tests/run` program one after another waits on one |
| 85 | +/// core for minutes (louise_perceus' was half of CI's unit tests, |
| 86 | +/// gabla108/toboggan#147). Each worker gets `stack` bytes (the compiler |
| 87 | +/// recurses once per statement: give it the CLI's room) and takes the next |
| 88 | +/// item from one counter, as `louise_db`'s `parse_all` does, so a slow |
| 89 | +/// program never holds back a queue. A panic in `f` is raised again here. |
| 90 | +pub fn par_map<I: Sync, T: Send>(items: &[I], stack: usize, f: impl Fn(&I) -> T + Sync) -> Vec<T> { |
| 91 | + use std::sync::atomic::{AtomicUsize, Ordering}; |
| 92 | + let threads = std::thread::available_parallelism() |
| 93 | + .map_or(1, |n| n.get()) |
| 94 | + .min(items.len()) |
| 95 | + .max(1); |
| 96 | + let next = AtomicUsize::new(0); |
| 97 | + let mut done: Vec<(usize, T)> = std::thread::scope(|scope| { |
| 98 | + let workers: Vec<_> = (0..threads) |
| 99 | + .map(|_| { |
| 100 | + std::thread::Builder::new() |
| 101 | + .stack_size(stack) |
| 102 | + .spawn_scoped(scope, || { |
| 103 | + let mut done = Vec::new(); |
| 104 | + loop { |
| 105 | + let i = next.fetch_add(1, Ordering::Relaxed); |
| 106 | + let Some(item) = items.get(i) else { break done }; |
| 107 | + done.push((i, f(item))); |
| 108 | + } |
| 109 | + }) |
| 110 | + .expect("spawn a test worker") |
| 111 | + }) |
| 112 | + .collect(); |
| 113 | + workers |
| 114 | + .into_iter() |
| 115 | + .flat_map(|w| w.join().unwrap_or_else(|p| std::panic::resume_unwind(p))) |
| 116 | + .collect() |
| 117 | + }); |
| 118 | + done.sort_by_key(|(i, _)| *i); |
| 119 | + done.into_iter().map(|(_, t)| t).collect() |
| 120 | +} |
| 121 | + |
| 82 | 122 | /// The programs (`.lse` files) of `tests/<dir>`, in name order. |
| 83 | 123 | pub fn programs(dir: &str) -> Vec<PathBuf> { |
| 84 | 124 | let mut files: Vec<PathBuf> = std::fs::read_dir(tests_dir(dir)) |