Skip to content

Commit

Permalink
refactor(rspack): improve dependency location (#8313)
Browse files Browse the repository at this point in the history
  • Loading branch information
shulaoda authored Nov 4, 2024
1 parent e014ad6 commit 10de7f9
Show file tree
Hide file tree
Showing 60 changed files with 288 additions and 313 deletions.
10 changes: 4 additions & 6 deletions crates/rspack_core/src/build_chunk_graph/code_splitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ use crate::incremental::IncrementalPasses;
use crate::{
assign_depths, get_entry_runtime, merge_runtime, AsyncDependenciesBlockIdentifier, ChunkGroup,
ChunkGroupKind, ChunkGroupOptions, ChunkGroupUkey, ChunkLoading, ChunkUkey, Compilation,
ConnectionState, DependenciesBlock, DependencyId, DependencyLocation, EntryDependency,
EntryRuntime, GroupOptions, Logger, ModuleDependency, ModuleGraph, ModuleIdentifier, RuntimeSpec,
SyntheticDependencyLocation,
ConnectionState, DependenciesBlock, DependencyId, DependencyLocation, DependencyName,
EntryDependency, EntryRuntime, GroupOptions, Logger, ModuleDependency, ModuleGraph,
ModuleIdentifier, RuntimeSpec,
};

type IndexMap<K, V, H = FxHasher> = RawIndexMap<K, V, BuildHasherDefault<H>>;
Expand Down Expand Up @@ -404,9 +404,7 @@ impl CodeSplitter {
));

for request in requests {
let loc = Some(DependencyLocation::Synthetic(
SyntheticDependencyLocation::new(name),
));
let loc = Some(DependencyLocation::Synthetic(DependencyName::new(name)));
entrypoint.add_origin(None, loc, request);
}

Expand Down
8 changes: 4 additions & 4 deletions crates/rspack_core/src/context_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ use crate::{
BuildMetaDefaultObject, BuildMetaExportsType, BuildResult, ChunkGraph, ChunkGroupOptions,
CodeGenerationResult, Compilation, ConcatenationScope, ContextElementDependency,
DependenciesBlock, Dependency, DependencyCategory, DependencyId, DependencyLocation,
DynamicImportMode, ExportsType, FactoryMeta, FakeNamespaceObjectMode, GroupOptions,
ImportAttributes, LibIdentOptions, Module, ModuleLayer, ModuleType, RealDependencyLocation,
Resolve, RuntimeGlobals, RuntimeSpec, SourceType,
DependencyRange, DynamicImportMode, ExportsType, FactoryMeta, FakeNamespaceObjectMode,
GroupOptions, ImportAttributes, LibIdentOptions, Module, ModuleLayer, ModuleType, Resolve,
RuntimeGlobals, RuntimeSpec, SourceType,
};

