Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: use arc str for assets key #8630

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
5 changes: 3 additions & 2 deletions crates/rspack_binding_values/src/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ pub struct JsChunk {

impl JsChunk {
pub fn from(chunk: &rspack_core::Chunk, compilation: &Compilation) -> Self {
let mut files = Vec::from_iter(chunk.files().iter().cloned());
let mut files = Vec::from_iter(chunk.files().iter().map(ToString::to_string));
files.sort_unstable();
let mut auxiliary_files = Vec::from_iter(chunk.auxiliary_files().iter().cloned());
let mut auxiliary_files =
Vec::from_iter(chunk.auxiliary_files().iter().map(ToString::to_string));
auxiliary_files.sort_unstable();

Self {
Expand Down
38 changes: 20 additions & 18 deletions crates/rspack_binding_values/src/compilation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::ptr::NonNull;

use dependencies::JsDependencies;
use entries::JsEntries;
use napi::JsString;
use napi_derive::napi;
use rspack_collections::IdentifierSet;
use rspack_core::rspack_sources::BoxSource;
Expand Down Expand Up @@ -126,7 +127,7 @@ impl JsCompilation {

for (filename, asset) in compilation.assets() {
assets.push(JsAsset {
name: filename.clone(),
name: filename.to_string(),
info: asset.info.clone().into(),
});
}
Expand All @@ -138,7 +139,7 @@ impl JsCompilation {
pub fn get_asset(&self, name: String) -> Result<Option<JsAsset>> {
let compilation = self.as_ref()?;

match compilation.assets().get(&name) {
match compilation.assets().get(name.as_str()) {
Some(asset) => Ok(Some(JsAsset {
name,
info: asset.info.clone().into(),
Expand All @@ -157,7 +158,7 @@ impl JsCompilation {

compilation
.assets()
.get(&name)
.get(name.as_str())
.and_then(|v| v.source.as_ref().map(|s| s.to_js_compat_source(env)))
.transpose()
}
Expand Down Expand Up @@ -273,7 +274,7 @@ impl JsCompilation {
let compilation = self.as_mut()?;

let source: BoxSource = source.into();
match compilation.assets_mut().entry(name) {
match compilation.assets_mut().entry(name.into()) {
std::collections::hash_map::Entry::Occupied(mut e) => e.get_mut().set_source(Some(source)),
std::collections::hash_map::Entry::Vacant(e) => {
e.insert(rspack_core::CompilationAsset::from(source));
Expand All @@ -288,31 +289,28 @@ impl JsCompilation {

compilation
.assets_mut()
.entry(name)
.entry(name.into())
.and_modify(|a| a.set_source(None));
Ok(())
}

#[napi]
pub fn get_asset_filenames(&self) -> Result<Vec<String>> {
pub fn get_asset_filenames(&self, env: Env) -> Result<Vec<JsString>> {
let compilation = self.as_ref()?;

Ok(
compilation
.assets()
.iter()
.filter(|(_, asset)| asset.get_source().is_some())
.map(|(filename, _)| filename)
.cloned()
.collect(),
)
compilation
.assets()
.iter()
.filter(|(_, asset)| asset.get_source().is_some())
.map(|(filename, _)| env.create_string(filename.as_ref()))
.collect()
}

#[napi]
pub fn has_asset(&self, name: String) -> Result<bool> {
let compilation = self.as_ref()?;

Ok(compilation.assets().contains_key(&name))
Ok(compilation.assets().contains_key(name.as_str()))
}

#[napi]
Expand All @@ -334,7 +332,7 @@ impl JsCompilation {
.module_assets
.entry(ModuleIdentifier::from(module))
.or_default()
.insert(filename);
.insert(filename.into());
Ok(())
}

Expand Down Expand Up @@ -673,7 +671,11 @@ impl JsCompilation {
.into_iter()
.map(|d| d.to_string_lossy().to_string())
.collect(),
assets: res.assets.into_iter().collect(),
assets: res
.assets
.into_iter()
.map(|asset| asset.to_string())
.collect(),
id: res.id,
};
Ok(js_result)
Expand Down
30 changes: 18 additions & 12 deletions crates/rspack_binding_values/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use rspack_napi::{
use rspack_util::itoa;
use rustc_hash::FxHashMap as HashMap;

use crate::{identifier::JsIdentifier, JsCompilation};
use crate::{identifier::JsIdentifier, JsArcStr, JsCompilation};

thread_local! {
static MODULE_DESCRIPTOR_REFS: RefCell<HashMap<Identifier, OneShotRef<JsModuleDescriptor>>> = Default::default();
Expand Down Expand Up @@ -329,7 +329,7 @@ impl From<rspack_core::StatsAsset> for JsStatsAsset {
fn from(stats: rspack_core::StatsAsset) -> Self {
Self {
r#type: stats.r#type,
name: stats.name,
name: stats.name.to_string(),
size: stats.size,
chunks: stats.chunks,
chunk_names: stats.chunk_names,
Expand Down Expand Up @@ -715,8 +715,8 @@ pub struct JsStatsSize {
#[napi(object, object_from_js = false)]
pub struct JsStatsChunk {
pub r#type: String,
pub files: Vec<String>,
pub auxiliary_files: Vec<String>,
pub files: Vec<JsArcStr>,
pub auxiliary_files: Vec<JsArcStr>,
pub id: Option<String>,
pub id_hints: Vec<String>,
pub hash: Option<String>,
Expand Down Expand Up @@ -759,8 +759,12 @@ impl TryFrom<StatsChunk<'_>> for JsStatsChunk {

Ok(JsStatsChunk {
r#type: stats.r#type.to_string(),
files: stats.files,
auxiliary_files: stats.auxiliary_files,
files: stats.files.into_iter().map(JsArcStr::new).collect(),
auxiliary_files: stats
.auxiliary_files
.into_iter()
.map(JsArcStr::new)
.collect(),
id: stats.id,
entry: stats.entry,
initial: stats.initial,
Expand Down Expand Up @@ -810,14 +814,14 @@ impl TryFrom<StatsChunk<'_>> for JsStatsChunk {

#[napi(object, object_from_js = false)]
pub struct JsStatsChunkGroupAsset {
pub name: String,
pub name: JsArcStr,
pub size: f64,
}

impl From<rspack_core::StatsChunkGroupAsset> for JsStatsChunkGroupAsset {
fn from(stats: rspack_core::StatsChunkGroupAsset) -> Self {
Self {
name: stats.name,
name: JsArcStr::new(stats.name),
size: stats.size as f64,
}
}
Expand Down Expand Up @@ -856,15 +860,17 @@ impl From<rspack_core::StatsChunkGroup> for JsStatsChunkGroup {

#[napi(object, object_from_js = false)]
pub struct JsStatsChildGroupChildAssets {
pub preload: Option<Vec<String>>,
pub prefetch: Option<Vec<String>>,
pub preload: Option<Vec<JsArcStr>>,
pub prefetch: Option<Vec<JsArcStr>>,
}

impl From<rspack_core::StatschunkGroupChildAssets> for JsStatsChildGroupChildAssets {
fn from(stats: rspack_core::StatschunkGroupChildAssets) -> Self {
Self {
preload: (!stats.preload.is_empty()).then_some(stats.preload),
prefetch: (!stats.prefetch.is_empty()).then_some(stats.prefetch),
preload: (!stats.preload.is_empty())
.then_some(stats.preload.into_iter().map(JsArcStr::new).collect()),
prefetch: (!stats.prefetch.is_empty())
.then_some(stats.prefetch.into_iter().map(JsArcStr::new).collect()),
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions crates/rspack_binding_values/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use futures::Future;
use rspack_napi::napi::threadsafe_function::{ThreadsafeFunction, ThreadsafeFunctionCallMode};
use rspack_napi::napi::{bindgen_prelude::*, Env, NapiRaw, Result};
Expand All @@ -14,3 +16,18 @@ where
});
Ok(())
}

pub struct JsArcStr(Arc<str>);

impl JsArcStr {
pub fn new(val: Arc<str>) -> Self {
Self(val)
}
}

impl ToNapiValue for JsArcStr {
unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> Result<sys::napi_value> {
let env_wrapper = Env::from(env);
ToNapiValue::to_napi_value(env, env_wrapper.create_string(val.0.as_ref())?)
}
}
17 changes: 9 additions & 8 deletions crates/rspack_core/src/chunk.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::cmp::Ordering;
use std::hash::BuildHasherDefault;
use std::sync::Arc;
use std::{fmt::Debug, hash::Hash};

use indexmap::IndexMap;
Expand Down Expand Up @@ -65,8 +66,8 @@ pub struct Chunk {
prevent_integration: bool,
groups: UkeySet<ChunkGroupUkey>,
runtime: RuntimeSpec,
files: HashSet<String>,
auxiliary_files: HashSet<String>,
files: HashSet<Arc<str>>,
auxiliary_files: HashSet<Arc<str>>,
chunk_reason: Option<String>,
rendered: bool,
}
Expand Down Expand Up @@ -156,24 +157,24 @@ impl Chunk {
self.runtime = runtime;
}

pub fn files(&self) -> &HashSet<String> {
pub fn files(&self) -> &HashSet<Arc<str>> {
&self.files
}

pub fn add_file(&mut self, file: String) {
self.files.insert(file);
pub fn add_file<T: Into<Arc<str>>>(&mut self, file: T) {
self.files.insert(file.into());
}

pub fn remove_file(&mut self, file: &str) -> bool {
self.files.remove(file)
}

pub fn auxiliary_files(&self) -> &HashSet<String> {
pub fn auxiliary_files(&self) -> &HashSet<Arc<str>> {
&self.auxiliary_files
}

pub fn add_auxiliary_file(&mut self, auxiliary_file: String) {
self.auxiliary_files.insert(auxiliary_file);
pub fn add_auxiliary_file<T: Into<Arc<str>>>(&mut self, auxiliary_file: T) {
self.auxiliary_files.insert(auxiliary_file.into());
}

pub fn remove_auxiliary_file(&mut self, auxiliary_file: &str) -> bool {
Expand Down
15 changes: 8 additions & 7 deletions crates/rspack_core/src/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ pub struct Compilation {
pub entrypoints: IndexMap<String, ChunkGroupUkey>,
pub async_entrypoints: Vec<ChunkGroupUkey>,
assets: CompilationAssets,
pub module_assets: IdentifierMap<HashSet<String>>,
pub emitted_assets: DashSet<String, BuildHasherDefault<FxHasher>>,
pub module_assets: IdentifierMap<HashSet<Arc<str>>>,
pub emitted_assets: DashSet<Arc<str>, BuildHasherDefault<FxHasher>>,
diagnostics: Vec<Diagnostic>,
logging: CompilationLogging,
pub plugin_driver: SharedPluginDriver,
Expand Down Expand Up @@ -551,7 +551,6 @@ impl Compilation {
} else {
self.global_entry.include_dependencies.push(entry_id);
}

Ok(())
}

Expand Down Expand Up @@ -584,8 +583,9 @@ impl Compilation {
Ok(())
}

pub fn emit_asset(&mut self, filename: String, asset: CompilationAsset) {
tracing::trace!("Emit asset {}", filename);
pub fn emit_asset<T: Into<Arc<str>>>(&mut self, filename: T, asset: CompilationAsset) {
let filename = filename.into();
tracing::trace!("Emit asset {}", &filename);
if let Some(mut original) = self.assets.remove(&filename)
&& let Some(original_source) = &original.source
&& let Some(asset_source) = asset.get_source()
Expand Down Expand Up @@ -628,7 +628,8 @@ impl Compilation {
}
}

pub fn rename_asset(&mut self, filename: &str, new_name: String) {
pub fn rename_asset<T: Into<Arc<str>>>(&mut self, filename: &str, new_name: T) {
let new_name = new_name.into();
if let Some(asset) = self.assets.remove(filename) {
self.assets.insert(new_name.clone(), asset);
self.chunk_by_ukey.iter_mut().for_each(|(_, chunk)| {
Expand Down Expand Up @@ -2161,7 +2162,7 @@ impl Compilation {
}
}

pub type CompilationAssets = HashMap<String, CompilationAsset>;
pub type CompilationAssets = HashMap<Arc<str>, CompilationAsset>;

#[derive(Debug, Clone)]
pub struct CompilationAsset {
Expand Down
16 changes: 10 additions & 6 deletions crates/rspack_core/src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub struct Compiler {
pub old_cache: Arc<OldCache>,
/// emitted asset versions
/// the key of HashMap is filename, the value of HashMap is version
pub emitted_asset_versions: HashMap<String, String>,
pub emitted_asset_versions: HashMap<Arc<str>, String>,
}

impl Compiler {
Expand Down Expand Up @@ -286,7 +286,7 @@ impl Compiler {
.incremental
.contains(IncrementalPasses::EMIT_ASSETS)
{
new_emitted_asset_versions.insert(filename.to_string(), asset.info.version.clone());
new_emitted_asset_versions.insert(filename.clone(), asset.info.version.clone());
}

if let Some(old_version) = self.emitted_asset_versions.get(filename) {
Expand Down Expand Up @@ -378,7 +378,10 @@ impl Compiler {

if need_write {
self.output_filesystem.write(&file_path, &content).await?;
self.compilation.emitted_assets.insert(filename.to_string());
self
.compilation
.emitted_assets
.insert(Arc::from(filename.to_string()));
}

let info = AssetEmittedInfo {
Expand Down Expand Up @@ -432,10 +435,11 @@ impl Compiler {
.iter()
.filter_map(|(filename, _version)| {
if !assets.contains_key(filename) {
let filename = filename.to_owned();
let filename = filename.clone();
Some(async {
if !clean_options.keep(filename.as_str()) {
let filename = Utf8Path::new(&self.options.output.path).join(filename);
let filename = filename;
if !clean_options.keep(filename.as_ref()) {
let filename = Utf8Path::new(&self.options.output.path).join(filename.as_ref());
let _ = self.output_filesystem.remove_file(&filename).await;
}
})
Expand Down
7 changes: 4 additions & 3 deletions crates/rspack_core/src/compiler/module_executor/execute.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::sync::Arc;
use std::{iter::once, sync::atomic::AtomicU32};

use itertools::Itertools;
Expand Down Expand Up @@ -37,7 +38,7 @@ pub struct ExecuteModuleResult {
pub missing_dependencies: HashSet<ArcPath>,
pub build_dependencies: HashSet<ArcPath>,
pub code_generated_modules: IdentifierSet,
pub assets: HashSet<String>,
pub assets: HashSet<Arc<str>>,
pub id: ExecuteModuleId,
}

Expand Down Expand Up @@ -271,8 +272,8 @@ impl Task<MakeTaskContext> for ExecuteTask {
execute_result.assets.extend(
module_assets
.values()
.flat_map(|m| m.iter().map(|i| i.to_owned()).collect_vec())
.collect::<HashSet<String>>(),
.flat_map(|m| m.iter().map(Clone::clone).collect_vec())
.collect::<HashSet<_>>(),
);
}
let executed_runtime_modules = runtime_modules
Expand Down
Loading
Loading