Skip to content

Commit

Permalink
perf: use arc str for assets
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind committed Dec 5, 2024
1 parent 24f6004 commit dca7627
Show file tree
Hide file tree
Showing 20 changed files with 141 additions and 84 deletions.
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())?)
}
}
25 changes: 24 additions & 1 deletion crates/rspack_core/src/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ pub struct Compilation {
pub async_entrypoints: Vec<ChunkGroupUkey>,
assets: CompilationAssets,
pub module_assets: IdentifierMap<HashSet<Arc<str>>>,
pub emitted_assets: DashSet<String, BuildHasherDefault<FxHasher>>,
pub emitted_assets: DashSet<Arc<str>, BuildHasherDefault<FxHasher>>,
diagnostics: Vec<Diagnostic>,
logging: CompilationLogging,
pub plugin_driver: SharedPluginDriver,
Expand Down Expand Up @@ -552,6 +552,29 @@ impl Compilation {
self.global_entry.include_dependencies.push(entry_id);
}

let make_artifact = std::mem::take(&mut self.make_artifact);
self.make_artifact = update_module_graph(
self,
make_artifact,
vec![MakeParam::BuildEntryAndClean(
self
.entries
.values()
.flat_map(|item| item.all_dependencies())
.chain(self.global_entry.all_dependencies())
.copied()
.collect(),
)],
)
.await?;

// 1. 可以移动
// 2. add_include 不用管,napi 枷锁
// 3. finish_make 去调用,rust panic,js throw error
// 4. js finish_make 调用,但是

// 1. addtionable + add_include !=

Ok(())
}

Expand Down
5 changes: 4 additions & 1 deletion crates/rspack_core/src/compiler/mod.rs
Original file line number Diff line number Diff line change
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
4 changes: 2 additions & 2 deletions crates/rspack_core/src/compiler/module_executor/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,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
11 changes: 5 additions & 6 deletions crates/rspack_core/src/stats/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ impl<'compilation> Stats<'compilation> {

impl Stats<'_> {
pub fn get_assets(&self) -> (Vec<StatsAsset>, Vec<StatsAssetsByChunkName>) {
let mut compilation_file_to_chunks: HashMap<&String, Vec<&Chunk>> = HashMap::default();
let mut compilation_file_to_auxiliary_chunks: HashMap<&String, Vec<&Chunk>> =
HashMap::default();
let mut compilation_file_to_chunks: HashMap<&str, Vec<&Chunk>> = HashMap::default();
let mut compilation_file_to_auxiliary_chunks: HashMap<&str, Vec<&Chunk>> = HashMap::default();
for chunk in self.compilation.chunk_by_ukey.values() {
for file in chunk.files() {
let chunks = compilation_file_to_chunks.entry(file).or_default();
Expand All @@ -65,7 +64,7 @@ impl Stats<'_> {
}
}

let mut assets: HashMap<&String, StatsAsset> = self
let mut assets: HashMap<&str, StatsAsset> = self
.compilation
.assets()
.par_iter()
Expand All @@ -79,7 +78,7 @@ impl Stats<'_> {
})
}
(
name,
name.as_ref(),
StatsAsset {
r#type: "asset",
name: name.clone(),
Expand Down Expand Up @@ -112,7 +111,7 @@ impl Stats<'_> {
.collect::<HashMap<_, _>>();
for asset in self.compilation.assets().values() {
if let Some(source_map) = &asset.get_info().related.source_map {
assets.remove(source_map);
assets.remove(source_map.as_str());
}
}
assets.par_iter_mut().for_each(|(name, asset)| {
Expand Down
14 changes: 7 additions & 7 deletions crates/rspack_core/src/stats/struct.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::borrow::Cow;
use std::fmt::Debug;
use std::{borrow::Cow, sync::Arc};

use rspack_paths::Utf8PathBuf;
use rspack_sources::Source;
Expand Down Expand Up @@ -90,7 +90,7 @@ pub struct StatsErrorModuleTraceModule {
#[derive(Debug)]
pub struct StatsAsset {
pub r#type: &'static str,
pub name: String,
pub name: Arc<str>,
pub size: f64,
pub chunks: Vec<Option<String>>,
pub chunk_names: Vec<String>,
Expand Down Expand Up @@ -196,8 +196,8 @@ pub struct StatsOriginRecord {
#[derive(Debug)]
pub struct StatsChunk<'a> {
pub r#type: &'static str,
pub files: Vec<String>,
pub auxiliary_files: Vec<String>,
pub files: Vec<Arc<str>>,
pub auxiliary_files: Vec<Arc<str>>,
pub id: Option<String>,
pub entry: bool,
pub initial: bool,
Expand All @@ -219,7 +219,7 @@ pub struct StatsChunk<'a> {

#[derive(Debug)]
pub struct StatsChunkGroupAsset {
pub name: String,
pub name: Arc<str>,
pub size: usize,
}

Expand All @@ -244,8 +244,8 @@ pub struct StatsChunkGroupChildren {

#[derive(Debug)]
pub struct StatschunkGroupChildAssets {
pub preload: Vec<String>,
pub prefetch: Vec<String>,
pub preload: Vec<Arc<str>>,
pub prefetch: Vec<Arc<str>>,
}

#[derive(Debug)]
Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_core/src/stats/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::borrow::Cow;
use std::{borrow::Cow, sync::Arc};

use itertools::Itertools;
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
Expand Down Expand Up @@ -75,7 +75,7 @@ pub fn get_chunk_group_oreded_child_assets(
order_key: &ChunkGroupOrderKey,
chunk_group_by_ukey: &ChunkGroupByUkey,
chunk_by_ukey: &ChunkByUkey,
) -> Vec<String> {
) -> Vec<Arc<str>> {
ordered_children
.get(&ChunkGroupOrderKey::Preload)
.unwrap_or_else(|| panic!("should have {order_key} chunk groups"))
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_plugin_banner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ async fn process_assets(&self, compilation: &mut Compilation) -> Result<()> {
}

for (file, comment) in updates {
let _res = compilation.update_asset(file.as_str(), |old, info| {
let _res = compilation.update_asset(file.as_ref(), |old, info| {
let new = self.update_source(comment, old, self.config.footer);
Ok((new, info))
});
Expand Down
Loading

0 comments on commit dca7627

Please sign in to comment.