Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ahabhgk committed Aug 14, 2024
1 parent bc26ec3 commit 503e26c
Show file tree
Hide file tree
Showing 25 changed files with 65 additions and 106 deletions.
4 changes: 2 additions & 2 deletions crates/rspack_core/src/chunk_graph/chunk_graph_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
use std::hash::{Hash, Hasher};

use rspack_collections::{IdentifierMap, IdentifierSet, UkeySet};
use rspack_collections::{IdentifierSet, UkeySet};
use rspack_hash::RspackHashDigest;
use rspack_util::ext::DynHash;
use rustc_hash::FxHasher;
use tracing::instrument;

use crate::{
get_chunk_group_from_ukey, AsyncDependenciesBlockIdentifier, BoxModule, ChunkByUkey, ChunkGroup,
get_chunk_group_from_ukey, AsyncDependenciesBlockIdentifier, ChunkByUkey, ChunkGroup,
ChunkGroupByUkey, ChunkGroupUkey, ChunkUkey, Compilation, ModuleIdentifier, RuntimeGlobals,
RuntimeSpec, RuntimeSpecMap, RuntimeSpecSet,
};
Expand Down
42 changes: 26 additions & 16 deletions crates/rspack_core/src/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -754,11 +754,11 @@ impl Compilation {

fn run_iteration(
compilation: &mut Compilation,
codegen_cache_counter: &mut Option<CacheCount>,
cache_counter: &mut Option<CacheCount>,
filter_op: impl Fn(&(ModuleIdentifier, &Box<dyn Module>)) -> bool + Sync + Send,
) -> Result<()> {
let results = compilation.code_generation_modules(
codegen_cache_counter,
cache_counter,
compilation
.get_module_graph()
.modules()
Expand Down Expand Up @@ -794,7 +794,7 @@ impl Compilation {

pub(crate) fn code_generation_modules(
&mut self,
codegen_cache_counter: &mut Option<CacheCount>,
cache_counter: &mut Option<CacheCount>,
modules: IdentifierSet,
) -> Result<Vec<ModuleIdentifier>> {
let chunk_graph = &self.chunk_graph;
Expand Down Expand Up @@ -855,24 +855,34 @@ impl Compilation {
let module = module_graph
.module_by_identifier(&module)
.expect("should have module");
module.code_generation(self, Some(&runtime), None)
module.code_generation(self, Some(runtime), None)
})
.map(|(res, runtimes)| (module, res, runtimes))
.map(|(res, runtimes, from_cache)| (module, res, runtimes, from_cache))
})
.collect::<Result<Vec<_>>>()?;
let results = results.into_iter().map(|(module, codegen_res, runtimes)| {
let codegen_res_id = codegen_res.id;
self
.code_generation_results
.module_generation_result_map
.insert(codegen_res_id, codegen_res);
for runtime in runtimes {
let results = results
.into_iter()
.map(|(module, codegen_res, runtimes, from_cache)| {
if let Some(counter) = cache_counter {
if from_cache {
counter.hit();
} else {
counter.miss();
}
}

let codegen_res_id = codegen_res.id;
self
.code_generation_results
.add(module, runtime, codegen_res_id);
}
module
});
.module_generation_result_map
.insert(codegen_res_id, codegen_res);
for runtime in runtimes {
self
.code_generation_results
.add(module, runtime, codegen_res_id);
}
module
});

Ok(results.collect())
}
Expand Down
12 changes: 3 additions & 9 deletions crates/rspack_core/src/concatenated_module.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use std::{
borrow::Cow,
collections::hash_map::{DefaultHasher, Entry},
collections::hash_map::Entry,
fmt::Debug,
hash::{BuildHasherDefault, Hash, Hasher},
hash::{BuildHasherDefault, Hasher},
sync::{Arc, LazyLock, Mutex},
};

use dashmap::DashMap;
use indexmap::{IndexMap, IndexSet};
use once_cell::sync::OnceCell;
use rayon::prelude::*;
use regex::Regex;
use rspack_ast::javascript::Ast;
Expand Down Expand Up @@ -137,7 +136,6 @@ impl ConcatenationEntry {
#[derive(Debug)]
struct ConcatenationEntryConcatenated {
module: ModuleIdentifier,
runtime_condition: RuntimeCondition,
}

#[derive(Debug)]
Expand Down Expand Up @@ -363,7 +361,6 @@ pub struct ConcatenatedModule {
cached_source_sizes: DashMap<SourceType, f64, BuildHasherDefault<FxHasher>>,

diagnostics: Mutex<Vec<Diagnostic>>,
cached_hash: OnceCell<u64>,
build_info: Option<BuildInfo>,
}

Expand All @@ -386,7 +383,6 @@ impl ConcatenatedModule {
blocks: vec![],
cached_source_sizes: DashMap::default(),
diagnostics: Mutex::new(vec![]),
cached_hash: OnceCell::default(),
build_info: None,
source_map_kind: SourceMapKind::empty(),
}
Expand Down Expand Up @@ -529,7 +525,7 @@ impl Module for ConcatenatedModule {
/// the compilation is asserted to be `Some(Compilation)`, https://github.com/webpack/webpack/blob/1f99ad6367f2b8a6ef17cce0e058f7a67fb7db18/lib/optimize/ModuleConcatenationPlugin.js#L394-L418
async fn build(
&mut self,
build_context: BuildContext<'_>,
_build_context: BuildContext<'_>,
compilation: Option<&Compilation>,
) -> Result<BuildResult> {
let compilation = compilation.expect("should pass compilation");
Expand Down Expand Up @@ -1494,7 +1490,6 @@ impl ConcatenatedModule {
list.push(ConcatenationEntry::Concatenated(
ConcatenationEntryConcatenated {
module: root_module,
runtime_condition: RuntimeCondition::Boolean(true),
},
));
list
Expand Down Expand Up @@ -1542,7 +1537,6 @@ impl ConcatenatedModule {
list.push(ConcatenationEntry::Concatenated(
ConcatenationEntryConcatenated {
module: *con.module_identifier(),
runtime_condition,
},
));
} else {
Expand Down
1 change: 0 additions & 1 deletion crates/rspack_core/src/context_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use itertools::Itertools;
use regex::{Captures, Regex};
use rspack_collections::{Identifiable, Identifier};
use rspack_error::{impl_empty_diagnosable_trait, miette::IntoDiagnostic, Diagnostic, Result};
use rspack_hash::RspackHash;
use rspack_macros::impl_source_map_config;
use rspack_regex::RspackRegex;
use rspack_sources::{BoxSource, ConcatSource, RawSource, SourceExt};
Expand Down
7 changes: 1 addition & 6 deletions crates/rspack_core/src/dependencies_block.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
use std::{
borrow::Cow,
fmt::Display,
hash::{Hash, Hasher},
sync::Arc,
};
use std::{borrow::Cow, fmt::Display, hash::Hash, sync::Arc};

use derivative::Derivative;
use rspack_collections::Identifier;
Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_core/src/dependency/loader_import.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
AsContextDependency, AsDependencyTemplate, Compilation, Context, Dependency, DependencyCategory,
DependencyId, DependencyType, ModuleDependency, RuntimeSpec,
AsContextDependency, AsDependencyTemplate, Context, Dependency, DependencyCategory, DependencyId,
DependencyType, ModuleDependency,
};

#[derive(Debug, Hash, PartialEq, Eq, Clone)]
Expand Down
9 changes: 0 additions & 9 deletions crates/rspack_core/src/exports_info.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
use std::borrow::Cow;
use std::collections::hash_map::Entry;
use std::collections::BTreeMap;
use std::hash::Hasher;
use std::sync::atomic::AtomicU32;
use std::sync::atomic::Ordering::Relaxed;
use std::sync::Arc;
use std::sync::LazyLock;

use either::Either;
use itertools::Itertools;
use rspack_collections::impl_item_ukey;
use rspack_collections::Ukey;
use rspack_collections::UkeyDashMap;
use rspack_collections::UkeySet;
use rspack_util::atom::Atom;
use rspack_util::ext::DynHash;
use rustc_hash::FxHashMap as HashMap;
use rustc_hash::FxHashSet as HashSet;
use rustc_hash::FxHasher;
use serde::Serialize;

use crate::Compilation;
Expand All @@ -26,9 +22,6 @@ use crate::{
ModuleIdentifier, Nullable, RuntimeSpec,
};

static EXPORTS_INFO_HASH: LazyLock<UkeyDashMap<ExportsInfo, u64>> =
LazyLock::new(UkeyDashMap::default);

#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, Ord, PartialOrd, Serialize)]
pub struct ExportsInfo(Ukey);

Expand Down Expand Up @@ -1553,7 +1546,6 @@ impl ExportInfo {
pub struct ExportInfoData {
// the name could be `null` you could refer https://github.com/webpack/webpack/blob/ac7e531436b0d47cd88451f497cdfd0dad4153d/lib/ExportsInfo.js#L78
name: Option<Atom>,
usage_state: UsageState,
/// this is mangled name, https://github.com/webpack/webpack/blob/1f99ad6367f2b8a6ef17cce0e058f7a67fb7db18/lib/ExportsInfo.js#L1181-L1188
used_name: Option<Atom>,
target: HashMap<Option<DependencyId>, ExportInfoTargetValue>,
Expand Down Expand Up @@ -1688,7 +1680,6 @@ impl ExportInfoData {
.unwrap_or_default();
Self {
name,
usage_state: UsageState::Unknown,
used_name,
used_in_runtime,
target,
Expand Down
2 changes: 0 additions & 2 deletions crates/rspack_core/src/external_module.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use std::borrow::Cow;
use std::hash::Hash;
use std::iter;

use rspack_collections::{Identifiable, Identifier};
use rspack_error::{error, impl_empty_diagnosable_trait, Diagnostic, Result};
use rspack_hash::RspackHash;
use rspack_macros::impl_source_map_config;
use rspack_util::{ext::DynHash, json_stringify, source_map::SourceMapKind};
use rustc_hash::{FxHashMap as HashMap, FxHashSet};
Expand Down
5 changes: 2 additions & 3 deletions crates/rspack_core/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use async_trait::async_trait;
use json::JsonValue;
use rspack_collections::{Identifiable, Identifier, IdentifierSet};
use rspack_error::{Diagnosable, Diagnostic, Result};
use rspack_hash::{RspackHash, RspackHashDigest};
use rspack_hash::RspackHashDigest;
use rspack_sources::Source;
use rspack_util::atom::Atom;
use rspack_util::ext::{AsAny, DynEq, DynHash};
use rspack_util::ext::{AsAny, DynHash};
use rspack_util::source_map::ModuleSourceMapConfig;
use rustc_hash::FxHashSet as HashSet;

Expand Down Expand Up @@ -597,7 +597,6 @@ pub struct LibIdentOptions<'me> {
#[cfg(test)]
mod test {
use std::borrow::Cow;
use std::hash::Hash;

use rspack_collections::{Identifiable, Identifier};
use rspack_error::{Diagnosable, Diagnostic, Result};
Expand Down
16 changes: 7 additions & 9 deletions crates/rspack_core/src/old_cache/occasion/code_generate.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use rspack_collections::Identifier;
use rspack_error::Result;

use crate::{
get_runtime_key, CodeGenerationJob, Module, ModuleIdentifier, RuntimeSpec, RuntimeSpecSet,
};
use crate::{old_cache::storage, BoxModule, CodeGenerationResult, Compilation, NormalModuleSource};
use crate::{old_cache::storage, CodeGenerationResult};
use crate::{CodeGenerationJob, ModuleIdentifier, RuntimeSpec};

type Storage = dyn storage::Storage<CodeGenerationResult>;

Expand All @@ -18,25 +16,25 @@ impl CodeGenerateOccasion {
Self { storage }
}

pub fn use_cache<'a>(
pub fn use_cache(
&self,
job: CodeGenerationJob,
provide: impl Fn(ModuleIdentifier, &RuntimeSpec) -> Result<CodeGenerationResult>,
) -> Result<(CodeGenerationResult, Vec<RuntimeSpec>)> {
) -> Result<(CodeGenerationResult, Vec<RuntimeSpec>, bool)> {
let storage = match &self.storage {
Some(s) => s,
None => {
let res = provide(job.module, &job.runtime)?;
return Ok((res, job.runtimes));
return Ok((res, job.runtimes, false));
}
};
let cache_key = Identifier::from(format!("{}|{}", job.module, job.hash.encoded()));
if let Some(value) = storage.get(&cache_key) {
return Ok((value, job.runtimes));
Ok((value, job.runtimes, true))
} else {
let res = provide(job.module, &job.runtime)?;
storage.set(cache_key, res.clone());
return Ok((res, job.runtimes));
Ok((res, job.runtimes, false))
}
}
}
5 changes: 2 additions & 3 deletions crates/rspack_core/src/parser_and_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ use swc_core::common::Span;

use crate::{
AsyncDependenciesBlock, BoxDependency, BoxLoader, BuildInfo, BuildMeta, CodeGenerationData,
Compilation, CompilerOptions, DependencyTemplate, GeneratorOptions, Module, ModuleDependency,
ModuleIdentifier, ModuleLayer, ModuleType, NormalModule, ParserOptions, RuntimeGlobals,
RuntimeSpec, SourceType,
Compilation, CompilerOptions, DependencyTemplate, Module, ModuleDependency, ModuleIdentifier,
ModuleLayer, ModuleType, NormalModule, ParserOptions, RuntimeGlobals, RuntimeSpec, SourceType,
};
use crate::{ChunkGraph, ConcatenationScope, Context, ModuleGraph};

Expand Down
4 changes: 1 addition & 3 deletions crates/rspack_core/src/raw_module.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use std::borrow::Cow;
use std::hash::Hash;

use rspack_collections::Identifiable;
use rspack_error::{impl_empty_diagnosable_trait, Diagnostic, Result};
use rspack_hash::RspackHash;
use rspack_macros::impl_source_map_config;
use rspack_sources::{BoxSource, RawSource, Source, SourceExt};
use rspack_util::source_map::SourceMapKind;
Expand Down Expand Up @@ -108,7 +106,7 @@ impl Module for RawModule {

async fn build(
&mut self,
build_context: BuildContext<'_>,
_build_context: BuildContext<'_>,
_: Option<&Compilation>,
) -> Result<BuildResult> {
Ok(BuildResult {
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_core/src/self_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl Module for SelfModule {

async fn build(
&mut self,
build_context: BuildContext<'_>,
_build_context: BuildContext<'_>,
_: Option<&Compilation>,
) -> Result<BuildResult> {
let build_info = BuildInfo {
Expand Down
12 changes: 1 addition & 11 deletions crates/rspack_macros_test/tests/runtime_module.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::{marker::PhantomData, sync::Arc};
use std::marker::PhantomData;

use rspack_collections::Identifier;
use rspack_core::{rspack_sources::Source, Compilation, RuntimeModule};
use rspack_error::Result;
use rspack_macros::impl_runtime_module;

#[allow(dead_code)]
Expand All @@ -14,15 +13,6 @@ fn with_generic() {
marker: PhantomData<T>,
}

impl<T: std::fmt::Debug + Send + Sync + Eq + 'static> Foo<T> {
fn name(&self) -> Identifier {
String::new().into()
}
fn generate_with_custom(&self, _compilation: &Compilation) -> Result<Arc<dyn Source>> {
todo!()
}
}

impl<T: std::fmt::Debug + Send + Sync + Eq + 'static> RuntimeModule for Foo<T> {
fn name(&self) -> Identifier {
todo!()
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_plugin_asset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ impl AssetParserAndGenerator {
.chunk_graph(&compilation.chunk_graph)
.content_hash_optional(contenthash)
.hash_optional(contenthash)
.filename(&source_file_name),
.filename(source_file_name),
)?;
let public_path = PublicPath::ensure_ends_with_slash(public_path);
Ok((public_path, info))
Expand Down
Loading

0 comments on commit 503e26c

Please sign in to comment.