From b09f804a3f190187c26cd6d7cee5cca8df6c43d9 Mon Sep 17 00:00:00 2001 From: Wim Pomp Date: Thu, 15 May 2025 13:34:31 +0200 Subject: [PATCH] - flexible number of threads - path: &str -> path: AsRef --- Cargo.toml | 8 ++++---- src/lib.rs | 10 ++++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c1ba5b1..cfd2e07 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tiffwrite" -version = "2025.3.1" +version = "2025.5.0" edition = "2021" authors = ["Wim Pomp "] license = "MIT" @@ -16,9 +16,9 @@ name = "tiffwrite" crate-type = ["cdylib", "rlib"] [dependencies] -anyhow = "1.0.97" -chrono = "0.4.40" -flate2 = "1.1.0" +anyhow = "1.0.98" +chrono = "0.4.41" +flate2 = "1.1.1" ndarray = "0.16.1" num = "0.4.3" rayon = "1.10.0" diff --git a/src/lib.rs b/src/lib.rs index e1a955f..f18921e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,11 +11,12 @@ use std::collections::HashSet; use std::fs::{File, OpenOptions}; use std::hash::{DefaultHasher, Hash, Hasher}; use std::io::{Read, Seek, SeekFrom, Write}; +use std::path::Path; use std::time::Duration; use std::{cmp::Ordering, collections::HashMap}; use std::{ thread, - thread::{sleep, JoinHandle}, + thread::{available_parallelism, sleep, JoinHandle}, }; use zstd::zstd_safe::CompressionLevel; use zstd::{stream::Encoder, DEFAULT_COMPRESSION_LEVEL}; @@ -672,9 +673,9 @@ impl Drop for IJTiffFile { } impl IJTiffFile { - /// create new tifffile from path string, use it's save() method to save frames + /// create new tifffile from path, use it's save() method to save frames /// the file is finalized when it goes out of scope - pub fn new(path: &str) -> Result { + pub fn new>(path: P) -> Result { let mut file = OpenOptions::new() .create(true) .truncate(true) @@ -806,9 +807,10 @@ impl IJTiffFile { A: AsArray<'a, T, Ix2>, T: Bytes + Clone + Send + Sync + 'static, { + let n_threads = usize::from(available_parallelism()?); loop { self.collect_threads(false)?; - if self.threads.len() < 48 { + if self.threads.len() < n_threads { break; } sleep(Duration::from_millis(100));