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(rspack): use cow-utils instead #7908

Merged
merged 1 commit into from
Sep 18, 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
26 changes: 26 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ async-trait = { version = "0.1.79" }
bitflags = { version = "2.5.0" }
camino = { version = "1.1.8" }
concat-string = { version = "1.0.1" }
cow-utils = { version = "0.1.3" }
css-module-lexer = { version = "0.0.14" }
dashmap = { version = "5.5.3" }
derivative = { version = "2.2.0" }
Expand Down
9 changes: 9 additions & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
allow-dbg-in-tests = true
allow-unwrap-in-tests = true

disallowed-methods = [
{ path = "str::to_ascii_lowercase", reason = "To avoid memory allocation, use `cow_utils::CowUtils::cow_to_ascii_lowercase` instead." },
{ path = "str::to_ascii_uppercase", reason = "To avoid memory allocation, use `cow_utils::CowUtils::cow_to_ascii_uppercase` instead." },
{ path = "str::to_lowercase", reason = "To avoid memory allocation, use `cow_utils::CowUtils::cow_to_lowercase` instead." },
{ path = "str::to_uppercase", reason = "To avoid memory allocation, use `cow_utils::CowUtils::cow_to_uppercase` instead." },
{ path = "str::replace", reason = "To avoid memory allocation, use `cow_utils::CowUtils::replace` instead." },
{ path = "str::replacen", reason = "To avoid memory allocation, use `cow_utils::CowUtils::replacen` instead." },
]
1 change: 1 addition & 0 deletions crates/node_binding/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ rspack_tracing = { version = "0.1.0", path = "../rspack_tracing" }
tokio = { workspace = true, features = ["rt", "rt-multi-thread"] }

async-trait = { workspace = true }
cow-utils = { workspace = true }
once_cell = { workspace = true }
tracing = { workspace = true }

Expand Down
7 changes: 6 additions & 1 deletion crates/node_binding/src/plugins/interceptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{
};

use async_trait::async_trait;
use cow_utils::CowUtils;
use napi::{
bindgen_prelude::{Buffer, FromNapiValue, Promise, ToNapiValue},
Env, JsFunction, NapiRaw,
Expand Down Expand Up @@ -1161,7 +1162,11 @@ impl CompilationRuntimeModule for CompilationRuntimeModuleTap {
),
module_identifier: module.identifier().to_string(),
constructor_name: module.get_constructor_name(),
name: module.name().to_string().replace("webpack/runtime/", ""),
name: module
.name()
.as_str()
.cow_replace("webpack/runtime/", "")
.into_owned(),
},
chunk: JsChunk::from(chunk),
};
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_binding_options/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ ignored = ["tracing"]

