Initialize media estimates outside the global cache lock (#47856)

## Why

Image and audio estimate cache misses computed values while holding the cache mutex, blocking access to unrelated keys during initialization.

## What changed

Add `BlockingLruCache::get_or_init` using `Arc<OnceLock<V>>` and migrate the image and audio estimate caches to it. Initialization and same-key waits run in a Tokio blocking region after releasing the cache mutex. Callers share initialization while an entry remains cached; eviction can allow another initialization, so factories must be deterministic.

## Testing

Add a concurrency test covering access to other keys during initialization, eviction of an in-flight entry, and successful completion of both the evicted initializer and its replacement.

GitOrigin-RevId: 079226a58828b854eddc2b501079527bcbe5e603
This commit is contained in:
jif
2026-09-24 14:58:04 +00:00
committed by copyberry
parent 421082e79d
commit df06bb054c
4 changed files with 89 additions and 9 deletions
+4 -2
View File
@@ -11,7 +11,9 @@ use codex_utils_cache::sha1_digest;
use codex_utils_string::approx_token_count;
use std::io::Cursor;
use std::num::NonZeroUsize;
use std::sync::Arc;
use std::sync::LazyLock;
use std::sync::OnceLock;
use symphonia::core::formats::FormatOptions;
use symphonia::core::formats::TrackType;
use symphonia::core::formats::probe::Hint;
@@ -30,7 +32,7 @@ const MAX_PROMPT_AUDIO_BASE64_BYTES: usize = MAX_PROMPT_AUDIO_INPUT_BYTES.div_ce
const AUDIO_TOKEN_ESTIMATE_CACHE_SIZE: usize = 32;
const AUDIO_TOKENS_PER_SECOND: f64 = 10.0;
static AUDIO_TOKEN_ESTIMATE_CACHE: LazyLock<BlockingLruCache<[u8; 20], usize>> =
static AUDIO_TOKEN_ESTIMATE_CACHE: LazyLock<BlockingLruCache<[u8; 20], Arc<OnceLock<usize>>>> =
LazyLock::new(|| {
BlockingLruCache::new(
NonZeroUsize::new(AUDIO_TOKEN_ESTIMATE_CACHE_SIZE).unwrap_or(NonZeroUsize::MIN),
@@ -145,7 +147,7 @@ fn canonical_audio_mime(mime: &str) -> Option<&'static str> {
/// Estimates audio tokens from decoded duration, falling back to the data URL size.
pub fn estimate_audio_token_count(audio_url: &str) -> usize {
let key = sha1_digest(audio_url.as_bytes());
AUDIO_TOKEN_ESTIMATE_CACHE.get_or_insert_with(key, || {
AUDIO_TOKEN_ESTIMATE_CACHE.get_or_init(key, || {
let Some(duration_seconds) = audio_duration_seconds(audio_url) else {
return approx_token_count(audio_url);
};
+51
View File
@@ -0,0 +1,51 @@
//! Checks that initialization releases the global lock and survives LRU eviction.
use super::BlockingLruCache;
use std::num::NonZeroUsize;
use std::sync::mpsc;
use std::time::Duration;
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn initialization_keeps_other_keys_available_through_eviction() {
let cache = BlockingLruCache::new(NonZeroUsize::new(/*n*/ 2).expect("capacity"));
assert_eq!(cache.get_or_init("hot", || 3), 3);
let handle = tokio::runtime::Handle::current();
let (started_tx, started_rx) = mpsc::channel();
let (release_tx, release_rx) = mpsc::channel();
std::thread::scope(|scope| {
let cache = &cache;
let initializing = scope.spawn(move || {
let _entered = handle.enter();
cache.get_or_init("busy", || {
started_tx.send(()).expect("signal initialization");
release_rx
.recv_timeout(Duration::from_secs(/*secs*/ 5))
.expect("other keys should finish before initialization is released");
7
})
});
started_rx
.recv_timeout(Duration::from_secs(/*secs*/ 5))
.expect("initialization should start");
assert_eq!(cache.get_or_init("hot", || panic!("cached value")), 3);
assert_eq!(cache.get_or_init("other", || 5), 5);
assert!(cache.get(&"busy").is_none());
// The evicted initializer still owns its cell. A new resident cell can
// independently initialize the same deterministic value for this key.
assert_eq!(cache.get_or_init("busy", || 7), 7);
release_tx.send(()).expect("release initialization");
assert_eq!(initializing.join().expect("initialization thread"), 7);
assert_eq!(
cache.with_mut(|entries| {
entries
.iter()
.map(|(key, value)| (*key, *value.get().expect("initialized value")))
.collect::<Vec<_>>()
}),
vec![("busy", 7), ("other", 5)]
);
});
}
+25
View File
@@ -1,6 +1,8 @@
use std::borrow::Borrow;
use std::hash::Hash;
use std::num::NonZeroUsize;
use std::sync::Arc;
use std::sync::OnceLock;
use lru::LruCache;
use sha1::Digest;
@@ -119,6 +121,25 @@ where
}
}
impl<K, V> BlockingLruCache<K, Arc<OnceLock<V>>>
where
K: Eq + Hash,
V: Clone,
{
/// Returns the cached value, initializing it outside the global cache lock.
///
/// Concurrent callers share initialization while the entry remains cached.
/// An in-flight entry can be evicted and initialized again, so factories must
/// be deterministic. Initialization and same-key waits use a blocking region.
pub fn get_or_init(&self, key: K, value: impl FnOnce() -> V) -> V {
let entry = self.get_or_insert_with(key, || Arc::new(OnceLock::new()));
if let Some(value) = entry.get() {
return value.clone();
}
tokio::task::block_in_place(|| entry.get_or_init(value).clone())
}
}
fn lock_if_runtime<K, V>(m: &Mutex<LruCache<K, V>>) -> Option<MutexGuard<'_, LruCache<K, V>>>
where
K: Eq + Hash,
@@ -141,6 +162,10 @@ pub fn sha1_digest(bytes: &[u8]) -> [u8; 20] {
out
}
#[cfg(test)]
#[path = "initialization_tests.rs"]
mod initialization_tests;
#[cfg(test)]
mod tests {
use super::BlockingLruCache;