From 3ae4b91047f78f7cb58c10f6744c5986fcc3d300 Mon Sep 17 00:00:00 2001 From: bohan Date: Fri, 17 Nov 2023 15:52:56 +0800 Subject: [PATCH] perf: minimize memory allocation (#4689) * perf: minimize memory allocation * chore: rename `module` to `create_module` --- .../src/code_generation_results.rs | 3 +- crates/rspack_core/src/dependencies_block.rs | 3 +- crates/rspack_core/src/exports_info.rs | 5 +-- crates/rspack_core/src/normal_module.rs | 16 ++++--- .../rspack_core/src/normal_module_factory.rs | 44 +++++++++---------- crates/rspack_core/src/plugin/api.rs | 16 ++++--- crates/rspack_core/src/plugin/args.rs | 5 +-- .../rspack_core/src/plugin/plugin_driver.rs | 10 ++--- .../src/lazy_compilation.rs | 40 +++++++++-------- 9 files changed, 72 insertions(+), 70 deletions(-) diff --git a/crates/rspack_core/src/code_generation_results.rs b/crates/rspack_core/src/code_generation_results.rs index 96751d5c787..fd1e3190250 100644 --- a/crates/rspack_core/src/code_generation_results.rs +++ b/crates/rspack_core/src/code_generation_results.rs @@ -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; @@ -146,7 +145,7 @@ impl Default for CodeGenResultId { } } -pub static CODE_GEN_RESULT_ID: Lazy = Lazy::new(|| AtomicU32::new(0)); +pub static CODE_GEN_RESULT_ID: AtomicU32 = AtomicU32::new(0); #[derive(Debug, Default)] pub struct CodeGenerationResults { diff --git a/crates/rspack_core/src/dependencies_block.rs b/crates/rspack_core/src/dependencies_block.rs index 10301aa1de3..51171db1198 100644 --- a/crates/rspack_core/src/dependencies_block.rs +++ b/crates/rspack_core/src/dependencies_block.rs @@ -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}; @@ -15,7 +14,7 @@ pub trait DependenciesBlock { fn get_dependencies(&self) -> &[DependencyId]; } -static ASYNC_DEPENDENCIES_BLOCK_ID: Lazy = Lazy::new(|| AtomicU32::new(0)); +static ASYNC_DEPENDENCIES_BLOCK_ID: AtomicU32 = AtomicU32::new(0); fn get_async_dependencies_block_id() -> AsyncDependenciesBlockId { AsyncDependenciesBlockId(Identifier::from( diff --git a/crates/rspack_core/src/exports_info.rs b/crates/rspack_core/src/exports_info.rs index 33dfbb7c8ba..c234eb1a6ca 100644 --- a/crates/rspack_core/src/exports_info.rs +++ b/crates/rspack_core/src/exports_info.rs @@ -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; @@ -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 = 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) { @@ -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 = 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) { diff --git a/crates/rspack_core/src/normal_module.rs b/crates/rspack_core/src/normal_module.rs index c0adfb9368a..556264605a4 100644 --- a/crates/rspack_core/src/normal_module.rs +++ b/crates/rspack_core/src/normal_module.rs @@ -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, @@ -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, diff --git a/crates/rspack_core/src/normal_module_factory.rs b/crates/rspack_core/src/normal_module_factory.rs index 815d3c972d5..2121ea94223 100644 --- a/crates/rspack_core/src/normal_module_factory.rs +++ b/crates/rspack_core/src/normal_module_factory.rs @@ -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; @@ -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)] @@ -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) }; diff --git a/crates/rspack_core/src/plugin/api.rs b/crates/rspack_core/src/plugin/api.rs index 7fd0f22f6f3..999db45cb23 100644 --- a/crates/rspack_core/src/plugin/api.rs +++ b/crates/rspack_core/src/plugin/api.rs @@ -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}; @@ -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) } diff --git a/crates/rspack_core/src/plugin/args.rs b/crates/rspack_core/src/plugin/args.rs index 0a884b7e856..559a103dbe5 100644 --- a/crates/rspack_core/src/plugin/args.rs +++ b/crates/rspack_core/src/plugin/args.rs @@ -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, } #[derive(Debug, Clone)] diff --git a/crates/rspack_core/src/plugin/plugin_driver.rs b/crates/rspack_core/src/plugin/plugin_driver.rs index 3abdfb95afd..7a08184e73f 100644 --- a/crates/rspack_core/src/plugin/plugin_driver.rs +++ b/crates/rspack_core/src/plugin/plugin_driver.rs @@ -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, @@ -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)); } } diff --git a/crates/rspack_plugin_runtime/src/lazy_compilation.rs b/crates/rspack_plugin_runtime/src/lazy_compilation.rs index 0eaeb873f90..49cf4bb96df 100644 --- a/crates/rspack_plugin_runtime/src/lazy_compilation.rs +++ b/crates/rspack_plugin_runtime/src/lazy_compilation.rs @@ -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}; @@ -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) }