Process files in parallel

This commit is contained in:
Andrew 2023-06-27 07:45:31 +12:00 committed by Josh Holmer
parent 31e796c4e8
commit bde1d113e4
2 changed files with 12 additions and 9 deletions

View file

@ -13,6 +13,9 @@
#![warn(clippy::range_plus_one)] #![warn(clippy::range_plus_one)]
#![allow(clippy::cognitive_complexity)] #![allow(clippy::cognitive_complexity)]
#[cfg(not(feature = "parallel"))]
mod rayon;
use clap::{value_parser, Arg, ArgAction, ArgMatches, Command}; use clap::{value_parser, Arg, ArgAction, ArgMatches, Command};
use indexmap::IndexSet; use indexmap::IndexSet;
use log::{error, warn}; use log::{error, warn};
@ -21,6 +24,7 @@ use oxipng::Options;
use oxipng::RowFilter; use oxipng::RowFilter;
use oxipng::StripChunks; use oxipng::StripChunks;
use oxipng::{InFile, OutFile}; use oxipng::{InFile, OutFile};
use rayon::prelude::*;
use std::fs::DirBuilder; use std::fs::DirBuilder;
#[cfg(feature = "zopfli")] #[cfg(feature = "zopfli")]
use std::num::NonZeroU8; use std::num::NonZeroU8;
@ -313,9 +317,8 @@ Heuristic filter selection strategies:
true, true,
); );
let mut success = false; let success = files.into_par_iter().filter(|(input, output)| {
for (input, output) in files { match oxipng::optimize(input, output, &opts) {
match oxipng::optimize(&input, &output, &opts) {
// For optimizing single files, this will return the correct exit code always. // For optimizing single files, this will return the correct exit code always.
// For recursive optimization, the correct choice is a bit subjective. // For recursive optimization, the correct choice is a bit subjective.
// We're choosing to return a 0 exit code if ANY file in the set // We're choosing to return a 0 exit code if ANY file in the set
@ -323,16 +326,15 @@ Heuristic filter selection strategies:
// The reason for this is that recursion may pick up files that are not // The reason for this is that recursion may pick up files that are not
// PNG files, and return an error for them. // PNG files, and return an error for them.
// We don't really want to return an error code for those files. // We don't really want to return an error code for those files.
Ok(_) => { Ok(_) => true,
success = true;
}
Err(e) => { Err(e) => {
error!("{}", e); error!("{}", e);
false
} }
}; }
} });
if !success { if success.count() == 0 {
exit(1); exit(1);
} }
} }

View file

@ -75,6 +75,7 @@ pub fn join<A, B>(a: impl FnOnce() -> A, b: impl FnOnce() -> B) -> (A, B) {
(a(), b()) (a(), b())
} }
#[allow(dead_code)]
pub fn spawn<A>(a: impl FnOnce() -> A) -> A { pub fn spawn<A>(a: impl FnOnce() -> A) -> A {
a() a()
} }