diff options
| author | jakka <jakka@jakka.su> | 2025-07-17 12:34:41 +0300 |
|---|---|---|
| committer | jakka <jakka@jakka.su> | 2025-07-17 12:34:41 +0300 |
| commit | 04eceb7e3d748c23517485d89035848cfe58a334 (patch) | |
| tree | e790351d65ee73c39e974d9107d6bea6553cd499 | |
| parent | 566783c647fc85f1cfbe214e932aeb664e1f440d (diff) | |
test threading version without rayon
| -rw-r--r-- | Cargo.lock | 52 | ||||
| -rw-r--r-- | Cargo.toml | 1 | ||||
| -rw-r--r-- | src/files.rs | 52 | ||||
| -rw-r--r-- | src/main.rs | 6 |
4 files changed, 36 insertions, 75 deletions
@@ -180,31 +180,6 @@ dependencies = [ ] [[package]] -name = "crossbeam-deque" -version = "0.8.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9dd111b7b7f7d55b72c0a6ae361660ee5853c9af73f70c3c2ef6858b950e2e51" -dependencies = [ - "crossbeam-epoch", - "crossbeam-utils", -] - -[[package]] -name = "crossbeam-epoch" -version = "0.9.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" -dependencies = [ - "crossbeam-utils", -] - -[[package]] -name = "crossbeam-utils" -version = "0.8.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" - -[[package]] name = "ctrlc" version = "3.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -236,12 +211,6 @@ dependencies = [ ] [[package]] -name = "either" -version = "1.15.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" - -[[package]] name = "encode_unicode" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -282,7 +251,6 @@ dependencies = [ "flac-bound", "indicatif", "metaflac", - "rayon", "rusqlite", "walkdir", ] @@ -472,26 +440,6 @@ dependencies = [ ] [[package]] -name = "rayon" -version = "1.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa" -dependencies = [ - "either", - "rayon-core", -] - -[[package]] -name = "rayon-core" -version = "1.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2" -dependencies = [ - "crossbeam-deque", - "crossbeam-utils", -] - -[[package]] name = "redox_users" version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -20,7 +20,6 @@ walkdir = "2.5.0" console = { version = "0.15.11", features = ["windows-console-colors"] } claxon = { git = "https://github.com/justjakka/claxon.git" } metaflac = "0.2.8" -rayon = "1.10.0" rusqlite = { version = "0.36.0", default-features = false, features = [ "modern_sqlite", ] } diff --git a/src/files.rs b/src/files.rs index 7413801..9a33e5e 100644 --- a/src/files.rs +++ b/src/files.rs @@ -1,17 +1,17 @@ use anyhow::{Result, anyhow}; #[cfg(not(test))] use indicatif::{ProgressBar, ProgressDrawTarget, ProgressStyle}; -use rayon::prelude::*; use rusqlite::Connection; use std::{ error::Error, fmt::Display, path::{Path, PathBuf}, sync::{ - Arc, Mutex, + Arc, Mutex, RwLock, atomic::{AtomicBool, Ordering}, }, - time::UNIX_EPOCH, + thread::{self, sleep}, + time::{self, UNIX_EPOCH}, }; use walkdir::WalkDir; @@ -130,7 +130,7 @@ pub fn index_files_recursively( Ok(()) } -pub fn reencode_files(conn: Connection, handler: Arc<AtomicBool>) -> Result<()> { +pub fn reencode_files(conn: Connection, handler: Arc<AtomicBool>, threads: usize) -> Result<()> { #[cfg(not(test))] let bar = ProgressBar::with_draw_target( Some(conn.get_toencode_number()?), @@ -139,33 +139,51 @@ pub fn reencode_files(conn: Connection, handler: Arc<AtomicBool>) -> Result<()> .with_style(ProgressStyle::with_template(BAR_TEMPLATE)?.progress_chars("#>-")) .with_message("Reencoding"); - let files = conn.get_toencode_files()?; + let mut files = conn.get_toencode_files()?.into_iter(); + + let thread_counter = Arc::new(RwLock::new(0_usize)); let lock = Arc::new(Mutex::new(conn)); - files.par_iter().for_each(|file| { - if handler.load(Ordering::SeqCst) { - let newhandler = handler.clone(); - match handle_encode(file, newhandler) { - Err(error) => eprintln!("{}", FileError::new(file, error)), + while handler.load(Ordering::SeqCst) { + while *thread_counter.read().unwrap() >= threads { + sleep(time::Duration::from_millis(100)); + } + + let file = match files.next() { + Some(file) => file, + None => break, + }; + + let newhandler = handler.clone(); + let newlock = lock.clone(); + #[cfg(not(test))] + let newbar = bar.clone(); + let newcounter = thread_counter.clone(); + *thread_counter.write().unwrap() += 1; + + let _ = thread::spawn(move || { + match handle_encode(&file, newhandler) { + Err(error) => eprintln!("{}", FileError::new(&file, error)), Ok(false) => { - let conn = match lock.lock() { + let conn = match newlock.lock() { Ok(conn) => conn, Err(_) => { eprintln!("Lock poisoned on file:\t{}", file.to_string_lossy()); return; } }; - if let Err(error) = conn.update_file(file) { + if let Err(error) = conn.update_file(&file) { eprintln!("{}", FileError::new(file, error)); } #[cfg(not(test))] - bar.inc(1) + newbar.inc(1) } Ok(true) => {} - } - } - }); + }; + *newcounter.write().unwrap() -= 1; + }); + } #[cfg(not(test))] { @@ -247,7 +265,7 @@ mod tests { let temp = handler.clone(); index_files_recursively(Path::new("./testfiles"), &conn, temp).unwrap(); println!("\n{}", conn.get_toencode_number().unwrap()); - reencode_files(conn, handler).unwrap(); + reencode_files(conn, handler, 4).unwrap(); let conn = Connection::new(Some(&dbname)).unwrap(); println!("\n{}", conn.get_toencode_number().unwrap()); std::fs::remove_file(dbname).unwrap(); diff --git a/src/main.rs b/src/main.rs index 122ce60..d1e4562 100644 --- a/src/main.rs +++ b/src/main.rs @@ -116,11 +116,7 @@ fn main() -> Result<()> { if args.get_flag("doit") { let hanlder = running.clone(); let threads = *args.get_one::<usize>("threads").unwrap(); - let pool = rayon::ThreadPoolBuilder::new() - .num_threads(threads) - .build()?; - - pool.install(|| files::reencode_files(conn, hanlder))?; + files::reencode_files(conn, hanlder, threads)?; } Ok::<(), anyhow::Error>(()) } |
