Skip to content

Commit

Permalink
refactor(rust): improve replace_all_placeholder (#9289)
Browse files Browse the repository at this point in the history
refactor(rust): improve replace_all_placeholder
  • Loading branch information
shulaoda authored Feb 14, 2025
1 parent 5458f29 commit 7d4c6c2
Showing 1 changed file with 41 additions and 21 deletions.
62 changes: 41 additions & 21 deletions crates/rspack_core/src/utils/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ pub fn include_hash(filename: &str, hashes: &HashSet<String>) -> bool {
}

pub trait Replacer {
fn get_replacer(&mut self, hash_len: Option<usize>) -> Cow<'_, str>;
fn get(&mut self, hash_len: Option<usize>) -> Cow<'_, str>;
}

impl Replacer for &str {
#[inline]
fn get_replacer(&mut self, _: Option<usize>) -> Cow<'_, str> {
fn get(&mut self, _: Option<usize>) -> Cow<'_, str> {
Cow::Borrowed(self)
}
}

impl Replacer for &String {
#[inline]
fn get_replacer(&mut self, _: Option<usize>) -> Cow<'_, str> {
fn get(&mut self, _: Option<usize>) -> Cow<'_, str> {
Cow::Borrowed(self.as_str())
}
}
Expand All @@ -59,7 +59,7 @@ where
S: AsRef<str>,
{
#[inline]
fn get_replacer(&mut self, hash_len: Option<usize>) -> Cow<'_, str> {
fn get(&mut self, hash_len: Option<usize>) -> Cow<'_, str> {
Cow::Owned((*self)(hash_len).as_ref().to_string())
}
}
Expand All @@ -86,20 +86,33 @@ pub fn replace_all_placeholder<'a>(
}

let start_offset = start + offset;
if let Some(end) = pattern[start_offset..].find(']') {
let end = start_offset + end;
let pat_temp = &pattern[start_offset..];

let replacer = replacer.get_replacer(
pattern[start_offset..end]
.strip_prefix(':')
.and_then(|n| n.parse::<usize>().ok()),
);
let (end, len) = match pat_temp.as_bytes().first() {
Some(b']') => (start_offset, None),
Some(b':') => {
if let Some(end) = pat_temp.find(']') {
let end = start_offset + end;
let len = pattern[start_offset + 1..end].parse::<usize>().ok();

result.push_str(&pattern[ending..start]);
result.push_str(replacer.as_ref());
if len.is_none() {
continue;
}

ending = end + 1;
}
(end, len)
} else {
continue;
}
}
_ => continue,
};

let replacer = replacer.get(len);

result.push_str(&pattern[ending..start]);
result.push_str(replacer.as_ref());

ending = end + 1;
}

