Skip to content

Commit

Permalink
perf: minimize memory allocation (#4689)
Browse files Browse the repository at this point in the history
* perf: minimize memory allocation

* chore: rename `module` to `create_module`
  • Loading branch information
bvanjoi authored Nov 17, 2023
1 parent 013a08e commit 3ae4b91
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 70 deletions.
3 changes: 1 addition & 2 deletions crates/rspack_core/src/code_generation_results.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::ops::{Deref, DerefMut};
use std::sync::atomic::AtomicU32;

use anymap::CloneAny;
use once_cell::sync::Lazy;
use rspack_error::{internal_error, Result};
use rspack_hash::{HashDigest, HashFunction, HashSalt, RspackHash, RspackHashDigest};
use rspack_identifier::IdentifierMap;
Expand Down Expand Up @@ -146,7 +145,7 @@ impl Default for CodeGenResultId {
}
}

pub static CODE_GEN_RESULT_ID: Lazy<AtomicU32> = Lazy::new(|| AtomicU32::new(0));
pub static CODE_GEN_RESULT_ID: AtomicU32 = AtomicU32::new(0);

#[derive(Debug, Default)]
pub struct CodeGenerationResults {
Expand Down
3 changes: 1 addition & 2 deletions crates/rspack_core/src/dependencies_block.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::sync::atomic::{AtomicU32, Ordering};

use once_cell::sync::Lazy;
use rspack_identifier::{Identifiable, Identifier};

use crate::{BoxDependency, DependencyId, GroupOptions};
Expand All @@ -15,7 +14,7 @@ pub trait DependenciesBlock {
fn get_dependencies(&self) -> &[DependencyId];
}

static ASYNC_DEPENDENCIES_BLOCK_ID: Lazy<AtomicU32> = Lazy::new(|| AtomicU32::new(0));
static ASYNC_DEPENDENCIES_BLOCK_ID: AtomicU32 = AtomicU32::new(0);

fn get_async_dependencies_block_id() -> AsyncDependenciesBlockId {
AsyncDependenciesBlockId(Identifier::from(
Expand Down
5 changes: 2 additions & 3 deletions crates/rspack_core/src/exports_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::sync::atomic::AtomicU32;
use std::sync::atomic::Ordering::Relaxed;
use std::sync::Arc;

use once_cell::sync::Lazy;
use rspack_util::ext::DynHash;
use rustc_hash::FxHashMap as HashMap;
use rustc_hash::FxHashSet as HashSet;
Expand All @@ -23,7 +22,7 @@ pub trait ExportsHash {
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, Ord, PartialOrd, Serialize)]
pub struct ExportsInfoId(u32);

pub static EXPORTS_INFO_ID: Lazy<AtomicU32> = Lazy::new(|| AtomicU32::new(0));
pub static EXPORTS_INFO_ID: AtomicU32 = AtomicU32::new(0);

impl ExportsHash for ExportsInfoId {
fn export_info_hash(&self, hasher: &mut dyn Hasher, module_graph: &ModuleGraph) {
Expand Down Expand Up @@ -531,7 +530,7 @@ pub struct ExportInfoTargetValue {
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, Ord, PartialOrd, Serialize)]
pub struct ExportInfoId(u32);

pub static EXPORT_INFO_ID: Lazy<AtomicU32> = Lazy::new(|| AtomicU32::new(0));
pub static EXPORT_INFO_ID: AtomicU32 = AtomicU32::new(0);

impl ExportsHash for ExportInfoId {
fn export_info_hash(&self, hasher: &mut dyn Hasher, module_graph: &ModuleGraph) {
Expand Down
16 changes: 10 additions & 6 deletions crates/rspack_core/src/normal_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@ impl NormalModuleSource {
pub static DEBUG_ID: AtomicUsize = AtomicUsize::new(1);

impl NormalModule {
fn create_id(module_type: &ModuleType, request: &str) -> String {
if *module_type == ModuleType::Js {
request.to_string()
} else {
format!("{module_type}|{request}")
}
}

#[allow(clippy::too_many_arguments)]
pub fn new(
request: String,
Expand All @@ -169,15 +177,11 @@ impl NormalModule {
contains_inline_loader: bool,
) -> Self {
let module_type = module_type.into();
let identifier = if module_type == ModuleType::Js {
request.to_string()
} else {
format!("{module_type}|{request}")
};
let id = Self::create_id(&module_type, &request);
Self {
blocks: Vec::new(),
dependencies: Vec::new(),
id: ModuleIdentifier::from(identifier),
id: ModuleIdentifier::from(id),
context: Box::new(get_context(&resource_data)),
request,
user_request,
Expand Down
44 changes: 20 additions & 24 deletions crates/rspack_core/src/normal_module_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use regex::Regex;
use rspack_error::{
internal_error, Diagnostic, IntoTWithDiagnosticArray, Result, TWithDiagnosticArray,
};
use rspack_identifier::Identifiable;
use rspack_loader_runner::{get_scheme, Loader, Scheme};
use sugar_path::{AsPath, SugarPath};
use swc_core::common::Span;
Expand All @@ -15,12 +14,12 @@ use crate::{
module_rules_matcher, parse_resource, resolve, stringify_loaders_and_resource,
tree_shaking::visitor::{get_side_effects_from_package_json, SideEffects},
BoxLoader, CompilerContext, CompilerOptions, DependencyCategory, DependencyType, FactorizeArgs,
FactoryMeta, FuncUseCtx, GeneratorOptions, MissingModule, ModuleArgs, ModuleExt, ModuleFactory,
FactoryMeta, FuncUseCtx, GeneratorOptions, MissingModule, ModuleExt, ModuleFactory,
ModuleFactoryCreateData, ModuleFactoryResult, ModuleIdentifier, ModuleRule, ModuleRuleEnforce,
ModuleRuleUse, ModuleRuleUseLoader, ModuleType, NormalModule, NormalModuleAfterResolveArgs,
NormalModuleBeforeResolveArgs, ParserOptions, RawModule, Resolve, ResolveArgs, ResolveError,
ResolveOptionsWithDependencyType, ResolveResult, Resolver, ResolverFactory, ResourceData,
ResourceParsedData, SharedPluginDriver,
NormalModuleBeforeResolveArgs, NormalModuleCreateData, ParserOptions, RawModule, Resolve,
ResolveArgs, ResolveError, ResolveOptionsWithDependencyType, ResolveResult, Resolver,
ResolverFactory, ResourceData, ResourceParsedData, SharedPluginDriver,
};

#[derive(Debug)]
Expand Down Expand Up @@ -633,33 +632,30 @@ impl NormalModuleFactory {

self.context.module_type = Some(resolved_module_type);

let normal_module = NormalModule::new(
request,
user_request,
dependency.request().to_owned(),
resolved_module_type,
resolved_parser_and_generator,
resolved_parser_options,
resolved_generator_options,
match_resource_data,
resource_data,
resolved_resolve_options,
loaders,
self.context.options.clone(),
contains_inline,
);

let module = if let Some(module) = self
.plugin_driver
.module(ModuleArgs {
.create_module(NormalModuleCreateData {
dependency_type: data.dependency.dependency_type().clone(),
indentfiler: normal_module.identifier(),
lazy_visit_modules: self.context.lazy_visit_modules.clone(),
})
.await?
{
module
} else {
let normal_module = NormalModule::new(
request,
user_request,
dependency.request().to_owned(),
resolved_module_type,
resolved_parser_and_generator,
resolved_parser_options,
resolved_generator_options,
match_resource_data,
resource_data,
resolved_resolve_options,
loaders,
self.context.options.clone(),
contains_inline,
);
Box::new(normal_module)
};

Expand Down
16 changes: 10 additions & 6 deletions crates/rspack_core/src/plugin/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ use crate::{
AdditionalChunkRuntimeRequirementsArgs, AdditionalModuleRequirementsArgs, AssetEmittedArgs,
AssetInfo, BoxLoader, BoxModule, ChunkAssetArgs, ChunkHashArgs, Compilation, CompilationArgs,
CompilerOptions, ContentHashArgs, DoneArgs, FactorizeArgs, JsChunkHashArgs, MakeParam, Module,
ModuleArgs, ModuleFactoryResult, ModuleType, NormalModule, NormalModuleAfterResolveArgs,
NormalModuleBeforeResolveArgs, NormalModuleFactoryContext, OptimizeChunksArgs,
ParserAndGenerator, PluginContext, ProcessAssetsArgs, RenderArgs, RenderChunkArgs,
RenderManifestArgs, RenderModuleContentArgs, RenderStartupArgs, Resolver, SourceType,
ThisCompilationArgs,
ModuleFactoryResult, ModuleType, NormalModule, NormalModuleAfterResolveArgs,
NormalModuleBeforeResolveArgs, NormalModuleCreateData, NormalModuleFactoryContext,
OptimizeChunksArgs, ParserAndGenerator, PluginContext, ProcessAssetsArgs, RenderArgs,
RenderChunkArgs, RenderManifestArgs, RenderModuleContentArgs, RenderStartupArgs, Resolver,
SourceType, ThisCompilationArgs,
};

// use anyhow::{Context, Result};
Expand Down Expand Up @@ -126,7 +126,11 @@ pub trait Plugin: Debug + Send + Sync {
Ok(None)
}

async fn module(&self, _ctx: PluginContext, _args: &ModuleArgs) -> PluginModuleHookOutput {
async fn create_module(
&self,
_ctx: PluginContext,
_args: &NormalModuleCreateData,
) -> PluginModuleHookOutput {
Ok(None)
}

Expand Down
5 changes: 1 addition & 4 deletions crates/rspack_core/src/plugin/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,8 @@ pub struct FactorizeArgs<'me> {
}

#[derive(Debug, Clone)]
pub struct ModuleArgs {
pub indentfiler: ModuleIdentifier,
pub struct NormalModuleCreateData {
pub dependency_type: DependencyType,
// lazy compilation visit module
pub lazy_visit_modules: std::collections::HashSet<String>,
}

#[derive(Debug, Clone)]
Expand Down
10 changes: 5 additions & 5 deletions crates/rspack_core/src/plugin/plugin_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ use crate::{
AdditionalChunkRuntimeRequirementsArgs, AdditionalModuleRequirementsArgs, ApplyContext,
AssetEmittedArgs, BoxLoader, BoxedParserAndGeneratorBuilder, Chunk, ChunkAssetArgs,
ChunkContentHash, ChunkHashArgs, Compilation, CompilationArgs, CompilerOptions, Content,
ContentHashArgs, DoneArgs, FactorizeArgs, JsChunkHashArgs, MakeParam, Module, ModuleArgs,
ModuleType, NormalModule, NormalModuleAfterResolveArgs, NormalModuleBeforeResolveArgs,
NormalModuleFactoryContext, OptimizeChunksArgs, Plugin,
ContentHashArgs, DoneArgs, FactorizeArgs, JsChunkHashArgs, MakeParam, Module, ModuleType,
NormalModule, NormalModuleAfterResolveArgs, NormalModuleBeforeResolveArgs,
NormalModuleCreateData, NormalModuleFactoryContext, OptimizeChunksArgs, Plugin,
PluginAdditionalChunkRuntimeRequirementsOutput, PluginAdditionalModuleRequirementsOutput,
PluginBuildEndHookOutput, PluginChunkHashHookOutput, PluginCompilationHookOutput, PluginContext,
PluginFactorizeHookOutput, PluginJsChunkHashHookOutput, PluginMakeHookOutput,
Expand Down Expand Up @@ -321,10 +321,10 @@ impl PluginDriver {
Ok(None)
}

pub async fn module(&self, args: ModuleArgs) -> PluginModuleHookOutput {
pub async fn create_module(&self, args: NormalModuleCreateData) -> PluginModuleHookOutput {
for plugin in &self.plugins {
tracing::trace!("running render runtime:{}", plugin.name());
if let Some(module) = plugin.module(PluginContext::new(), &args).await? {
if let Some(module) = plugin.create_module(PluginContext::new(), &args).await? {
return Ok(Some(module));
}
}
Expand Down
40 changes: 22 additions & 18 deletions crates/rspack_plugin_runtime/src/lazy_compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use std::hash::Hash;
use async_trait::async_trait;
use rspack_core::{
rspack_sources::{RawSource, Source, SourceExt},
AsyncDependenciesBlockId, Compilation, DependenciesBlock, DependencyId, DependencyType, Module,
ModuleArgs, ModuleType, Plugin, PluginContext, PluginModuleHookOutput, RuntimeGlobals,
AsyncDependenciesBlockId, Compilation, DependenciesBlock, DependencyId, Module, ModuleType,
NormalModuleCreateData, Plugin, PluginContext, PluginModuleHookOutput, RuntimeGlobals,
RuntimeSpec, SourceType,
};
use rspack_core::{CodeGenerationResult, Context, ModuleIdentifier};
Expand Down Expand Up @@ -115,22 +115,26 @@ impl Plugin for LazyCompilationPlugin {
"LazyCompilationPlugin"
}

async fn module(&self, _ctx: PluginContext, args: &ModuleArgs) -> PluginModuleHookOutput {
if args.indentfiler.contains("rspack-dev-client")
|| args.lazy_visit_modules.contains(args.indentfiler.as_str())
{
return Ok(None);
}
if matches!(
args.dependency_type,
DependencyType::DynamicImport | DependencyType::Entry
) {
return Ok(Some(Box::new(LazyCompilationProxyModule {
module_identifier: args.indentfiler,
dependencies: Vec::new(),
blocks: Vec::new(),
})));
}
async fn create_module(
&self,
_ctx: PluginContext,
_args: &NormalModuleCreateData,
) -> PluginModuleHookOutput {
// if args.indentfiler.contains("rspack-dev-client")
// || args.lazy_visit_modules.contains(args.indentfiler.as_str())
// {
// return Ok(None);
// }
// if matches!(
// args.dependency_type,
// DependencyType::DynamicImport | DependencyType::Entry
// ) {
// return Ok(Some(Box::new(LazyCompilationProxyModule {
// module_identifier: args.indentfiler,
// dependencies: Vec::new(),
// blocks: Vec::new(),
// })));
// }

Ok(None)
}
Expand Down

0 comments on commit 3ae4b91

Please sign in to comment.