From 988945ca8917b9ddbc5f2101f6ca49342a7c2661 Mon Sep 17 00:00:00 2001 From: Jakub Trzeciak Date: Thu, 15 Feb 2024 22:41:54 +0100 Subject: add parallel file processing --- Cargo.lock | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + make | 2 ++ src/main.rs | 16 +++++++++++----- 4 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 make diff --git a/Cargo.lock b/Cargo.lock index 421d539..29fcb4f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -132,12 +132,43 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "crossbeam-deque" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "248e3bacc7dc6baa3b21e405ee045c3047101a49145e7e9eca583ab4c2ca5345" + [[package]] name = "data-encoding" version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7e962a19be5cfc3f3bf6dd8f61eb50107f356ad6270fbb3ed41476571db78be5" +[[package]] +name = "either" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11157ac094ffbdde99aa67b23417ebdd801842852b500e395a45a9c0aac03e4a" + [[package]] name = "flate2" version = "1.0.28" @@ -207,6 +238,7 @@ version = "0.1.0" dependencies = [ "clap", "lofty", + "rayon", "regex", ] @@ -243,6 +275,26 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rayon" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa7237101a77a10773db45d62004a272517633fbcc3df19d96455ede1122e051" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2" +dependencies = [ + "crossbeam-deque", + "crossbeam-utils", +] + [[package]] name = "regex" version = "1.10.3" diff --git a/Cargo.toml b/Cargo.toml index b8caa88..d5a6742 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,3 +9,4 @@ edition = "2021" lofty = "0.18.2" regex = "1.8.3" clap = { version = "4.4.18", features = ["derive"] } +rayon = "1.8.1" diff --git a/make b/make new file mode 100644 index 0000000..5f9c91f --- /dev/null +++ b/make @@ -0,0 +1,2 @@ +flamegraph: + env PERF=/usr/bin/perf flamegraph -- target/release/ogg-tagger diff --git a/src/main.rs b/src/main.rs index 0c65b30..0dc65bc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ use std::{env, fs::{self, File}, path::Path, fmt::format, os::fd::IntoRawFd}; use std::io::{Error, ErrorKind, BufReader, Read}; use lofty::{Probe, TaggedFileExt, LoftyError, TagExt, Tag, Picture, Accessor, PictureType}; use clap::Parser; +use rayon::prelude::*; #[derive(Parser, Debug)] #[command(version, about, long_about = None)] @@ -121,15 +122,20 @@ fn main() -> Result<(), Box> { println!("💡 Start scanning \"{}\" dir for ogg file", music_path.as_str()); - for entry in music_dir { - let path = entry?.path(); + + let paths: Vec<_> = music_dir.filter_map(Result::ok) + .map(|entry| entry.path()) + .filter(|path| path.extension().and_then(std::ffi::OsStr::to_str) == Some("ogg")) + .collect(); + + paths.par_iter().for_each(|path| { if path.is_file() { - // Improved error handling - if let Err(e) = tag_ogg_file(&path) { + if let Err(e) = tag_ogg_file(path) { eprintln!("🔥 Error tagging file {:?}: {}", path, e); } } - } + }); + println!("💡 Ended successfully"); Ok(()) -- cgit v1.2.3