From 0c958937394de4d6d0728cfeb89f31728984eccf Mon Sep 17 00:00:00 2001 From: zak <8143258+ZakisM@users.noreply.github.com> Date: Wed, 15 Nov 2023 20:46:16 +0000 Subject: [PATCH 1/2] perf(es/minifier): Improve format.inline_script --- crates/swc_ecma_codegen/src/lib.rs | 65 +++++++++++------------------- 1 file changed, 23 insertions(+), 42 deletions(-) diff --git a/crates/swc_ecma_codegen/src/lib.rs b/crates/swc_ecma_codegen/src/lib.rs index c4d33bb47091..cb5136da61ce 100644 --- a/crates/swc_ecma_codegen/src/lib.rs +++ b/crates/swc_ecma_codegen/src/lib.rs @@ -77,51 +77,32 @@ where fn replace_close_inline_script(raw: &str) -> Cow { let chars = raw.as_bytes(); - let pattern_len = 8; // - let matched = |i: usize| { - if i + pattern_len >= chars.len() { - return false; - } - chars[i] == b'<' - && chars[i + 1] == b'/' - && (chars[i + 2] == b's' || chars[i + 2] == b'S') - && (chars[i + 3] == b'c' || chars[i + 3] == b'C') - && (chars[i + 4] == b'r' || chars[i + 4] == b'R') - && (chars[i + 5] == b'i' || chars[i + 5] == b'I') - && (chars[i + 6] == b'p' || chars[i + 6] == b'P') - && (chars[i + 7] == b't' || chars[i + 7] == b'T') - && (chars[i + 8] == b'>' - || chars[i + 8] == b' ' - || chars[i + 8] == b'\t' - || chars[i + 8] == b'\n' - || chars[i + 8] == b'\x0C' - || chars[i + 8] == b'\r') - }; - let mut matched_list = (0..chars.len()).filter(|&i| matched(i)).peekable(); - if matched_list.peek().is_none() { + + let mut matched_indexes = chars + .iter() + .enumerate() + .filter(|(index, byte)| { + byte == &&b'<' + && index + 8 < chars.len() + && chars[index + 1..index + 8].eq_ignore_ascii_case(b"/script") + && matches!( + chars[index + 8], + b'>' | b' ' | b'\t' | b'\n' | b'\x0C' | b'\r' + ) + }) + .map(|(index, _)| index) + .peekable(); + + if matched_indexes.peek().is_none() { return Cow::Borrowed(raw); } - let mut result = String::new(); - let mut last_end = 0; - for start in matched_list { - for c in unsafe { chars.get_unchecked(last_end..start) } { - result.push(*c as char); - } - for (index, c) in unsafe { chars.get_unchecked(start..start + pattern_len) } - .iter() - .enumerate() - { - if index == 1 { - // " <\/s" - result.push('\\'); - } - result.push(*c as char); - } - last_end = start + pattern_len; - } - for c in unsafe { chars.get_unchecked(last_end..chars.len()) } { - result.push(*c as char); + + let mut result = String::from(raw); + + for (offset, i) in matched_indexes.enumerate() { + result.insert(i + 1 + offset, '\\'); } + Cow::Owned(result) } From b2c018e70736a1f552e71194c1c179dae4bce2cf Mon Sep 17 00:00:00 2001 From: zak <8143258+ZakisM@users.noreply.github.com> Date: Wed, 15 Nov 2023 20:56:54 +0000 Subject: [PATCH 2/2] perf(es/minifier): Improve format.inline_script --- crates/swc_ecma_codegen/src/lib.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/crates/swc_ecma_codegen/src/lib.rs b/crates/swc_ecma_codegen/src/lib.rs index cb5136da61ce..441c3a9c396e 100644 --- a/crates/swc_ecma_codegen/src/lib.rs +++ b/crates/swc_ecma_codegen/src/lib.rs @@ -77,16 +77,17 @@ where fn replace_close_inline_script(raw: &str) -> Cow { let chars = raw.as_bytes(); + let pattern_len = 8; // let mut matched_indexes = chars .iter() .enumerate() .filter(|(index, byte)| { byte == &&b'<' - && index + 8 < chars.len() - && chars[index + 1..index + 8].eq_ignore_ascii_case(b"/script") + && index + pattern_len < chars.len() + && chars[index + 1..index + pattern_len].eq_ignore_ascii_case(b"/script") && matches!( - chars[index + 8], + chars[index + pattern_len], b'>' | b' ' | b'\t' | b'\n' | b'\x0C' | b'\r' ) })