Skip to content

Commit

Permalink
Replace once_cell with std OnceLock
Browse files Browse the repository at this point in the history
  • Loading branch information
alexheretic committed Dec 8, 2023
1 parent 3c027fb commit f3495f4
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 35 deletions.
19 changes: 6 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
21 changes: 11 additions & 10 deletions src/ffmpeg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Vec<u8>> = OnceLock::new();

// hashing svt-av1 version means new encoder releases will avoid old cache data
static SVT_AV1_V: Lazy<Vec<u8>> = 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
Expand Down
25 changes: 14 additions & 11 deletions src/temporary.rs
Original file line number Diff line number Diff line change
@@ -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<Mutex<HashMap<PathBuf, TempKind>>> = Lazy::new(<_>::default);

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TempKind {
/// Should always be deleted at the end of the program.
Expand All @@ -19,13 +16,18 @@ pub enum TempKind {

/// Add a file as temporary so it can be deleted later.
pub fn add(file: impl Into<PathBuf>, 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<HashMap<PathBuf, TempKind>> {
static TEMPS: OnceLock<Mutex<HashMap<PathBuf, TempKind>>> = OnceLock::new();
TEMPS.get_or_init(<_>::default)
}

/// Delete all added temporary files.
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -67,23 +69,24 @@ 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);
}
}

/// Return a temporary directory that is distinct per process/run.
///
/// Configured --temp-dir is used as a parent or, if not set, the current working dir.
pub fn process_dir(conf_parent: Option<PathBuf>) -> PathBuf {
static SUBDIR: Lazy<String> = Lazy::new(|| {
static SUBDIR: OnceLock<String> = 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
});

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);
Expand Down

0 comments on commit f3495f4

Please sign in to comment.