From 3e72b924b69d0bf3a7660f589838c14ae4f0f2f2 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Tue, 17 Jan 2023 20:20:51 +0100 Subject: 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! --- src/main.rs | 28 +++++++++++++++++++--------- 1 file 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::>>()?; bar.finish(); ensure_output_directory(&args.output_directory)?; -- cgit v1.2.3