remove rayon, manual parallelization

This commit is contained in:
132nd-Professor
2022-08-12 20:02:44 +02:00
parent 8c46e2bbcc
commit 5eccad6517
3 changed files with 27 additions and 17 deletions
-1
View File
@@ -6,7 +6,6 @@ edition = "2021"
[dependencies] [dependencies]
zip = {version = ">= 0.6.2", default-features = false, features = ["deflate"]} zip = {version = ">= 0.6.2", default-features = false, features = ["deflate"]}
rayon = "*"
[profile.release] [profile.release]
lto = true lto = true
+21 -11
View File
@@ -4,9 +4,8 @@ mod reader;
mod tacview; mod tacview;
mod writer; mod writer;
use rayon::prelude::*;
use std::process;
use std::sync::Arc; use std::sync::Arc;
use std::{process, thread};
use crate::{tacview::Coalition, writer::StringWriter}; use crate::{tacview::Coalition, writer::StringWriter};
@@ -22,22 +21,33 @@ fn main_inner() -> Result<(), Box<dyn std::error::Error>> {
println!("Processing {}", input_filename); println!("Processing {}", input_filename);
let lines = reader::read_data(&input_filename, is_zip)?; let lines = reader::read_data(&input_filename, is_zip)?;
let (header, body) = processor::split_into_header_and_body(&lines)?; let (header, body) = processor::split_into_header_and_body(lines)?;
let coalition_per_line = Arc::new(processor::divide_body_by_coalition(&body)?);
let coalition_per_line = Arc::new(processor::divide_body_by_coalition(body)?);
let header = Arc::new(header); let header = Arc::new(header);
let body = Arc::new(body); let body = Arc::new(body);
let input_filename = Arc::new(input_filename);
let _: Vec<_> = vec![Coalition::Blue, Coalition::Red, Coalition::Purple] let handles: Vec<_> = vec![Coalition::Blue, Coalition::Red, Coalition::Purple]
.into_par_iter() .into_iter()
.map(|c| { .map(|c| {
let mut writer = writer::create_writer(is_zip, &input_filename.clone(), &c).unwrap(); let z = is_zip;
writer.write_strings(&header).unwrap(); let b = body.clone();
writer let coalitions = coalition_per_line.clone();
.write_for_coalition(&body, &coalition_per_line.clone(), c) let h = header.clone();
.unwrap(); let i = input_filename.clone();
thread::spawn(move || {
let mut writer = writer::create_writer(z, &*i.clone(), &c).unwrap();
writer.write_strings(&*h).unwrap();
writer.write_for_coalition(&*b, &*coalitions, c).unwrap();
})
}) })
.collect(); .collect();
for handle in handles {
handle.join().unwrap();
}
Ok(()) Ok(())
} }
+6 -5
View File
@@ -7,12 +7,12 @@ use crate::tacview::{Coalition, CoalitionIDs};
const COMMENT: char = '#'; const COMMENT: char = '#';
const MINUS: char = '-'; const MINUS: char = '-';
pub fn split_into_header_and_body<S>(lines: &[S]) -> Result<(&[S], &[S]), ProcessingError> pub fn split_into_header_and_body<S>(mut lines: Vec<S>) -> Result<(Vec<S>, Vec<S>), ProcessingError>
where where
S: AsRef<str>, S: AsRef<str>,
{ {
let mut i = 0; let mut split_idx = 0;
for line in lines { for line in &lines {
if line if line
.as_ref() .as_ref()
.chars() .chars()
@@ -22,9 +22,10 @@ where
{ {
break; break;
} }
i += 1; split_idx += 1;
} }
Ok((&lines[..i], &lines[i..])) let body = lines.split_off(split_idx);
Ok((lines, body))
} }
pub fn divide_body_by_coalition<S>(body: &[S]) -> Result<Vec<Coalition>, Box<dyn Error>> pub fn divide_body_by_coalition<S>(body: &[S]) -> Result<Vec<Coalition>, Box<dyn Error>>