| @@ -942,22 +942,54 @@ fn write_if_changed(path: &Path, contents: &str) -> std::io::Result<()> { |
| 942 | 942 | } |
| 943 | 943 | |
| 944 | 944 | /// `f` of each job, in order, running up to one job per core at a time. |
| 945 | | −fn in_parallel<J: Send, R: Send>(jobs: Vec<J>, f: impl Fn(J) -> R + Sync) -> Vec<R> { |
| 946 | | − let width = std::thread::available_parallelism().map_or(1, |n| n.get()); |
| 947 | | − let mut results = Vec::with_capacity(jobs.len()); |
| 948 | | − let mut jobs = jobs.into_iter().peekable(); |
| 949 | | − while jobs.peek().is_some() { |
| 950 | | − let batch: Vec<J> = jobs.by_ref().take(width).collect(); |
| 951 | | − std::thread::scope(|scope| { |
| 952 | | − let running: Vec<_> = batch.into_iter().map(|j| scope.spawn(|| f(j))).collect(); |
| 953 | | − results.extend( |
| 954 | | − running |
| 955 | | − .into_iter() |
| 956 | | − .map(|r| r.join().expect("a compiler thread panicked")), |
| 957 | | − ); |
| 958 | | − }); |
| 959 | | − } |
| 945 | +pub fn in_parallel<J: Send, R: Send>(jobs: Vec<J>, f: impl Fn(J) -> R + Sync) -> Vec<R> { |
| 946 | + let mut results: Vec<Option<R>> = std::iter::repeat_with(|| None).take(jobs.len()).collect(); |
| 947 | + each_in_parallel(jobs, cores(), f, |index, result| { |
| 948 | + results[index] = Some(result) |
| 949 | + }); |
| 960 | 950 | results |
| 951 | + .into_iter() |
| 952 | + .map(|r| r.expect("every job ran")) |
| 953 | + .collect() |
| 954 | +} |
| 955 | + |
| 956 | +/// Run `f` on every job, up to `width` at a time: a worker that finishes |
| 957 | +/// takes the next job, so a slow job holds no other back (issue #316). |
| 958 | +/// `done` sees each result on the calling thread as it comes, with its |
| 959 | +/// job's index; a panic in `f` is the caller's once the others are done. |
| 960 | +pub fn each_in_parallel<J: Send, R: Send>( |
| 961 | + jobs: Vec<J>, |
| 962 | + width: usize, |
| 963 | + f: impl Fn(J) -> R + Sync, |
| 964 | + mut done: impl FnMut(usize, R), |
| 965 | +) { |
| 966 | + let width = width.clamp(1, jobs.len().max(1)); |
| 967 | + let queue = std::sync::Mutex::new(jobs.into_iter().enumerate()); |
| 968 | + let (sender, results) = std::sync::mpsc::channel(); |
| 969 | + std::thread::scope(|scope| { |
| 970 | + for _ in 0..width { |
| 971 | + let sender = sender.clone(); |
| 972 | + let (queue, f) = (&queue, &f); |
| 973 | + scope.spawn(move || { |
| 974 | + loop { |
| 975 | + let next = queue.lock().unwrap_or_else(|e| e.into_inner()).next(); |
| 976 | + let Some((index, job)) = next else { break }; |
| 977 | + if sender.send((index, f(job))).is_err() { |
| 978 | + break; |
| 979 | + } |
| 980 | + } |
| 981 | + }); |
| 982 | + } |
| 983 | + drop(sender); |
| 984 | + for (index, result) in results { |
| 985 | + done(index, result); |
| 986 | + } |
| 987 | + }); |
| 988 | +} |
| 989 | + |
| 990 | +/// The cores this machine has, at least one. |
| 991 | +pub fn cores() -> usize { |
| 992 | + std::thread::available_parallelism().map_or(1, |n| n.get()) |
| 961 | 993 | } |
| 962 | 994 | |
| 963 | 995 | /// Run the C compiler or linker; on failure, its output. A failure |
| @@ -1101,6 +1133,36 @@ mod tests { |
| 1101 | 1133 | use std::sync::atomic::{AtomicUsize, Ordering}; |
| 1102 | 1134 | |
| 1103 | 1135 | #[test] |
| 1136 | + fn parallel_jobs_keep_their_order_and_run_once() { |
| 1137 | + let runs = AtomicUsize::new(0); |
| 1138 | + let results = in_parallel((0..100).collect(), |n: u32| { |
| 1139 | + runs.fetch_add(1, Ordering::Relaxed); |
| 1140 | + n * 2 |
| 1141 | + }); |
| 1142 | + assert_eq!(results, (0..100).map(|n| n * 2).collect::<Vec<_>>()); |
| 1143 | + assert_eq!(runs.load(Ordering::Relaxed), 100); |
| 1144 | + assert!(in_parallel(Vec::<u32>::new(), |n| n).is_empty()); |
| 1145 | + } |
| 1146 | + |
| 1147 | + /// A slow job holds no other back (issue #316): batches used to wait |
| 1148 | + /// for their slowest job before the next batch started. |
| 1149 | + #[test] |
| 1150 | + fn a_slow_job_does_not_hold_the_others_back() { |
| 1151 | + let mut finished = Vec::new(); |
| 1152 | + let jobs: Vec<u64> = std::iter::once(400) |
| 1153 | + .chain(std::iter::repeat_n(5, 20)) |
| 1154 | + .collect(); |
| 1155 | + each_in_parallel( |
| 1156 | + jobs, |
| 1157 | + 2, |
| 1158 | + |ms| std::thread::sleep(std::time::Duration::from_millis(ms)), |
| 1159 | + |index, ()| finished.push(index), |
| 1160 | + ); |
| 1161 | + assert_eq!(finished.len(), 21); |
| 1162 | + assert_eq!(finished.last(), Some(&0), "{finished:?}"); |
| 1163 | + } |
| 1164 | + |
| 1165 | + #[test] |
| 1104 | 1166 | fn dependency_lists_are_parsed() { |
| 1105 | 1167 | let rule = "out/a.o: src/a.c src/my\\ dir/b.h \\\n /usr/include/c.h\n"; |
| 1106 | 1168 | assert_eq!( |