From 6816dfde32f0069982728551673639a9b0b94baa Mon Sep 17 00:00:00 2001 From: Maros Hluska Date: Fri, 29 Sep 2023 22:25:11 -0700 Subject: [PATCH] Add multithreaded compression (#42) * Add multithreaded compression * Update error message --- Cargo.toml | 2 +- src/file.rs | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 85cf579..766c444 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ 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, default-features = false } +zstd = { version = "0.12.3", optional = true, features = ["zstdmt"] } [features] default = ["bincode", "rayon"] diff --git a/src/file.rs b/src/file.rs index e0b3c91..90e162d 100644 --- a/src/file.rs +++ b/src/file.rs @@ -16,6 +16,7 @@ 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; @@ -87,6 +88,13 @@ 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; + zstd_encoder + .multithread(workers) + .map_err(|e| format!("Failed to enable multithreaded compression: {}", e))?; + encode_into_std_write(data, &mut zstd_encoder, "Failed to write data")?; zstd_encoder .finish()