Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/rolldown_plugin_utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ workspace = true
anyhow = { workspace = true }
arcstr = { workspace = true }
cow-utils = { workspace = true }
dashmap = { workspace = true }
derive_more = { workspace = true }
itertools = { workspace = true }
memchr = { workspace = true }
Expand Down
22 changes: 20 additions & 2 deletions crates/rolldown_plugin_utils/src/file_to_url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::path::{Path, PathBuf};
use std::pin::Pin;
use std::sync::{Arc, LazyLock};

use dashmap::Entry;
use regex::Regex;

use rolldown_utils::base64::to_standard_base64;
Expand Down Expand Up @@ -73,10 +74,14 @@ impl FileToUrlEnv<'_> {

let cache =
self.ctx.meta().get::<AssetCache>().ok_or_else(|| anyhow::anyhow!("AssetCache missing"))?;

// Fast path: check if already cached
if let Some(cached) = cache.0.get(id.as_ref()) {
return Ok(cached.to_string());
}

// Slow path: compute the URL
// Note: We compute outside the lock to avoid holding it across await points
let file = clean_url(&id);
let content = std::fs::read(file)?;

Expand All @@ -103,8 +108,21 @@ impl FileToUrlEnv<'_> {
rolldown_utils::concat_string!("__VITE_ASSET__", reference_id, "__", postfix)
};

cache.0.insert(id.to_string(), url.clone());
Ok(url)
// Use entry API to atomically insert only if not present
// If another thread inserted while we were computing, use their value instead
let final_url = match cache.0.entry(id.to_string()) {
Entry::Occupied(entry) => {
// Another thread already inserted a value, use theirs to maintain consistency
entry.get().clone()
}
Entry::Vacant(entry) => {
// We're the first, insert our computed value
entry.insert(url.clone());
url
}
};

Ok(final_url)
}

async fn should_inline(
Expand Down
Loading