Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: reduce alloc for bailout reason #7118

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions crates/rspack_core/src/external_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,14 +349,11 @@ impl Module for ExternalModule {
&self,
_mg: &ModuleGraph,
_cg: &ChunkGraph,
) -> Option<String> {
) -> Option<Cow<'static, str>> {
match self.external_type.as_ref() {
"amd" | "umd" | "amd-require" | "umd2" | "system" | "jsonp" => {
// return `${this.externalType} externals can't be concatenated`;
Some(format!(
"{} externals can't be concatenated",
self.external_type
))
Some(format!("{} externals can't be concatenated", self.external_type).into())
}
_ => None,
}
Expand Down
13 changes: 8 additions & 5 deletions crates/rspack_core/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,14 @@ pub trait Module:
&self,
_mg: &ModuleGraph,
_cg: &ChunkGraph,
) -> Option<String> {
Some(format!(
"Module Concatenation is not implemented for {}",
self.module_type()
))
) -> Option<Cow<'static, str>> {
Some(
format!(
"Module Concatenation is not implemented for {}",
self.module_type()
)
.into(),
)
}

/// Resolve options matched by module rules.
Expand Down
6 changes: 5 additions & 1 deletion crates/rspack_core/src/normal_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,11 @@ impl Module for NormalModule {
ConnectionState::Bool(true)
}

fn get_concatenation_bailout_reason(&self, mg: &ModuleGraph, cg: &ChunkGraph) -> Option<String> {
fn get_concatenation_bailout_reason(
&self,
mg: &ModuleGraph,
cg: &ChunkGraph,
) -> Option<Cow<'static, str>> {
self
.parser_and_generator
.get_concatenation_bailout_reason(self, mg, cg)
Expand Down
3 changes: 2 additions & 1 deletion crates/rspack_core/src/parser_and_generator.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::any::Any;
use std::borrow::Cow;
use std::fmt::Debug;

use derivative::Derivative;
Expand Down Expand Up @@ -102,7 +103,7 @@ pub trait ParserAndGenerator: Send + Sync + Debug + AsAny {
_module: &dyn Module,
_mg: &ModuleGraph,
_cg: &ChunkGraph,
) -> Option<String>;
) -> Option<Cow<'static, str>>;
}

impl dyn ParserAndGenerator + '_ {
Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_plugin_asset/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![feature(let_chains)]

use std::hash::Hasher;
use std::{borrow::Cow, hash::Hasher};

use async_trait::async_trait;
use rayon::prelude::*;
Expand Down Expand Up @@ -457,7 +457,7 @@ impl ParserAndGenerator for AssetParserAndGenerator {
_module: &dyn rspack_core::Module,
_mg: &ModuleGraph,
_cg: &ChunkGraph,
) -> Option<String> {
) -> Option<Cow<'static, str>> {
None
}
}
Expand Down
8 changes: 4 additions & 4 deletions crates/rspack_plugin_css/src/parser_and_generator/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::borrow::Cow;

use indexmap::{IndexMap, IndexSet};
use once_cell::sync::Lazy;
use regex::Regex;
Expand Down Expand Up @@ -479,10 +481,8 @@ impl ParserAndGenerator for CssParserAndGenerator {
_module: &dyn rspack_core::Module,
_mg: &ModuleGraph,
_cg: &ChunkGraph,
) -> Option<String> {
Some(String::from(
"Module Concatenation is not implemented for CssParserAndGenerator",
))
) -> Option<Cow<'static, str>> {
Some("Module Concatenation is not implemented for CssParserAndGenerator".into())
}
}

Expand Down
11 changes: 6 additions & 5 deletions crates/rspack_plugin_javascript/src/parser_and_generator/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::borrow::Cow;
use std::sync::Arc;

use itertools::Itertools;
Expand Down Expand Up @@ -329,15 +330,15 @@ impl ParserAndGenerator for JavaScriptParserAndGenerator {
module: &dyn rspack_core::Module,
_mg: &ModuleGraph,
_cg: &ChunkGraph,
) -> Option<String> {
) -> Option<Cow<'static, str>> {
// Only harmony modules are valid for optimization
if module.build_meta().is_none()
|| module
.build_meta()
.map(|meta| meta.exports_type != BuildMetaExportsType::Namespace)
.unwrap_or_default()
{
return Some(String::from("Module is not an ECMAScript module"));
return Some("Module is not an ECMAScript module".into());
}

if let Some(deps) = module.get_presentational_dependencies() {
Expand All @@ -348,16 +349,16 @@ impl ParserAndGenerator for JavaScriptParserAndGenerator {
.downcast_ref::<HarmonyCompatibilityDependency>()
.is_some()
}) {
return Some(String::from("Module is not an ECMAScript module"));
return Some("Module is not an ECMAScript module".into());
}
} else {
return Some(String::from("Module is not an ECMAScript module"));
return Some("Module is not an ECMAScript module".into());
}