if ending < pattern.len() {
Expand All @@ -111,10 +124,17 @@ pub fn replace_all_placeholder<'a>(

#[test]
fn test_replace_all_placeholder() {
let result = replace_all_placeholder("hello-[hash].js", "[hash]", "abc");
assert_eq!(result, "hello-abc.js");
let result = replace_all_placeholder("hello-[hash]-[hash:5].js", "[hash]", |n: Option<usize>| {
&"abcdefgh"[..n.unwrap_or(8)]
});
assert_eq!(result, "hello-abcdefgh-abcde.js");
let result = replace_all_placeholder(
"hello-[hash]-[hash:-]-[hash_name]-[hash:1]-[hash:].js",
"[hash]",
"abc",
);
assert_eq!(result, "hello-abc-[hash:-]-[hash_name]-abc-[hash:].js");

let result = replace_all_placeholder(
"hello-[hash]-[hash:5]-[hash_name]-[hash:o].js",
"[hash]",
|n: Option<usize>| &"abcdefgh"[..n.unwrap_or(8)],
);
assert_eq!(result, "hello-abcdefgh-abcde-[hash_name]-[hash:o].js");
}

2 comments on commit 7d4c6c2

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented on 7d4c6c2 Feb 14, 2025

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 (2025-02-14 05524cd) Current Change
10000_big_production-mode_disable-minimize + exec 37.9 s ± 263 ms 39.6 s ± 960 ms +4.53 %
10000_development-mode + exec 1.83 s ± 21 ms 1.9 s ± 193 ms +3.83 %
10000_development-mode_hmr + exec 680 ms ± 7.1 ms 687 ms ± 3.3 ms +0.96 %
10000_production-mode + exec 2.29 s ± 68 ms 2.4 s ± 264 ms +5.02 %
10000_production-mode_persistent-cold + exec 2.44 s ± 66 ms 2.47 s ± 76 ms +1.24 %
10000_production-mode_persistent-hot + exec 1.68 s ± 135 ms 1.68 s ± 66 ms -0.18 %
arco-pro_development-mode + exec 1.8 s ± 164 ms 1.76 s ± 77 ms -2.28 %
arco-pro_development-mode_hmr + exec 388 ms ± 1.9 ms 388 ms ± 1.6 ms 0.00 %
arco-pro_production-mode + exec 3.6 s ± 123 ms 3.7 s ± 172 ms +2.68 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.63 s ± 134 ms 3.7 s ± 132 ms +1.98 %
arco-pro_production-mode_persistent-cold + exec 3.79 s ± 176 ms 3.85 s ± 107 ms +1.72 %
arco-pro_production-mode_persistent-hot + exec 2.37 s ± 75 ms 2.41 s ± 103 ms +1.99 %
arco-pro_production-mode_traverse-chunk-modules + exec 3.62 s ± 183 ms 3.72 s ± 225 ms +2.68 %
large-dyn-imports_development-mode + exec 2.12 s ± 18 ms 2.15 s ± 86 ms +1.53 %
large-dyn-imports_production-mode + exec 2.15 s ± 48 ms 2.2 s ± 59 ms +2.24 %
threejs_development-mode_10x + exec 1.58 s ± 69 ms 1.55 s ± 24 ms -1.58 %
threejs_development-mode_10x_hmr + exec 785 ms ± 7.9 ms 770 ms ± 13 ms -1.85 %
threejs_production-mode_10x + exec 5.25 s ± 50 ms 5.31 s ± 172 ms +1.25 %
threejs_production-mode_10x_persistent-cold + exec 5.31 s ± 95 ms 5.34 s ± 50 ms +0.64 %
threejs_production-mode_10x_persistent-hot + exec 4.52 s ± 102 ms 4.59 s ± 299 ms +1.61 %
10000_big_production-mode_disable-minimize + rss memory 8704 MiB ± 49.5 MiB 8723 MiB ± 54.4 MiB +0.22 %
10000_development-mode + rss memory 636 MiB ± 27.9 MiB 647 MiB ± 9.82 MiB +1.67 %
10000_development-mode_hmr + rss memory 1223 MiB ± 158 MiB 1259 MiB ± 222 MiB +2.97 %
10000_production-mode + rss memory 628 MiB ± 14.1 MiB 625 MiB ± 29.8 MiB -0.55 %
10000_production-mode_persistent-cold + rss memory 735 MiB ± 12.5 MiB 746 MiB ± 25.2 MiB +1.56 %
10000_production-mode_persistent-hot + rss memory 719 MiB ± 3.87 MiB 721 MiB ± 17 MiB +0.31 %
arco-pro_development-mode + rss memory 583 MiB ± 24.4 MiB 577 MiB ± 23.3 MiB -1.00 %
arco-pro_development-mode_hmr + rss memory 644 MiB ± 77 MiB 647 MiB ± 120 MiB +0.43 %
arco-pro_production-mode + rss memory 724 MiB ± 7.62 MiB 712 MiB ± 18.4 MiB -1.64 %
arco-pro_production-mode_generate-package-json-webpack-plugin + rss memory 728 MiB ± 15.8 MiB 735 MiB ± 43.1 MiB +0.88 %
arco-pro_production-mode_persistent-cold + rss memory 843 MiB ± 21.6 MiB 848 MiB ± 58.8 MiB +0.54 %
arco-pro_production-mode_persistent-hot + rss memory 710 MiB ± 22.1 MiB 711 MiB ± 41 MiB +0.07 %
arco-pro_production-mode_traverse-chunk-modules + rss memory 709 MiB ± 8.18 MiB 709 MiB ± 30 MiB -0.04 %
large-dyn-imports_development-mode + rss memory 643 MiB ± 3.77 MiB 640 MiB ± 3.36 MiB -0.41 %
large-dyn-imports_production-mode + rss memory 525 MiB ± 6.6 MiB 526 MiB ± 7.37 MiB +0.06 %
threejs_development-mode_10x + rss memory 552 MiB ± 18.3 MiB 550 MiB ± 6.16 MiB -0.33 %
threejs_development-mode_10x_hmr + rss memory 1105 MiB ± 90.1 MiB 1196 MiB ± 65.4 MiB +8.24 %
threejs_production-mode_10x + rss memory 841 MiB ± 33 MiB 833 MiB ± 34.4 MiB -0.95 %
threejs_production-mode_10x_persistent-cold + rss memory 968 MiB ± 36.3 MiB 956 MiB ± 54.7 MiB -1.30 %
threejs_production-mode_10x_persistent-hot + rss memory 866 MiB ± 43.1 MiB 843 MiB ± 62.9 MiB -2.69 %

Threshold exceeded: ["10000_production-mode + exec"]

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented on 7d4c6c2 Feb 14, 2025

Choose a reason for hiding this comment

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

📝 Ecosystem CI detail: Open

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

Please sign in to comment.