From 085cd3c968de88a07251237232eb51c3d6ba26d8 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Fri, 25 Dec 2020 14:34:11 +0200 Subject: refactor: use rayon to get some parallelism --- Cargo.toml | 1 + src/bin/summain.rs | 14 ++++++++------ src/lib.rs | 4 ++-- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0f797c3..0729cc7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ edition = "2018" [dependencies] anyhow = "1" digest = "0.9" +rayon = "1.5" serde = { version = "1", features = ["derive"] } serde_yaml = "0.8" sha2 = "0.9" diff --git a/src/bin/summain.rs b/src/bin/summain.rs index 0abe2f5..20ca738 100644 --- a/src/bin/summain.rs +++ b/src/bin/summain.rs @@ -1,4 +1,5 @@ use anyhow::Context; +use rayon::prelude::*; use std::path::{Path, PathBuf}; use structopt::StructOpt; use summain::ManifestEntry; @@ -6,8 +7,11 @@ use summain::ManifestEntry; fn main() -> anyhow::Result<()> { let mut opt = Opt::from_args(); opt.pathnames[..].sort(); - for pathname in opt.pathnames { - report(&pathname).with_context(|| format!("{}", pathname.display()))? + let v: Vec> = + opt.pathnames.par_iter().map(|p| manifest(&p)).collect(); + for m in v { + let m = m?; + println!("{}", serde_yaml::to_string(&m)?); } Ok(()) } @@ -18,8 +22,6 @@ struct Opt { pathnames: Vec, } -fn report(pathname: &Path) -> anyhow::Result<()> { - let e = ManifestEntry::new(pathname)?; - println!("{}", serde_yaml::to_string(&e)?); - Ok(()) +fn manifest(path: &Path) -> anyhow::Result { + ManifestEntry::new(path).with_context(|| format!("{}", path.display())) } diff --git a/src/lib.rs b/src/lib.rs index 7db1115..3c90156 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -42,7 +42,7 @@ const BUF_SIZE: usize = 1024 * 1024; /// An entry in a file manifest. #[derive(Serialize, Debug)] pub struct ManifestEntry { - path: PathBuf, + path: String, #[serde(with = "mode")] mode: u32, mtime: i64, @@ -73,7 +73,7 @@ impl ManifestEntry { None }; Ok(Self { - path: path.to_path_buf(), + path: path.to_string_lossy().into_owned(), mode: m.st_mode(), mtime: m.st_mtime(), mtime_nsec: m.st_mtime_nsec(), -- cgit v1.2.1