[dependencies]
async-trait = { workspace = true }
cow-utils = { workspace = true }
derivative = { workspace = true }
glob = { workspace = true }
napi = { workspace = true, features = ["async", "tokio_rt", "serde-json", "anyhow"] }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use cow_utils::CowUtils;
use derivative::Derivative;
use napi::{bindgen_prelude::Buffer, Either};
use napi_derive::napi;
Expand Down Expand Up @@ -110,7 +111,7 @@ impl From<RawCopyPattern> for CopyPattern {
}),
context: context.map(Into::into),
to_type: if let Some(to_type) = to_type {
match to_type.to_lowercase().as_str() {
match to_type.cow_to_lowercase().as_ref() {
"dir" => Some(ToType::Dir),
"file" => Some(ToType::File),
"template" => Some(ToType::Template),
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_binding_values/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ name = "rspack_binding_values"
repository = "https://github.com/web-infra-dev/rspack"
version = "0.1.0"
[dependencies]
cow-utils = { workspace = true }
futures = { workspace = true }
heck = { workspace = true }
napi = { workspace = true, features = ["async", "tokio_rt", "serde-json", "anyhow"] }
Expand Down
7 changes: 4 additions & 3 deletions crates/rspack_binding_values/src/html.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::HashMap;

use cow_utils::CowUtils;
use napi::Either;
use napi_derive::napi;
use rspack_plugin_html::{
Expand Down Expand Up @@ -54,13 +55,13 @@ impl From<JsHtmlPluginTag> for HtmlPluginTag {
.filter_map(|(key, value)| {
value.as_ref().and_then(|v| match v {
Either::A(x) => Some(HtmlPluginAttribute {
attr_name: key.to_ascii_lowercase(),
attr_value: Some(x.to_ascii_lowercase()),
attr_name: key.cow_to_ascii_lowercase().into_owned(),
attr_value: Some(x.cow_to_ascii_lowercase().into_owned()),
}),
Either::B(x) => {
if *x {
Some(HtmlPluginAttribute {
attr_name: key.to_ascii_lowercase(),
attr_name: key.cow_to_ascii_lowercase().into_owned(),
attr_value: None,
})
} else {
Expand Down
6 changes: 4 additions & 2 deletions crates/rspack_binding_values/src/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::sync::LazyLock;

use cow_utils::CowUtils;
use heck::{ToLowerCamelCase, ToSnakeCase};
use napi_derive::napi;
use rspack_core::RuntimeGlobals;
Expand Down Expand Up @@ -129,9 +130,10 @@ impl JsAdditionalTreeRuntimeRequirementsResult {
let mut runtime_requirements = RuntimeGlobals::default();

for item in self.runtime_requirements.value.iter() {
let name = item.to_snake_case().to_uppercase();
let snake_case = item.to_snake_case();
let name = snake_case.cow_to_uppercase();

if let Some(item) = RUNTIME_GLOBAL_MAP.1.get(&name) {
if let Some(item) = RUNTIME_GLOBAL_MAP.1.get(name.as_ref()) {
runtime_requirements.extend(*item);
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ anymap = { workspace = true }
async-recursion = { workspace = true }
async-trait = { workspace = true }
bitflags = { workspace = true }
cow-utils = { workspace = true }
dashmap = { workspace = true, features = ["rayon"] }
derivative = { workspace = true }
dyn-clone = "1.0.17"
Expand Down
20 changes: 11 additions & 9 deletions crates/rspack_core/src/context_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::path::PathBuf;
use std::sync::LazyLock;
use std::{borrow::Cow, fs, hash::Hash, sync::Arc};

use cow_utils::CowUtils;
use indoc::formatdoc;
use itertools::Itertools;
use regex::{Captures, Regex};
Expand Down Expand Up @@ -1026,12 +1027,9 @@ impl ContextModule {

// FIXME: nodejs resolver return path of context, sometimes is '/a/b', sometimes is '/a/b/'
let relative_path = {
let mut path_str = path_str.to_owned();
let p = path_str
.drain(ctx.len()..)
.collect::<String>()
.replace('\\', "/");
if p.starts_with('/') {
let path_str = path_str.to_owned().drain(ctx.len()..).collect::<String>();
let p = path_str.cow_replace('\\', "/");
if p.as_ref().starts_with('/') {
format!(".{p}")
} else {
format!("./{p}")
Expand Down Expand Up @@ -1301,9 +1299,13 @@ fn alternative_requests(
items.push(item.clone());
// TODO resolveOptions.modules can be array
for module in resolve_options.modules() {
let dir = module.replace('\\', "/");
let full_path: String = format!("{}{}", item.context.replace('\\', "/"), &item.request[1..]);
if full_path.starts_with(&dir) {
let dir = module.cow_replace('\\', "/");
let full_path: String = format!(
"{}{}",
item.context.cow_replace('\\', "/"),
&item.request[1..]
);
if full_path.starts_with(dir.as_ref()) {
items.push(AlternativeRequest::new(
item.context.clone(),
full_path[(dir.len() + 1)..].to_string(),
Expand Down
10 changes: 6 additions & 4 deletions crates/rspack_core/src/context_module_factory.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::sync::Arc;
use std::{borrow::Cow, sync::Arc};

use cow_utils::CowUtils;
use rspack_error::{error, Result};
use rspack_hook::define_hook;
use rspack_paths::Utf8PathBuf;
Expand Down Expand Up @@ -164,15 +165,16 @@ impl ContextModuleFactory {
let (loader_request, specifier) = match request.rfind('!') {
Some(idx) => {
let mut loaders_prefix = String::new();
let mut loaders_request = request[..idx + 1].to_string();
let mut i = 0;

let loaders_request = Cow::Borrowed(&request[..idx + 1]);
while i < loaders_request.len() && loaders_request.chars().nth(i) == Some('!') {
loaders_prefix.push('!');
i += 1;
}
loaders_request = loaders_request[i..]
let loaders_request = loaders_request.as_ref()[i..]
.trim_end_matches('!')
.replace("!!", "!");
.cow_replace("!!", "!");

let loaders = if loaders_request.is_empty() {
vec![]
Expand Down
4 changes: 3 additions & 1 deletion crates/rspack_core/src/utils/template.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use cow_utils::CowUtils;

pub fn to_normal_comment(str: &str) -> String {
if str.is_empty() {
return String::new();
}
format!("/* {} */", str.replace("*/", "* /"))
format!("/* {} */", str.cow_replace("*/", "* /"))
}
1 change: 1 addition & 0 deletions crates/rspack_error/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ version = "0.1.0"

[dependencies]
anyhow = { workspace = true, features = ["backtrace"] }
cow-utils = { workspace = true }
derivative = { workspace = true }
futures = { workspace = true }
miette = { version = "5", features = ["fancy"] }
Expand Down
Loading
Loading