Skip to content

Commit

Permalink
Add multithreaded compression (#42)
Browse files Browse the repository at this point in the history
* Add multithreaded compression

* Update error message
  • Loading branch information
mhluska authored Sep 30, 2023
1 parent 7ba56e7 commit 6816dfd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
8 changes: 8 additions & 0 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -87,6 +88,13 @@ pub fn save_data_into_std_write<T: FileData, W: 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()
Expand Down

0 comments on commit 6816dfd

Please sign in to comment.