Avoid using excessive number of threads (#161)
This commit is contained in:
parent
9af874d21b
commit
0d4a9e06d6
1 changed files with 4 additions and 12 deletions
16
src/lib.rs
16
src/lib.rs
|
|
@ -209,7 +209,7 @@ pub struct Options {
|
||||||
pub use_heuristics: bool,
|
pub use_heuristics: bool,
|
||||||
/// Number of threads to use
|
/// Number of threads to use
|
||||||
///
|
///
|
||||||
/// Default: 1.5x CPU cores, rounded down
|
/// Default: number of CPU cores
|
||||||
pub threads: usize,
|
pub threads: usize,
|
||||||
|
|
||||||
/// Maximum amount of time to spend on optimizations.
|
/// Maximum amount of time to spend on optimizations.
|
||||||
|
|
@ -298,10 +298,6 @@ impl Default for Options {
|
||||||
alphas.insert(colors::AlphaOptim::NoOp);
|
alphas.insert(colors::AlphaOptim::NoOp);
|
||||||
alphas.insert(colors::AlphaOptim::Black);
|
alphas.insert(colors::AlphaOptim::Black);
|
||||||
|
|
||||||
// Default to 1 thread on single-core, otherwise use threads = 1.5x CPU cores
|
|
||||||
let num_cpus = num_cpus::get();
|
|
||||||
let thread_count = num_cpus + (num_cpus >> 1);
|
|
||||||
|
|
||||||
Options {
|
Options {
|
||||||
backup: false,
|
backup: false,
|
||||||
pretend: false,
|
pretend: false,
|
||||||
|
|
@ -325,7 +321,7 @@ impl Default for Options {
|
||||||
strip: Headers::None,
|
strip: Headers::None,
|
||||||
deflate: Deflaters::Zlib,
|
deflate: Deflaters::Zlib,
|
||||||
use_heuristics: false,
|
use_heuristics: false,
|
||||||
threads: thread_count,
|
threads: num_cpus::get(),
|
||||||
timeout: None,
|
timeout: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -335,10 +331,8 @@ impl Default for Options {
|
||||||
pub fn optimize(input: &InFile, output: &OutFile, opts: &Options) -> PngResult<()> {
|
pub fn optimize(input: &InFile, output: &OutFile, opts: &Options) -> PngResult<()> {
|
||||||
// Initialize the thread pool with correct number of threads
|
// Initialize the thread pool with correct number of threads
|
||||||
#[cfg(feature = "parallel")]
|
#[cfg(feature = "parallel")]
|
||||||
let thread_count = opts.threads;
|
|
||||||
#[cfg(feature = "parallel")]
|
|
||||||
let _ = rayon::ThreadPoolBuilder::new()
|
let _ = rayon::ThreadPoolBuilder::new()
|
||||||
.num_threads(thread_count)
|
.num_threads(opts.threads)
|
||||||
.build_global();
|
.build_global();
|
||||||
|
|
||||||
// Read in the file and try to decode as PNG.
|
// Read in the file and try to decode as PNG.
|
||||||
|
|
@ -434,10 +428,8 @@ pub fn optimize(input: &InFile, output: &OutFile, opts: &Options) -> PngResult<(
|
||||||
pub fn optimize_from_memory(data: &[u8], opts: &Options) -> PngResult<Vec<u8>> {
|
pub fn optimize_from_memory(data: &[u8], opts: &Options) -> PngResult<Vec<u8>> {
|
||||||
// Initialize the thread pool with correct number of threads
|
// Initialize the thread pool with correct number of threads
|
||||||
#[cfg(feature = "parallel")]
|
#[cfg(feature = "parallel")]
|
||||||
let thread_count = opts.threads;
|
|
||||||
#[cfg(feature = "parallel")]
|
|
||||||
let _ = rayon::ThreadPoolBuilder::new()
|
let _ = rayon::ThreadPoolBuilder::new()
|
||||||
.num_threads(thread_count)
|
.num_threads(opts.threads)
|
||||||
.build_global();
|
.build_global();
|
||||||
|
|
||||||
// Read in the file and try to decode as PNG.
|
// Read in the file and try to decode as PNG.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue