Skip to content

Commit

Permalink
perf: compilation assets
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind committed Dec 5, 2024
1 parent c26a11a commit 24f6004
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 21 deletions.
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
12 changes: 7 additions & 5 deletions crates/rspack_core/src/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ pub struct Compilation {
pub entrypoints: IndexMap<String, ChunkGroupUkey>,
pub async_entrypoints: Vec<ChunkGroupUkey>,
assets: CompilationAssets,
pub module_assets: IdentifierMap<HashSet<String>>,
pub module_assets: IdentifierMap<HashSet<Arc<str>>>,
pub emitted_assets: DashSet<String, BuildHasherDefault<FxHasher>>,
diagnostics: Vec<Diagnostic>,
logging: CompilationLogging,
Expand Down Expand Up @@ -584,8 +584,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 +629,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 +2163,7 @@ impl Compilation {
}
}

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

#[derive(Debug, Clone)]
pub struct CompilationAsset {
Expand Down
11 changes: 6 additions & 5 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 @@ -432,10 +432,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
3 changes: 2 additions & 1 deletion 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
6 changes: 4 additions & 2 deletions crates/rspack_core/src/compiler/module_executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ mod entry;
mod execute;
mod overwrite;

use std::sync::Arc;

use dashmap::DashMap;
use dashmap::{mapref::entry::Entry, DashSet};
pub use execute::ExecuteModuleId;
Expand Down Expand Up @@ -34,8 +36,8 @@ pub struct ModuleExecutor {

event_sender: Option<UnboundedSender<Event>>,
stop_receiver: Option<oneshot::Receiver<MakeArtifact>>,
assets: DashMap<String, CompilationAsset>,
module_assets: IdentifierDashMap<DashSet<String>>,
assets: DashMap<Arc<str>, CompilationAsset>,
module_assets: IdentifierDashMap<DashSet<Arc<str>>>,
code_generated_modules: IdentifierDashSet,
module_code_generated_modules: IdentifierDashMap<IdentifierDashSet>,
pub executed_runtime_modules: IdentifierDashMap<ExecutedRuntimeModule>,
Expand Down

0 comments on commit 24f6004

Please sign in to comment.