Skip to content

Commit 17d611e

Browse files
use num_cpus by default for parallelism
1 parent c9dd876 commit 17d611e

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ zstd = { version = "0.13", optional = true, default-features = false }
4848
zopfli = { version = "0.8", optional = true }
4949
deflate64 = { version = "0.1.9", optional = true }
5050
lzma-rs = { version = "0.3", default-features = false, optional = true }
51+
num_cpus = { version = "1.16", optional = true }
5152

5253
[target.'cfg(any(all(target_arch = "arm", target_pointer_width = "32"), target_arch = "mips", target_arch = "powerpc"))'.dependencies]
5354
crossbeam-utils = "0.8.20"
@@ -83,7 +84,7 @@ deflate-zopfli = ["zopfli", "_deflate-any"]
8384
lzma = ["lzma-rs/stream"]
8485
unreserved = []
8586
xz = ["lzma-rs/raw_decoder"]
86-
parallelism = ["libc"]
87+
parallelism = ["libc", "num_cpus"]
8788
default = [
8889
"aes-crypto",
8990
"bzip2",

benches/extract.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ use zip::ZipArchive;
1212
#[cfg(all(feature = "parallelism", feature = "bzip2", unix))]
1313
use zip::read::{split_extract, ExtractionParameters};
1414

15+
#[cfg(feature = "parallelism")]
16+
use num_cpus;
17+
1518
/* This archive has a set of entries repeated 20x:
1619
* - 200K random data, stored uncompressed (CompressionMethod::Stored)
1720
* - 246K text data (the project gutenberg html version of king lear)
@@ -47,9 +50,6 @@ fn extract_basic(bench: &mut Bencher) {
4750
});
4851
}
4952

50-
#[cfg(all(feature = "parallelism", feature = "bzip2", unix))]
51-
const DECOMPRESSION_THREADS: usize = 8;
52-
5353
#[cfg(all(feature = "parallelism", feature = "bzip2", unix))]
5454
fn extract_split(bench: &mut Bencher) {
5555
let readable_archive = get_test_archive().unwrap();
@@ -60,7 +60,7 @@ fn extract_split(bench: &mut Bencher) {
6060
.unwrap();
6161

6262
let params = ExtractionParameters {
63-
decompression_threads: DECOMPRESSION_THREADS,
63+
decompression_threads: num_cpus::get(),
6464
..Default::default()
6565
};
6666

src/read/pipelining.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,7 @@ pub mod handle_creation {
601601
#[cfg(unix)]
602602
pub mod split_extraction {
603603
use displaydoc::Display;
604+
use num_cpus;
604605
use thiserror::Error;
605606

606607
use std::fs;
@@ -710,7 +711,7 @@ pub mod split_extraction {
710711
pub struct ExtractionParameters {
711712
/// Number of threads used for decompression.
712713
///
713-
/// Default value: 4.
714+
/// Default value: number of available cpus via [`num_cpus::get()`].
714715
///
715716
/// Note that multiple times this many threads will be spawned by [`split_extract()`] as
716717
/// part of the pipelined process. Only this many threads will be used to perform
@@ -748,7 +749,10 @@ pub mod split_extraction {
748749
impl Default for ExtractionParameters {
749750
fn default() -> Self {
750751
Self {
751-
decompression_threads: 4,
752+
/* NB: this will perform a syscall. We still probably want to call this dynamically
753+
* instead of globally caching the call, in order to respect the dynamic value of
754+
* e.g. sched affinity. */
755+
decompression_threads: num_cpus::get(),
752756
decompression_copy_buffer_length: 1024 * 1024,
753757
file_range_copy_buffer_length: 1024 * 1024,
754758
#[cfg(not(target_os = "linux"))]

0 commit comments

Comments
 (0)