From f3495f44d8e98026cf3a532ea08f12e99fb4f898 Mon Sep 17 00:00:00 2001 From: Alex Butler Date: Fri, 8 Dec 2023 14:00:16 +0000 Subject: [PATCH] Replace once_cell with std OnceLock --- Cargo.lock | 19 ++++++------------- Cargo.toml | 1 - src/ffmpeg.rs | 21 +++++++++++---------- src/temporary.rs | 25 ++++++++++++++----------- 4 files changed, 31 insertions(+), 35 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2fa118c..2606fa2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -18,7 +18,6 @@ dependencies = [ "humantime", "indicatif", "infer", - "once_cell", "serde", "serde_json", "shell-escape", @@ -621,9 +620,9 @@ dependencies = [ [[package]] name = "mio" -version = "0.8.9" +version = "0.8.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3dce281c5e46beae905d4de1870d8b1509a9142b62eedf18b443b011ca8343d0" +checksum = "8f3d0b296e374a4e6f3c7b0a1f5a51d748a0d34c85e7dc48fc3fa9a87657fe09" dependencies = [ "libc", "wasi", @@ -655,12 +654,6 @@ dependencies = [ "memchr", ] -[[package]] -name = "once_cell" -version = "1.18.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" - [[package]] name = "option-ext" version = "0.2.0" @@ -706,9 +699,9 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" [[package]] name = "portable-atomic" -version = "1.5.1" +version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3bccab0e7fd7cc19f820a1c8c91720af652d0c88dc9664dd72aef2614f04af3b" +checksum = "7170ef9988bc169ba16dd36a7fa041e5c4cbeb6a35b76d4c03daded371eae7c0" [[package]] name = "powerfmt" @@ -771,9 +764,9 @@ checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" [[package]] name = "rustix" -version = "0.38.26" +version = "0.38.27" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9470c4bf8246c8daf25f9598dca807fb6510347b1e1cfa55749113850c79d88a" +checksum = "bfeae074e687625746172d639330f1de242a178bf3189b51e35a7a21573513ac" dependencies = [ "bitflags 2.4.1", "errno 0.3.8", diff --git a/Cargo.toml b/Cargo.toml index cb7f60b..be94dd6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,7 +22,6 @@ futures = "0.3.19" humantime = "2.1" indicatif = "0.17" infer = { version = "0.15", default-features = false } -once_cell = "1.9" serde = { version = "1.0.185", features = ["derive"] } serde_json = "1.0.105" shell-escape = "0.1.5" diff --git a/src/ffmpeg.rs b/src/ffmpeg.rs index fcb9a11..e09d3bd 100644 --- a/src/ffmpeg.rs +++ b/src/ffmpeg.rs @@ -6,13 +6,12 @@ use crate::{ temporary::{self, TempKind}, }; use anyhow::Context; -use once_cell::sync::Lazy; use std::{ collections::HashSet, hash::{Hash, Hasher}, path::{Path, PathBuf}, process::Stdio, - sync::Arc, + sync::{Arc, OnceLock}, }; use tokio::process::Command; use tokio_stream::Stream; @@ -33,16 +32,18 @@ pub struct FfmpegEncodeArgs<'a> { impl FfmpegEncodeArgs<'_> { pub fn sample_encode_hash(&self, state: &mut impl Hasher) { + static SVT_AV1_V: OnceLock> = OnceLock::new(); + // hashing svt-av1 version means new encoder releases will avoid old cache data - static SVT_AV1_V: Lazy> = Lazy::new(|| { - use std::process::Command; - match Command::new("SvtAv1EncApp").arg("--version").output() { - Ok(out) => out.stdout, - _ => <_>::default(), - } - }); if &*self.vcodec == "libsvtav1" { - SVT_AV1_V.hash(state); + let svtav1_verion = SVT_AV1_V.get_or_init(|| { + use std::process::Command; + match Command::new("SvtAv1EncApp").arg("--version").output() { + Ok(out) => out.stdout, + _ => <_>::default(), + } + }); + svtav1_verion.hash(state); } // input not relevant to sample encoding diff --git a/src/temporary.rs b/src/temporary.rs index eea2306..7b78560 100644 --- a/src/temporary.rs +++ b/src/temporary.rs @@ -1,14 +1,11 @@ //! temp file logic -use once_cell::sync::Lazy; use std::{ collections::HashMap, env, iter, path::{Path, PathBuf}, - sync::Mutex, + sync::{Mutex, OnceLock}, }; -static TEMPS: Lazy>> = Lazy::new(<_>::default); - #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum TempKind { /// Should always be deleted at the end of the program. @@ -19,13 +16,18 @@ pub enum TempKind { /// Add a file as temporary so it can be deleted later. pub fn add(file: impl Into, kind: TempKind) { - TEMPS.lock().unwrap().insert(file.into(), kind); + temp_files().lock().unwrap().insert(file.into(), kind); } /// Remove a previously added file so that it won't be deleted later, /// if it hasn't already. pub fn unadd(file: &Path) -> bool { - TEMPS.lock().unwrap().remove(file).is_some() + temp_files().lock().unwrap().remove(file).is_some() +} + +fn temp_files() -> &'static Mutex> { + static TEMPS: OnceLock>> = OnceLock::new(); + TEMPS.get_or_init(<_>::default) } /// Delete all added temporary files. @@ -39,7 +41,7 @@ pub async fn clean(keep_keepables: bool) { /// Delete all added temporary files. pub async fn clean_all() { - let mut files: Vec<_> = std::mem::take(&mut *TEMPS.lock().unwrap()) + let mut files: Vec<_> = std::mem::take(&mut *temp_files().lock().unwrap()) .into_keys() .collect(); files.sort_by_key(|f| f.is_dir()); // rm dir at the end @@ -53,7 +55,7 @@ pub async fn clean_all() { } async fn clean_non_keepables() { - let mut matching: Vec<_> = TEMPS + let mut matching: Vec<_> = temp_files() .lock() .unwrap() .iter() @@ -67,7 +69,7 @@ async fn clean_non_keepables() { true => _ = tokio::fs::remove_dir(&file).await, false => _ = tokio::fs::remove_file(&file).await, } - TEMPS.lock().unwrap().remove(&file); + temp_files().lock().unwrap().remove(&file); } } @@ -75,7 +77,8 @@ async fn clean_non_keepables() { /// /// Configured --temp-dir is used as a parent or, if not set, the current working dir. pub fn process_dir(conf_parent: Option) -> PathBuf { - static SUBDIR: Lazy = Lazy::new(|| { + static SUBDIR: OnceLock = OnceLock::new(); + let subdir = SUBDIR.get_or_init(|| { let mut subdir = String::from(".ab-av1-"); subdir.extend(iter::repeat_with(fastrand::alphanumeric).take(12)); subdir @@ -83,7 +86,7 @@ pub fn process_dir(conf_parent: Option) -> PathBuf { let mut temp_dir = conf_parent.unwrap_or_else(|| env::current_dir().expect("current working directory")); - temp_dir.push(&*SUBDIR); + temp_dir.push(subdir); if !temp_dir.exists() { add(&temp_dir, TempKind::Keepable);