From ff7a40abb411f3b27589dc6dfd4aa7557c43fcf4 Mon Sep 17 00:00:00 2001 From: Wataru Inariba Date: Sat, 30 Sep 2023 14:39:48 +0900 Subject: [PATCH] fix: use `rayon::current_num_threads()` for multithreaded zstd encode --- Cargo.toml | 9 +++++---- src/file.rs | 10 ++++------ 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 766c444..8b7dd85 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,11 +10,12 @@ license = "AGPL-3.0-or-later" [dependencies] bincode = { version = "2.0.0-rc.3", optional = true } -once_cell = "1.17.1" -rayon = { version = "1.7.0", optional = true } -regex = "1.8.1" -zstd = { version = "0.12.3", optional = true, features = ["zstdmt"] } +once_cell = "1.18.0" +rayon = { version = "1.8.0", optional = true } +regex = "1.9.5" +zstd = { version = "0.12.4", optional = true, default-features = false } [features] default = ["bincode", "rayon"] custom-alloc = [] +rayon = ["dep:rayon", "zstd?/zstdmt"] diff --git a/src/file.rs b/src/file.rs index 90e162d..6f347c7 100644 --- a/src/file.rs +++ b/src/file.rs @@ -16,7 +16,6 @@ use bincode::{Decode, Encode}; use std::fs::File; use std::io::{BufReader, BufWriter, Read, Write}; use std::path::Path; -use std::thread::available_parallelism; const MAGIC: u32 = 0x09f15790; const VERSION: u8 = 1; @@ -88,12 +87,11 @@ pub fn save_data_into_std_write( if let Some(compression_level) = compression_level { let mut zstd_encoder = zstd::stream::Encoder::new(writer, compression_level) .map_err(|e| format!("Failed to create zstd encoder: {}", e))?; - let workers = available_parallelism() - .map_err(|e| format!("Failed to get cpu cores: {}", e))? - .get() as u32; + + #[cfg(feature = "rayon")] zstd_encoder - .multithread(workers) - .map_err(|e| format!("Failed to enable multithreaded compression: {}", e))?; + .multithread(rayon::current_num_threads() as u32) + .map_err(|e| format!("Failed to enable multithreaded zstd encoder: {}", e))?; encode_into_std_write(data, &mut zstd_encoder, "Failed to write data")?; zstd_encoder