if let Some(info) = module.build_info()
&& let Some(bailout) = info.module_concatenation_bailout.as_deref()
{
return Some(format!("Module uses {bailout}",));
return Some(format!("Module uses {bailout}").into());
}
None
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![allow(clippy::only_used_in_recursion)]
use std::borrow::Cow;
use std::collections::hash_map::DefaultHasher;
use std::collections::VecDeque;
use std::hash::Hasher;
Expand Down Expand Up @@ -98,7 +99,7 @@ impl ConcatConfiguration {
#[plugin]
#[derive(Debug, Default)]
pub struct ModuleConcatenationPlugin {
bailout_reason_map: FxDashMap<ModuleIdentifier, String>,
bailout_reason_map: FxDashMap<ModuleIdentifier, Cow<'static, str>>,
}

impl ModuleConcatenationPlugin {
Expand Down Expand Up @@ -128,13 +129,18 @@ impl ModuleConcatenationPlugin {
}
}

fn set_bailout_reason(&self, module: &ModuleIdentifier, reason: String, mg: &mut ModuleGraph) {
fn set_bailout_reason(
&self,
module: &ModuleIdentifier,
reason: Cow<'static, str>,
mg: &mut ModuleGraph,
) {
self.set_inner_bailout_reason(module, reason.clone());
mg.get_optimization_bailout_mut(module)
.push(format_bailout_reason(&reason));
}

fn set_inner_bailout_reason(&self, module: &ModuleIdentifier, reason: String) {
fn set_inner_bailout_reason(&self, module: &ModuleIdentifier, reason: Cow<'static, str>) {
self.bailout_reason_map.insert(*module, reason);
}

Expand All @@ -145,7 +151,7 @@ impl ModuleConcatenationPlugin {
dashmap::mapref::one::Ref<
'_,
rspack_identifier::Identifier,
String,
Cow<'static, str>,
std::hash::BuildHasherDefault<rustc_hash::FxHasher>,
>,
> {
Expand Down Expand Up @@ -578,7 +584,7 @@ impl ModuleConcatenationPlugin {
.is_async(&module_id)
.expect("should have async result")
{
bailout_reason.push("Module is async".to_string());
bailout_reason.push("Module is async".into());
return (false, false, module_id, bailout_reason);
}

Expand All @@ -587,11 +593,11 @@ impl ModuleConcatenationPlugin {
.expect("should have build info")
.strict
{
bailout_reason.push("Module is not in strict mode".to_string());
bailout_reason.push("Module is not in strict mode".into());
return (false, false, module_id, bailout_reason);
}
if number_of_module_chunks == 0 {
bailout_reason.push("Module is not in any chunk".to_string());
bailout_reason.push("Module is not in any chunk".into());
return (false, false, module_id, bailout_reason);
}

Expand Down Expand Up @@ -626,9 +632,10 @@ impl ModuleConcatenationPlugin {
// &mut module_graph,
// );

bailout_reason.push(format!(
"Reexports in this module do not have a static target ({cur_bailout_reason})"
));
bailout_reason.push(
format!("Reexports in this module do not have a static target ({cur_bailout_reason})")
.into(),
);

return (false, false, module_id, bailout_reason);
}
Expand Down Expand Up @@ -665,9 +672,8 @@ impl ModuleConcatenationPlugin {
// format!("List of module exports is dynamic ({bailout_reason})"),
// &mut module_graph,
// );
bailout_reason.push(format!(
"List of module exports is dynamic ({cur_bailout_reason})"
));
bailout_reason
.push(format!("List of module exports is dynamic ({cur_bailout_reason})").into());
can_be_root = false;
}

Expand All @@ -678,7 +684,7 @@ impl ModuleConcatenationPlugin {
// &mut module_graph,
// );
can_be_inner = false;
bailout_reason.push("Module is an entry point".to_string());
bailout_reason.push("Module is an entry point".into());
}
(can_be_root, can_be_inner, module_id, bailout_reason)
// if can_be_root {
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_plugin_json/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ impl ParserAndGenerator for JsonParserAndGenerator {
_module: &dyn Module,
_mg: &ModuleGraph,
_cg: &ChunkGraph,
) -> Option<String> {
) -> Option<Cow<'static, str>> {
None
}
}
Expand Down
7 changes: 3 additions & 4 deletions crates/rspack_plugin_wasm/src/parser_and_generator.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::borrow::Cow;
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

Expand Down Expand Up @@ -288,10 +289,8 @@ impl ParserAndGenerator for AsyncWasmParserAndGenerator {
_module: &dyn Module,
_mg: &rspack_core::ModuleGraph,
_cg: &rspack_core::ChunkGraph,
) -> Option<String> {
Some(String::from(
"Module Concatenation is not implemented for AsyncWasmParserAndGenerator",
))
) -> Option<Cow<'static, str>> {
Some("Module Concatenation is not implemented for AsyncWasmParserAndGenerator".into())
}
}

Expand Down
Loading