From 93085b60f1bd7b746e9a7f7dad03ed6732c1a524 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Tue, 6 Dec 2022 21:05:06 +0900 Subject: [PATCH 1/7] bench-parser.sh --- crates/swc_css_parser/scripts/instruemnt/bench-parser.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/swc_css_parser/scripts/instruemnt/bench-parser.sh b/crates/swc_css_parser/scripts/instruemnt/bench-parser.sh index 9ed0a3878bbc..083267c071b6 100755 --- a/crates/swc_css_parser/scripts/instruemnt/bench-parser.sh +++ b/crates/swc_css_parser/scripts/instruemnt/bench-parser.sh @@ -4,4 +4,4 @@ set -eu export RUST_LOG=off export MIMALLOC_SHOW_STATS=1 -cargo profile instruments --release -t time --features swc_common/concurrent --bench parser -- --bench --color $@ \ No newline at end of file +cargo profile instruments --release -t time --features swc_common/concurrent --bench parser -- $@ \ No newline at end of file From bd67299fbf842609f5cc38a96734a80a45a32aa3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Tue, 6 Dec 2022 21:09:42 +0900 Subject: [PATCH 2/7] Merge generics --- crates/swc_css_parser/benches/parser.rs | 32 +++++++++++++++++-------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/crates/swc_css_parser/benches/parser.rs b/crates/swc_css_parser/benches/parser.rs index 739d87dd222b..bb3e05570779 100644 --- a/crates/swc_css_parser/benches/parser.rs +++ b/crates/swc_css_parser/benches/parser.rs @@ -21,18 +21,30 @@ fn bench_stylesheet(b: &mut Bencher, src: &'static str) { }); } -fn bench_files(c: &mut Criterion) { - c.bench_function("css/parser/bootstrap_5_1_3", |b| { - bench_stylesheet(b, include_str!("./files/bootstrap_5_1_3.css")) - }); - - c.bench_function("css/parser/foundation_6_7_4", |b| { - bench_stylesheet(b, include_str!("./files/foundation_6_7_4.css")) +fn run(c: &mut Criterion, id: &str, src: &'static str) { + c.bench_function(&format!("css/parser/{}", id), |b| { + bench_stylesheet(b, src); }); +} - c.bench_function("css/parser/tailwind_3_1_1", |b| { - bench_stylesheet(b, include_str!("./files/tailwind_3_1_1.css")) - }); +fn bench_files(c: &mut Criterion) { + run( + c, + "bootstrap_5_1_3", + include_str!("./files/bootstrap_5_1_3.css"), + ); + + run( + c, + "foundation_6_7_4", + include_str!("./files/foundation_6_7_4.css"), + ); + + run( + c, + "tailwind_3_1_1", + include_str!("./files/tailwind_3_1_1.css"), + ); } criterion_group!(benches, bench_files); From e05db985c78b29e4b8919a837bc6287fe62ecdbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Tue, 6 Dec 2022 21:10:08 +0900 Subject: [PATCH 3/7] Merge genrics --- crates/swc_css_parser/benches/lexer.rs | 32 ++++++++++++++++++-------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/crates/swc_css_parser/benches/lexer.rs b/crates/swc_css_parser/benches/lexer.rs index 33749091ea97..ba1c696deb93 100644 --- a/crates/swc_css_parser/benches/lexer.rs +++ b/crates/swc_css_parser/benches/lexer.rs @@ -20,18 +20,30 @@ fn bench_stylesheet(b: &mut Bencher, src: &'static str) { }); } -fn bench_files(c: &mut Criterion) { - c.bench_function("css/lexer/bootstrap_5_1_3", |b| { - bench_stylesheet(b, include_str!("./files/bootstrap_5_1_3.css")) - }); - - c.bench_function("css/lexer/foundation_6_7_4", |b| { - bench_stylesheet(b, include_str!("./files/foundation_6_7_4.css")) +fn run(c: &mut Criterion, id: &str, src: &'static str) { + c.bench_function(&format!("css/parser/{}", id), |b| { + bench_stylesheet(b, src); }); +} - c.bench_function("css/lexer/tailwind_3_1_1", |b| { - bench_stylesheet(b, include_str!("./files/tailwind_3_1_1.css")) - }); +fn bench_files(c: &mut Criterion) { + run( + c, + "bootstrap_5_1_3", + include_str!("./files/bootstrap_5_1_3.css"), + ); + + run( + c, + "foundation_6_7_4", + include_str!("./files/foundation_6_7_4.css"), + ); + + run( + c, + "tailwind_3_1_1", + include_str!("./files/tailwind_3_1_1.css"), + ); } criterion_group!(benches, bench_files); From 80a387945d81ceafd373fc8c2f5d9cdccf6a262b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Tue, 6 Dec 2022 22:11:31 +0900 Subject: [PATCH 4/7] Fix bench-parser.sh --- crates/swc_css_parser/scripts/instruemnt/bench-parser.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/swc_css_parser/scripts/instruemnt/bench-parser.sh b/crates/swc_css_parser/scripts/instruemnt/bench-parser.sh index 083267c071b6..abb6ffb448ad 100755 --- a/crates/swc_css_parser/scripts/instruemnt/bench-parser.sh +++ b/crates/swc_css_parser/scripts/instruemnt/bench-parser.sh @@ -2,6 +2,6 @@ set -eu export RUST_LOG=off -export MIMALLOC_SHOW_STATS=1 +# export MIMALLOC_SHOW_STATS=1 cargo profile instruments --release -t time --features swc_common/concurrent --bench parser -- $@ \ No newline at end of file From a5de99b3bcc1c04870ae8633f80b5bf32a48ae78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Tue, 6 Dec 2022 22:14:34 +0900 Subject: [PATCH 5/7] Cow --- crates/swc_css_parser/src/parser/input.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/crates/swc_css_parser/src/parser/input.rs b/crates/swc_css_parser/src/parser/input.rs index 3ef45f9a4f0c..a8916afd135c 100644 --- a/crates/swc_css_parser/src/parser/input.rs +++ b/crates/swc_css_parser/src/parser/input.rs @@ -1,4 +1,4 @@ -use std::{fmt::Debug, mem::take}; +use std::{borrow::Cow, fmt::Debug, mem::take}; use swc_atoms::{Atom, JsWord}; use swc_common::{BytePos, Span, Spanned, SyntaxContext}; @@ -340,7 +340,7 @@ impl<'a> Input<'a> { } } - fn cur(&mut self) -> PResult { + fn cur(&mut self) -> PResult> { match self.input { InputType::Tokens(input) => { let idx = match self.idx.last() { @@ -354,7 +354,7 @@ impl<'a> Input<'a> { }; let token_and_span = match input.tokens.get(*idx) { - Some(token_and_span) => token_and_span.clone(), + Some(token_and_span) => token_and_span, None => { let bp = input.span.hi; let span = Span::new(bp, bp, SyntaxContext::empty()); @@ -363,7 +363,7 @@ impl<'a> Input<'a> { } }; - Ok(token_and_span) + Ok(Cow::Borrowed(token_and_span)) } InputType::ListOfComponentValues(input) => { let token_and_span = match self.get_component_value(&input.children, 0) { @@ -409,7 +409,7 @@ impl<'a> Input<'a> { } }; - Ok(token_and_span) + Ok(Cow::Owned(token_and_span)) } } } @@ -447,7 +447,7 @@ impl<'a> ParserInput for Input<'a> { while let Ok(TokenAndSpan { token: tok!(" "), span, - }) = self.cur() + }) = self.cur().as_deref() { last_pos = Some(span.hi); @@ -465,7 +465,7 @@ impl<'a> Iterator for Input<'a> { fn next(&mut self) -> Option { let token_and_span = match self.cur() { - Ok(token_and_span) => token_and_span, + Ok(token_and_span) => token_and_span.into_owned(), _ => return None, }; From 09cbc07ecef8ae943a4a2c3392b9b8dd37be3c94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Tue, 6 Dec 2022 22:26:35 +0900 Subject: [PATCH 6/7] BytePos --- crates/swc_css_parser/src/parser/input.rs | 52 ++++++++++++++--------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/crates/swc_css_parser/src/parser/input.rs b/crates/swc_css_parser/src/parser/input.rs index a8916afd135c..7914566bf2e6 100644 --- a/crates/swc_css_parser/src/parser/input.rs +++ b/crates/swc_css_parser/src/parser/input.rs @@ -219,16 +219,18 @@ pub enum InputType<'a> { ListOfComponentValues(&'a ListOfComponentValues), } +type SpanLike = (BytePos, BytePos); + #[derive(Debug)] enum TokenOrBlock { Token(Box), Function(Box<(Span, JsWord, Atom)>), - LBracket(Box), - LParen(Box), - LBrace(Box), - RParen(Box), - RBracket(Box), - RBrace(Box), + LBracket(SpanLike), + LParen(SpanLike), + LBrace(SpanLike), + RParen(SpanLike), + RBracket(SpanLike), + RBrace(SpanLike), } impl<'a> Input<'a> { @@ -288,11 +290,10 @@ impl<'a> Input<'a> { let res = self.get_component_value(&function.value, deep + 1); if res.is_none() { - return Some(TokenOrBlock::RParen(Box::new(Span::new( + return Some(TokenOrBlock::RParen(( function.span_hi() - BytePos(1), function.span_hi(), - Default::default(), - )))); + ))); } res @@ -300,9 +301,18 @@ impl<'a> Input<'a> { Some(ComponentValue::SimpleBlock(simple_block)) => { if self.idx.len() - 1 == deep { let close = match simple_block.name.token { - Token::LBracket => TokenOrBlock::LBracket(Box::new(simple_block.name.span)), - Token::LParen => TokenOrBlock::LParen(Box::new(simple_block.name.span)), - Token::LBrace => TokenOrBlock::LBrace(Box::new(simple_block.name.span)), + Token::LBracket => TokenOrBlock::LBracket(( + simple_block.name.span.lo, + simple_block.name.span.hi, + )), + Token::LParen => TokenOrBlock::LParen(( + simple_block.name.span.lo, + simple_block.name.span.hi, + )), + Token::LBrace => TokenOrBlock::LBrace(( + simple_block.name.span.lo, + simple_block.name.span.hi, + )), _ => { unreachable!(); } @@ -320,9 +330,9 @@ impl<'a> Input<'a> { Default::default(), ); let close = match simple_block.name.token { - Token::LBracket => TokenOrBlock::RBracket(Box::new(span)), - Token::LParen => TokenOrBlock::RParen(Box::new(span)), - Token::LBrace => TokenOrBlock::RBrace(Box::new(span)), + Token::LBracket => TokenOrBlock::RBracket((span.lo, span.hi)), + Token::LParen => TokenOrBlock::RParen((span.lo, span.hi)), + Token::LBrace => TokenOrBlock::RBrace((span.lo, span.hi)), _ => { unreachable!(); } @@ -377,27 +387,27 @@ impl<'a> Input<'a> { }, }, TokenOrBlock::LBracket(span) => TokenAndSpan { - span: *span, + span: Span::new(span.0, span.1, Default::default()), token: Token::LBracket, }, TokenOrBlock::LBrace(span) => TokenAndSpan { - span: *span, + span: Span::new(span.0, span.1, Default::default()), token: Token::LBrace, }, TokenOrBlock::LParen(span) => TokenAndSpan { - span: *span, + span: Span::new(span.0, span.1, Default::default()), token: Token::LParen, }, TokenOrBlock::RBracket(span) => TokenAndSpan { - span: *span, + span: Span::new(span.0, span.1, Default::default()), token: Token::RBracket, }, TokenOrBlock::RBrace(span) => TokenAndSpan { - span: *span, + span: Span::new(span.0, span.1, Default::default()), token: Token::RBrace, }, TokenOrBlock::RParen(span) => TokenAndSpan { - span: *span, + span: Span::new(span.0, span.1, Default::default()), token: Token::RParen, }, }, From 8703730b3247816c54637bebc807da06a0b65b09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Tue, 6 Dec 2022 22:28:51 +0900 Subject: [PATCH 7/7] More Cow --- crates/swc_css_parser/src/parser/input.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/crates/swc_css_parser/src/parser/input.rs b/crates/swc_css_parser/src/parser/input.rs index 7914566bf2e6..adced3631088 100644 --- a/crates/swc_css_parser/src/parser/input.rs +++ b/crates/swc_css_parser/src/parser/input.rs @@ -222,8 +222,8 @@ pub enum InputType<'a> { type SpanLike = (BytePos, BytePos); #[derive(Debug)] -enum TokenOrBlock { - Token(Box), +enum TokenOrBlock<'a> { + Token(&'a TokenAndSpan), Function(Box<(Span, JsWord, Atom)>), LBracket(SpanLike), LParen(SpanLike), @@ -261,7 +261,7 @@ impl<'a> Input<'a> { &mut self, list: &'a [ComponentValue], deep: usize, - ) -> Option { + ) -> Option> { let index = match self.idx.get(deep) { Some(index) => index, _ => return None, @@ -269,7 +269,7 @@ impl<'a> Input<'a> { match list.get(*index) { Some(ComponentValue::PreservedToken(token_and_span)) => { - Some(TokenOrBlock::Token(token_and_span.clone())) + Some(TokenOrBlock::Token(token_and_span)) } Some(ComponentValue::Function(function)) => { if self.idx.len() - 1 == deep { @@ -378,7 +378,9 @@ impl<'a> Input<'a> { InputType::ListOfComponentValues(input) => { let token_and_span = match self.get_component_value(&input.children, 0) { Some(token_or_block) => match token_or_block { - TokenOrBlock::Token(token_and_span) => *token_and_span, + TokenOrBlock::Token(token_and_span) => { + return Ok(Cow::Borrowed(token_and_span)) + } TokenOrBlock::Function(function) => TokenAndSpan { span: function.0, token: Token::Function {