static WEBPACK_CHUNK_NAME_INDEX_PLACEHOLDER: &str = "[index]";
Expand Down Expand Up @@ -887,7 +887,7 @@ impl Module for ContextModule {
if matches!(self.options.context_options.mode, ContextMode::LazyOnce)
&& !context_element_dependencies.is_empty()
{
let loc = RealDependencyLocation::new(
let loc = DependencyRange::new(
self.options.context_options.start,
self.options.context_options.end,
);
Expand Down
24 changes: 12 additions & 12 deletions crates/rspack_core/src/dependency/dependency_location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ use derivative::Derivative;

#[derive(Derivative)]
#[derivative(Debug, Clone, Hash)]
pub struct RealDependencyLocation {
pub struct DependencyRange {
pub end: u32,
pub start: u32,
#[derivative(Debug = "ignore", Hash = "ignore")]
source: Option<Arc<dyn SourceLocation>>,
}

impl RealDependencyLocation {
impl DependencyRange {
pub fn new(start: u32, end: u32) -> Self {
RealDependencyLocation {
DependencyRange {
end,
start,
source: None,
Expand All @@ -26,7 +26,7 @@ impl RealDependencyLocation {
}
}

impl From<(u32, u32)> for RealDependencyLocation {
impl From<(u32, u32)> for DependencyRange {
fn from(range: (u32, u32)) -> Self {
Self {
start: range.0,
Expand All @@ -36,7 +36,7 @@ impl From<(u32, u32)> for RealDependencyLocation {
}
}

impl From<swc_core::common::Span> for RealDependencyLocation {
impl From<swc_core::common::Span> for DependencyRange {
fn from(span: swc_core::common::Span) -> Self {
Self {
start: span.lo.0.saturating_sub(1),
Expand All @@ -46,7 +46,7 @@ impl From<swc_core::common::Span> for RealDependencyLocation {
}
}

impl fmt::Display for RealDependencyLocation {
impl fmt::Display for DependencyRange {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some(source) = &self.source {
let (start, end) = source.look_up_range_pos(self.start, self.end);
Expand All @@ -71,28 +71,28 @@ impl fmt::Display for RealDependencyLocation {
}

#[derive(Debug, Clone)]
pub struct SyntheticDependencyLocation {
pub struct DependencyName {
pub name: String,
}

impl SyntheticDependencyLocation {
impl DependencyName {
pub fn new(name: &str) -> Self {
SyntheticDependencyLocation {
DependencyName {
name: name.to_string(),
}
}
}

impl fmt::Display for SyntheticDependencyLocation {
impl fmt::Display for DependencyName {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.name)
}
}

#[derive(Debug, Clone)]
pub enum DependencyLocation {
Real(RealDependencyLocation),
Synthetic(SyntheticDependencyLocation),
Real(DependencyRange),
Synthetic(DependencyName),
}

impl fmt::Display for DependencyLocation {
Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_core/src/dependency/dependency_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use swc_core::ecma::atoms::Atom;

use super::dependency_template::AsDependencyTemplate;
use super::module_dependency::*;
use super::DependencyRange;
use super::ExportsSpec;
use super::RealDependencyLocation;
use super::{DependencyCategory, DependencyId, DependencyType};
use crate::create_exports_object_referenced;
use crate::AsContextDependency;
Expand Down Expand Up @@ -78,7 +78,7 @@ pub trait Dependency:
None
}

fn range(&self) -> Option<&RealDependencyLocation> {
fn range(&self) -> Option<&DependencyRange> {
None
}

Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_core/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rspack_error::{
};
use rspack_util::ext::AsAny;

use crate::{BoxLoader, RealDependencyLocation};
use crate::{BoxLoader, DependencyRange};

///////////////////// Module Factory /////////////////////

Expand All @@ -18,7 +18,7 @@ use crate::{BoxLoader, RealDependencyLocation};
pub struct EmptyDependency(Box<dyn Diagnostic + Send + Sync>);

impl EmptyDependency {
pub fn new(span: RealDependencyLocation) -> Self {
pub fn new(span: DependencyRange) -> Self {
Self(
TraceableError::from_lazy_file(
span.start as usize,
Expand Down
8 changes: 3 additions & 5 deletions crates/rspack_core/src/normal_module_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ use swc_core::common::Span;
use crate::{
diagnostics::EmptyDependency, module_rules_matcher, parse_resource, resolve,
stringify_loaders_and_resource, BoxLoader, BoxModule, CompilerOptions, Context, Dependency,
DependencyCategory, FuncUseCtx, GeneratorOptions, ModuleExt, ModuleFactory,
DependencyCategory, DependencyRange, FuncUseCtx, GeneratorOptions, ModuleExt, ModuleFactory,
ModuleFactoryCreateData, ModuleFactoryResult, ModuleIdentifier, ModuleLayer, ModuleRuleEffect,
ModuleRuleEnforce, ModuleRuleUse, ModuleRuleUseLoader, ModuleType, NormalModule,
ParserAndGenerator, ParserOptions, RawModule, RealDependencyLocation, Resolve, ResolveArgs,
ParserAndGenerator, ParserOptions, RawModule, Resolve, ResolveArgs,
ResolveOptionsWithDependencyType, ResolveResult, Resolver, ResolverFactory, ResourceData,
ResourceParsedData, RunnerContext, SharedPluginDriver,
};
Expand Down Expand Up @@ -231,9 +231,7 @@ impl NormalModuleFactory {

if first_char.is_none() {
let span = dependency.source_span().unwrap_or_default();
return Err(
EmptyDependency::new(RealDependencyLocation::new(span.start, span.end)).into(),
);
return Err(EmptyDependency::new(DependencyRange::new(span.start, span.end)).into());
}

// See: https://webpack.js.org/concepts/loaders/#inline
Expand Down
8 changes: 4 additions & 4 deletions crates/rspack_plugin_css/src/dependency/compose.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use rspack_core::{
AsContextDependency, AsDependencyTemplate, Dependency, DependencyCategory, DependencyId,
DependencyType, ModuleDependency, RealDependencyLocation,
DependencyRange, DependencyType, ModuleDependency,
};

#[derive(Debug, Clone)]
pub struct CssComposeDependency {
id: DependencyId,
request: String,
range: RealDependencyLocation,
range: DependencyRange,
}

impl CssComposeDependency {
pub fn new(request: String, range: RealDependencyLocation) -> Self {
pub fn new(request: String, range: DependencyRange) -> Self {
Self {
id: DependencyId::new(),
request,
Expand All @@ -33,7 +33,7 @@ impl Dependency for CssComposeDependency {
&DependencyType::CssCompose
}

fn range(&self) -> Option<&RealDependencyLocation> {
fn range(&self) -> Option<&DependencyRange> {
Some(&self.range)
}

Expand Down
12 changes: 6 additions & 6 deletions crates/rspack_plugin_css/src/dependency/import.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use rspack_core::{
AsContextDependency, Compilation, Dependency, DependencyCategory, DependencyId,
DependencyTemplate, DependencyType, ModuleDependency, RealDependencyLocation, RuntimeSpec,
TemplateContext, TemplateReplaceSource,
AsContextDependency, Compilation, Dependency, DependencyCategory, DependencyId, DependencyRange,
DependencyTemplate, DependencyType, ModuleDependency, RuntimeSpec, TemplateContext,
TemplateReplaceSource,
};

#[derive(Debug, Clone)]
pub struct CssImportDependency {
id: DependencyId,
request: String,
range: RealDependencyLocation,
range: DependencyRange,
}

impl CssImportDependency {
pub fn new(request: String, range: RealDependencyLocation) -> Self {
pub fn new(request: String, range: DependencyRange) -> Self {
Self {
id: DependencyId::new(),
request,
Expand All @@ -34,7 +34,7 @@ impl Dependency for CssImportDependency {
&DependencyType::CssImport
}

fn range(&self) -> Option<&RealDependencyLocation> {
fn range(&self) -> Option<&DependencyRange> {
Some(&self.range)
}

Expand Down
10 changes: 5 additions & 5 deletions crates/rspack_plugin_css/src/dependency/url.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use rspack_core::{
AsContextDependency, CodeGenerationDataFilename, CodeGenerationDataUrl, Compilation, Dependency,
DependencyCategory, DependencyId, DependencyTemplate, DependencyType, ModuleDependency,
ModuleIdentifier, PublicPath, RealDependencyLocation, RuntimeSpec, TemplateContext,
DependencyCategory, DependencyId, DependencyRange, DependencyTemplate, DependencyType,
ModuleDependency, ModuleIdentifier, PublicPath, RuntimeSpec, TemplateContext,
TemplateReplaceSource,
};

Expand All @@ -11,12 +11,12 @@ use crate::utils::{css_escape_string, AUTO_PUBLIC_PATH_PLACEHOLDER};
pub struct CssUrlDependency {
id: DependencyId,
request: String,
range: RealDependencyLocation,
range: DependencyRange,
replace_function: bool,
}

impl CssUrlDependency {
pub fn new(request: String, range: RealDependencyLocation, replace_function: bool) -> Self {
pub fn new(request: String, range: DependencyRange, replace_function: bool) -> Self {
Self {
request,
range,
Expand Down Expand Up @@ -64,7 +64,7 @@ impl Dependency for CssUrlDependency {
&DependencyType::CssUrl
}

fn range(&self) -> Option<&RealDependencyLocation> {
fn range(&self) -> Option<&DependencyRange> {
Some(&self.range)
}

Expand Down
14 changes: 7 additions & 7 deletions crates/rspack_plugin_css/src/parser_and_generator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ use rspack_core::{
diagnostics::map_box_diagnostics_to_module_parse_diagnostics,
rspack_sources::{BoxSource, ConcatSource, RawSource, ReplaceSource, Source, SourceExt},
BuildMetaDefaultObject, BuildMetaExportsType, ChunkGraph, Compilation, ConstDependency,
CssExportsConvention, Dependency, DependencyId, DependencyTemplate, GenerateContext,
LocalIdentName, Module, ModuleDependency, ModuleGraph, ModuleIdentifier, ModuleType,
NormalModule, ParseContext, ParseResult, ParserAndGenerator, RealDependencyLocation, RuntimeSpec,
SourceType, TemplateContext, UsageState,
CssExportsConvention, Dependency, DependencyId, DependencyRange, DependencyTemplate,
GenerateContext, LocalIdentName, Module, ModuleDependency, ModuleGraph, ModuleIdentifier,
ModuleType, NormalModule, ParseContext, ParseResult, ParserAndGenerator, RuntimeSpec, SourceType,
TemplateContext, UsageState,
};
use rspack_core::{ModuleInitFragments, RuntimeGlobals};
use rspack_error::{
Expand Down Expand Up @@ -161,7 +161,7 @@ impl ParserAndGenerator for CssParserAndGenerator {
let request = normalize_url(request);
let dep = Box::new(CssUrlDependency::new(
request,
RealDependencyLocation::new(range.start, range.end),
DependencyRange::new(range.start, range.end),
matches!(kind, css_module_lexer::UrlRangeKind::Function),
));
dependencies.push(dep.clone());
Expand All @@ -186,7 +186,7 @@ impl ParserAndGenerator for CssParserAndGenerator {
);
dependencies.push(Box::new(CssImportDependency::new(
request.to_string(),
RealDependencyLocation::new(range.start, range.end),
DependencyRange::new(range.start, range.end),
)));
}
css_module_lexer::Dependency::Replace { content, range } => presentational_dependencies
Expand Down Expand Up @@ -280,7 +280,7 @@ impl ParserAndGenerator for CssParserAndGenerator {
let from = from.trim_matches(|c| c == '\'' || c == '"');
let dep = CssComposeDependency::new(
from.to_string(),
RealDependencyLocation::new(range.start, range.end),
DependencyRange::new(range.start, range.end),
);
dep_id = Some(*dep.id());
dependencies.push(Box::new(dep));
Expand Down
8 changes: 4 additions & 4 deletions crates/rspack_plugin_extract_css/src/css_dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::path::PathBuf;
use rspack_collections::IdentifierSet;
use rspack_core::{
AffectType, AsContextDependency, AsDependencyTemplate, ConnectionState, Dependency,
DependencyCategory, DependencyId, ModuleDependency, ModuleGraph, RealDependencyLocation,
DependencyCategory, DependencyId, DependencyRange, ModuleDependency, ModuleGraph,
};
use rustc_hash::FxHashSet;

Expand All @@ -26,7 +26,7 @@ pub struct CssDependency {
// determine module's postOrderIndex
// @TODO(shulaoda) Does this have any additional side effects?
// pub(crate) order_index: u32,
range: RealDependencyLocation,
range: DependencyRange,
resource_identifier: String,
pub(crate) cacheable: bool,
pub(crate) file_dependencies: FxHashSet<PathBuf>,
Expand All @@ -46,7 +46,7 @@ impl CssDependency {
supports: Option<String>,
source_map: Option<String>,
identifier_index: u32,
range: RealDependencyLocation,
range: DependencyRange,
cacheable: bool,
file_dependencies: FxHashSet<PathBuf>,
context_dependencies: FxHashSet<PathBuf>,
Expand Down Expand Up @@ -108,7 +108,7 @@ impl Dependency for CssDependency {
// it can keep the right order, but Rspack uses HashSet,
// when determining the postOrderIndex, Rspack uses
// dependency span to set correct order
fn range(&self) -> Option<&RealDependencyLocation> {
fn range(&self) -> Option<&DependencyRange> {
Some(&self.range)
}

Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_plugin_extract_css/src/parser_plugin.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rspack_core::{BoxDependency, RealDependencyLocation};
use rspack_core::{BoxDependency, DependencyRange};
use rspack_plugin_javascript::{visitors::JavascriptParser, JavascriptParserPlugin};
use rspack_util::fx_hash::FxDashMap;
use serde::Deserialize;
Expand Down Expand Up @@ -55,7 +55,7 @@ impl JavascriptParserPlugin for PluginCssExtractParserPlugin {
supports.clone(),
source_map.clone(),
*identifier_index,
RealDependencyLocation::new(index as u32, (index + 1) as u32),
DependencyRange::new(index as u32, (index + 1) as u32),
parser.build_info.cacheable,
parser.build_info.file_dependencies.clone(),
parser.build_info.context_dependencies.clone(),
Expand Down
Loading

2 comments on commit 10de7f9

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result
modernjs ❌ failure
_selftest ✅ success
rspress ✅ success
rslib ✅ success
rsbuild ❌ failure
examples ❌ failure
devserver ✅ success

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

Name Base (2024-11-04 a987332) Current Change
10000_big_production-mode + exec 45.9 s ± 613 ms 43 s ± 1.25 s -6.45 %
10000_development-mode + exec 1.84 s ± 17 ms 1.83 s ± 12 ms -0.36 %
10000_development-mode_hmr + exec 645 ms ± 12 ms 643 ms ± 9.6 ms -0.35 %
10000_production-mode + exec 2.4 s ± 19 ms 2.42 s ± 19 ms +0.80 %
arco-pro_development-mode + exec 1.79 s ± 63 ms 1.78 s ± 64 ms -0.38 %
arco-pro_development-mode_hmr + exec 429 ms ± 1.2 ms 431 ms ± 1 ms +0.47 %
arco-pro_production-mode + exec 3.19 s ± 87 ms 3.19 s ± 85 ms -0.04 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.27 s ± 50 ms 3.24 s ± 107 ms -0.67 %
threejs_development-mode_10x + exec 1.58 s ± 7.7 ms 1.57 s ± 15 ms -0.57 %
threejs_development-mode_10x_hmr + exec 784 ms ± 4.2 ms 782 ms ± 17 ms -0.25 %
threejs_production-mode_10x + exec 4.93 s ± 40 ms 4.95 s ± 32 ms +0.34 %

Please sign in to comment.