Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't override global rayon pool #211

Merged
merged 2 commits into from
Apr 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ bit-vec = "^0.6.0"
byteorder = "^1.0.0"
crc = "^1.2.0"
itertools = "^0.9.0"
num_cpus = "^1.13.0"
zopfli = "^0.4.0"
miniz_oxide = "0.3"
rgb = "0.8.11"
Expand Down
18 changes: 0 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#![warn(clippy::range_plus_one)]
#![allow(clippy::cognitive_complexity)]

use num_cpus;
#[cfg(feature = "parallel")]
extern crate rayon;
#[cfg(not(feature = "parallel"))]
Expand Down Expand Up @@ -214,10 +213,6 @@ pub struct Options {
///
/// Default: `false`
pub use_heuristics: bool,
/// Number of threads to use
///
/// Default: number of CPU cores
pub threads: usize,

/// Maximum amount of time to spend on optimizations.
/// Further potential optimizations are skipped if the timeout is exceeded.
Expand Down Expand Up @@ -323,20 +318,13 @@ impl Default for Options {
strip: Headers::None,
deflate: Deflaters::Zlib,
use_heuristics: false,
threads: num_cpus::get(),
timeout: None,
}
}
}

/// Perform optimization on the input file using the options provided
pub fn optimize(input: &InFile, output: &OutFile, opts: &Options) -> PngResult<()> {
// Initialize the thread pool with correct number of threads
#[cfg(feature = "parallel")]
let _ = rayon::ThreadPoolBuilder::new()
.num_threads(opts.threads)
.build_global();

// Read in the file and try to decode as PNG.
if opts.verbosity.is_some() {
eprintln!("Processing: {}", input);
Expand Down Expand Up @@ -431,12 +419,6 @@ pub fn optimize(input: &InFile, output: &OutFile, opts: &Options) -> PngResult<(
/// Perform optimization on the input file using the options provided, where the file is already
/// loaded in-memory
pub fn optimize_from_memory(data: &[u8], opts: &Options) -> PngResult<Vec<u8>> {
// Initialize the thread pool with correct number of threads
#[cfg(feature = "parallel")]
let _ = rayon::ThreadPoolBuilder::new()
.num_threads(opts.threads)
.build_global();

// Read in the file and try to decode as PNG.
if opts.verbosity.is_some() {
eprintln!("Processing from memory");
Expand Down
7 changes: 6 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,12 @@ fn parse_opts_into_struct(
}

if let Some(x) = matches.value_of("threads") {
opts.threads = x.parse::<usize>().unwrap();
let threads = x.parse::<usize>().unwrap();

rayon::ThreadPoolBuilder::new()
.num_threads(threads)
.build_global()
.map_err(|err| err.to_string())?;
}

Ok((out_file, out_dir, opts))
Expand Down