aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Schadt <kingdread@gmx.de>2023-01-17 20:20:51 +0100
committerDaniel Schadt <kingdread@gmx.de>2023-01-17 20:20:51 +0100
commit3e72b924b69d0bf3a7660f589838c14ae4f0f2f2 (patch)
tree7a5699cc9cf0c800ad8cd84268c1e36aa3c10cdd
parent088f292c1101adb15716705fd0e753de9668f9cf (diff)
downloadhittekaart-3e72b924b69d0bf3a7660f589838c14ae4f0f2f2.tar.gz
hittekaart-3e72b924b69d0bf3a7660f589838c14ae4f0f2f2.tar.bz2
hittekaart-3e72b924b69d0bf3a7660f589838c14ae4f0f2f2.zip
parallelize reading of GPX files
This is another pretty CPU bound task (parsing the XML files), so it makes sense to parallelize. We already have rayon, we already have the setting to control parallelism, so let's use it and make hittekaart fast!
-rw-r--r--src/main.rs28
1 files changed, 19 insertions, 9 deletions
diff --git a/src/main.rs b/src/main.rs
index 8727003..53a1301 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -5,14 +5,20 @@ use std::{
};
use clap::Parser;
-use color_eyre::eyre::{bail, eyre, Context, Result};
+use color_eyre::{
+ eyre::{bail, eyre, Context, Result},
+ Report,
+};
use hittekaart::{
gpx::{self, Compression},
renderer,
};
use indicatif::{MultiProgress, ProgressBar, ProgressDrawTarget, ProgressStyle};
use is_terminal::IsTerminal;
-use rayon::ThreadPoolBuilder;
+use rayon::{
+ iter::{IndexedParallelIterator, IntoParallelRefIterator, ParallelIterator},
+ ThreadPoolBuilder,
+};
#[derive(Parser, Debug, Clone)]
#[command(author, version, about)]
@@ -69,13 +75,17 @@ fn main() -> Result<()> {
bar.set_prefix("Reading GPX files");
let mut tracks = Vec::new();
- for file in &args.files {
- let compression = Compression::suggest_from_path(file)
- .ok_or_else(|| eyre!("Could not determine format for {file:?}"))?;
- let data = gpx::extract_from_file(file, compression)?;
- tracks.push(data);
- bar.inc(1);
- }
+ args.files
+ .par_iter()
+ .map(|file| {
+ let compression = Compression::suggest_from_path(file)
+ .ok_or_else(|| eyre!("Could not determine format for {file:?}"))?;
+ let data = gpx::extract_from_file(file, compression)?;
+ bar.inc(1);
+ Ok::<_, Report>(data)
+ })
+ .collect_into_vec(&mut tracks);
+ let tracks = tracks.into_iter().collect::<Result<Vec<_>>>()?;
bar.finish();
ensure_output_directory(&args.output_directory)?;