Skip to content

Commit

Permalink
perf(es/minifier): Add has_flag to Comments (#8182)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 committed Oct 25, 2023
1 parent 115f6fe commit 7530e90
Show file tree
Hide file tree
Showing 123 changed files with 1,239 additions and 22 deletions.
84 changes: 84 additions & 0 deletions crates/swc_common/src/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,47 @@ pub trait Comments {

ret
}

/// This method is used to check if a comment with the given flag exist.
///
/// If `flag` is `PURE`, this method will look for `@__PURE__` and
/// `#__PURE__`.
fn has_flag(&self, lo: BytePos, flag: &str) -> bool {
let cmts = self.take_leading(lo);

let ret = if let Some(comments) = &cmts {
(|| {
for c in comments {
if c.kind == CommentKind::Block {
for line in c.text.lines() {
// jsdoc
let line = line.trim_start_matches(['*', ' ']);
let line = line.trim();

//
if line.len() == (flag.len() + 5)
&& (line.starts_with("#__") || line.starts_with("@__"))
&& line.ends_with("__")
&& flag == &line[3..line.len() - 2]
{
return true;
}
}
}
}

false
})()
} else {
false
};

if let Some(cmts) = cmts {
self.add_trailing_comments(lo, cmts);
}

ret
}
}

macro_rules! delegate {
Expand Down Expand Up @@ -139,6 +180,10 @@ macro_rules! delegate {
fn add_pure_comment(&self, pos: BytePos) {
(**self).add_pure_comment(pos)
}

fn has_flag(&self, lo: BytePos, flag: &str) -> bool {
(**self).has_flag(lo, flag)
}
};
}

Expand Down Expand Up @@ -225,6 +270,11 @@ impl Comments for NoopComments {

#[cfg_attr(not(debug_assertions), inline(always))]
fn add_pure_comment(&self, _: BytePos) {}

#[inline]
fn has_flag(&self, _: BytePos, _: &str) -> bool {
false
}
}

/// This implementation behaves like [NoopComments] if it's [None].
Expand Down Expand Up @@ -347,6 +397,15 @@ where
f(&[])
}
}

#[inline]
fn has_flag(&self, lo: BytePos, flag: &str) -> bool {
if let Some(c) = self {
c.has_flag(lo, flag)
} else {
false
}
}
}

pub type SingleThreadedCommentsMapInner = FxHashMap<BytePos, Vec<Comment>>;
Expand Down Expand Up @@ -485,6 +544,31 @@ impl Comments for SingleThreadedComments {
f(&[])
}
}

fn has_flag(&self, lo: BytePos, flag: &str) -> bool {
self.with_leading(lo, |comments| {
for c in comments {
if c.kind == CommentKind::Block {
for line in c.text.lines() {
// jsdoc
let line = line.trim_start_matches(['*', ' ']);
let line = line.trim();

//
if line.len() == (flag.len() + 5)
&& (line.starts_with("#__") || line.starts_with("@__"))
&& line.ends_with("__")
&& flag == &line[3..line.len() - 2]
{
return true;
}
}
}
}

false
})
}
}

impl SingleThreadedComments {
Expand Down
8 changes: 7 additions & 1 deletion crates/swc_common/src/syntax_pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1139,11 +1139,17 @@ impl BytePos {
self.0 >= Self::MIN_RESERVED.0 && self.0 != u32::MAX
}

/// Returns true if this is synthesized and has no relevant input source
/// Returns `true`` if this is synthesized and has no relevant input source
/// code.
pub const fn is_dummy(self) -> bool {
self.0 == 0
}

/// Returns `true`` if this is explicitly synthesized or has relevant input
/// source so can have a comment.
pub const fn can_have_comment(self) -> bool {
self.0 != 0
}
}

/// A character offset. Because of multibyte utf8 characters, a byte offset
Expand Down
21 changes: 1 addition & 20 deletions crates/swc_ecma_minifier/src/metadata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,24 +311,5 @@ fn has_flag(comments: Option<&dyn Comments>, span: Span, text: &'static str) ->
return false;
}

find_comment(comments, span, |c| {
if c.kind == CommentKind::Block {
for line in c.text.lines() {
// jsdoc
let line = line.trim_start_matches(['*', ' ']);
let line = line.trim();

//
if line.len() == (text.len() + 5)
&& (line.starts_with("#__") || line.starts_with("@__"))
&& line.ends_with("__")
&& text == &line[3..line.len() - 2]
{
return true;
}
}
}

false
})
comments.has_flag(span.lo, text)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@

x Module
,-[$DIR/tests/comments/exprs/array/blockStmt/input.js:1:1]
1 | ,-> React.useEffect(() => {
2 | | // @refresh reset
3 | `-> });
`----

x ModuleItem
,-[$DIR/tests/comments/exprs/array/blockStmt/input.js:1:1]
1 | ,-> React.useEffect(() => {
2 | | // @refresh reset
3 | `-> });
`----

x Stmt
,-[$DIR/tests/comments/exprs/array/blockStmt/input.js:1:1]
1 | ,-> React.useEffect(() => {
2 | | // @refresh reset
3 | `-> });
`----

x ExprStmt
,-[$DIR/tests/comments/exprs/array/blockStmt/input.js:1:1]
1 | ,-> React.useEffect(() => {
2 | | // @refresh reset
3 | `-> });
`----

x Expr
,-[$DIR/tests/comments/exprs/array/blockStmt/input.js:1:1]
1 | ,-> React.useEffect(() => {
2 | | // @refresh reset
3 | `-> });
`----

x CallExpr
,-[$DIR/tests/comments/exprs/array/blockStmt/input.js:1:1]
1 | ,-> React.useEffect(() => {
2 | | // @refresh reset
3 | `-> });
`----

x Callee
,-[$DIR/tests/comments/exprs/array/blockStmt/input.js:1:1]
1 | React.useEffect(() => {
: ^^^^^^^^^^^^^^^
2 | // @refresh reset
`----

x Expr
,-[$DIR/tests/comments/exprs/array/blockStmt/input.js:1:1]
1 | React.useEffect(() => {
: ^^^^^^^^^^^^^^^
2 | // @refresh reset
`----

x MemberExpr
,-[$DIR/tests/comments/exprs/array/blockStmt/input.js:1:1]
1 | React.useEffect(() => {
: ^^^^^^^^^^^^^^^
2 | // @refresh reset
`----

x Expr
,-[$DIR/tests/comments/exprs/array/blockStmt/input.js:1:1]
1 | React.useEffect(() => {
: ^^^^^
2 | // @refresh reset
`----

x Ident
,-[$DIR/tests/comments/exprs/array/blockStmt/input.js:1:1]
1 | React.useEffect(() => {
: ^^^^^
2 | // @refresh reset
`----

x Ident
,-[$DIR/tests/comments/exprs/array/blockStmt/input.js:1:1]
1 | React.useEffect(() => {
: ^^^^^^^^^
2 | // @refresh reset
`----

x ExprOrSpread
,-[$DIR/tests/comments/exprs/array/blockStmt/input.js:1:1]
1 | ,-> React.useEffect(() => {
2 | | // @refresh reset
3 | `-> });
`----

x Expr
,-[$DIR/tests/comments/exprs/array/blockStmt/input.js:1:1]
1 | ,-> React.useEffect(() => {
2 | | // @refresh reset
3 | `-> });
`----

x ArrowExpr
,-[$DIR/tests/comments/exprs/array/blockStmt/input.js:1:1]
1 | ,-> React.useEffect(() => {
2 | | // @refresh reset
3 | `-> });
`----

x BlockStmtOrExpr
,-[$DIR/tests/comments/exprs/array/blockStmt/input.js:1:1]
1 | ,-> React.useEffect(() => {
2 | | // @refresh reset
3 | `-> });
`----

x BlockStmt
,-[$DIR/tests/comments/exprs/array/blockStmt/input.js:1:1]
1 | ,-> React.useEffect(() => {
2 | | // @refresh reset
3 | `-> });
`----
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@

x Module
,-[$DIR/tests/comments/exprs/call/simple/input.js:1:1]
1 | test(123/*post: 9*/, 456/*post: 10*/);
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
`----

x ModuleItem
,-[$DIR/tests/comments/exprs/call/simple/input.js:1:1]
1 | test(123/*post: 9*/, 456/*post: 10*/);
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
`----

x Stmt
,-[$DIR/tests/comments/exprs/call/simple/input.js:1:1]
1 | test(123/*post: 9*/, 456/*post: 10*/);
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
`----

x ExprStmt
,-[$DIR/tests/comments/exprs/call/simple/input.js:1:1]
1 | test(123/*post: 9*/, 456/*post: 10*/);
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
`----

x Expr
,-[$DIR/tests/comments/exprs/call/simple/input.js:1:1]
1 | test(123/*post: 9*/, 456/*post: 10*/);
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
`----

x CallExpr
,-[$DIR/tests/comments/exprs/call/simple/input.js:1:1]
1 | test(123/*post: 9*/, 456/*post: 10*/);
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
`----

x Callee
,-[$DIR/tests/comments/exprs/call/simple/input.js:1:1]
1 | test(123/*post: 9*/, 456/*post: 10*/);
: ^^^^
`----

x Expr
,-[$DIR/tests/comments/exprs/call/simple/input.js:1:1]
1 | test(123/*post: 9*/, 456/*post: 10*/);
: ^^^^
`----

x Ident
,-[$DIR/tests/comments/exprs/call/simple/input.js:1:1]
1 | test(123/*post: 9*/, 456/*post: 10*/);
: ^^^^
`----

x ExprOrSpread
,-[$DIR/tests/comments/exprs/call/simple/input.js:1:1]
1 | test(123/*post: 9*/, 456/*post: 10*/);
: ^^^
`----

x Expr
,-[$DIR/tests/comments/exprs/call/simple/input.js:1:1]
1 | test(123/*post: 9*/, 456/*post: 10*/);
: ^^^
`----

x Lit
,-[$DIR/tests/comments/exprs/call/simple/input.js:1:1]
1 | test(123/*post: 9*/, 456/*post: 10*/);
: ^^^
`----

x Number
,-[$DIR/tests/comments/exprs/call/simple/input.js:1:1]
1 | test(123/*post: 9*/, 456/*post: 10*/);
: ^^^
`----

x ExprOrSpread
,-[$DIR/tests/comments/exprs/call/simple/input.js:1:1]
1 | test(123/*post: 9*/, 456/*post: 10*/);
: ^^^
`----

x Expr
,-[$DIR/tests/comments/exprs/call/simple/input.js:1:1]
1 | test(123/*post: 9*/, 456/*post: 10*/);
: ^^^
`----

x Lit
,-[$DIR/tests/comments/exprs/call/simple/input.js:1:1]
1 | test(123/*post: 9*/, 456/*post: 10*/);
: ^^^
`----

x Number
,-[$DIR/tests/comments/exprs/call/simple/input.js:1:1]
1 | test(123/*post: 9*/, 456/*post: 10*/);
: ^^^
`----
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const fnB = /*#__NO_SIDE_EFFECTS__*/ (args) => {
}

1 comment on commit 7530e90

@github-actions
Copy link

Choose a reason for hiding this comment

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

Benchmark

Benchmark suite Current: 7530e90 Previous: 5ca3b63 Ratio
es/full/bugs-1 286968 ns/iter (± 13402) 297107 ns/iter (± 11940) 0.97
es/full/minify/libraries/antd 1341778784 ns/iter (± 34555597) 1431074246 ns/iter (± 13490364) 0.94
es/full/minify/libraries/d3 286306825 ns/iter (± 5170624) 297754693 ns/iter (± 2027376) 0.96
es/full/minify/libraries/echarts 1067359691 ns/iter (± 6750095) 1137309748 ns/iter (± 11617776) 0.94
es/full/minify/libraries/jquery 88372825 ns/iter (± 1481513) 89855860 ns/iter (± 304318) 0.98
es/full/minify/libraries/lodash 103991719 ns/iter (± 1009148) 105214814 ns/iter (± 304412) 0.99
es/full/minify/libraries/moment 52124565 ns/iter (± 525162) 53085664 ns/iter (± 173477) 0.98
es/full/minify/libraries/react 18704977 ns/iter (± 184640) 19208546 ns/iter (± 198529) 0.97
es/full/minify/libraries/terser 227235382 ns/iter (± 2340466) 243013842 ns/iter (± 2570998) 0.94
es/full/minify/libraries/three 399464002 ns/iter (± 6224476) 418576015 ns/iter (± 1855906) 0.95
es/full/minify/libraries/typescript 2680478939 ns/iter (± 12629551) 2845320183 ns/iter (± 17253380) 0.94
es/full/minify/libraries/victory 580038489 ns/iter (± 7410687) 607442306 ns/iter (± 4475575) 0.95
es/full/minify/libraries/vue 124713445 ns/iter (± 3522768) 127766413 ns/iter (± 912801) 0.98
es/full/codegen/es3 32617 ns/iter (± 364) 34635 ns/iter (± 63) 0.94
es/full/codegen/es5 33320 ns/iter (± 461) 34595 ns/iter (± 87) 0.96
es/full/codegen/es2015 32898 ns/iter (± 580) 34772 ns/iter (± 55) 0.95
es/full/codegen/es2016 32808 ns/iter (± 352) 34677 ns/iter (± 84) 0.95
es/full/codegen/es2017 32618 ns/iter (± 474) 34759 ns/iter (± 198) 0.94
es/full/codegen/es2018 32810 ns/iter (± 449) 34580 ns/iter (± 102) 0.95
es/full/codegen/es2019 32484 ns/iter (± 254) 34649 ns/iter (± 81) 0.94
es/full/codegen/es2020 32670 ns/iter (± 363) 34644 ns/iter (± 142) 0.94
es/full/all/es3 176786074 ns/iter (± 1902496) 180442764 ns/iter (± 1191743) 0.98
es/full/all/es5 169610556 ns/iter (± 2622552) 172418217 ns/iter (± 899735) 0.98
es/full/all/es2015 130014048 ns/iter (± 1460973) 129762794 ns/iter (± 1756143) 1.00
es/full/all/es2016 127266324 ns/iter (± 1828597) 129137328 ns/iter (± 1063927) 0.99
es/full/all/es2017 126088478 ns/iter (± 1377105) 127755795 ns/iter (± 711828) 0.99
es/full/all/es2018 124710900 ns/iter (± 1891410) 125788986 ns/iter (± 1166357) 0.99
es/full/all/es2019 123453731 ns/iter (± 1249807) 125484832 ns/iter (± 1105489) 0.98
es/full/all/es2020 118648506 ns/iter (± 2712879) 121840500 ns/iter (± 661323) 0.97
es/full/parser 539582 ns/iter (± 11633) 556255 ns/iter (± 3100) 0.97
es/full/base/fixer 17106 ns/iter (± 315) 18612 ns/iter (± 281) 0.92
es/full/base/resolver_and_hygiene 81135 ns/iter (± 1381) 84521 ns/iter (± 217) 0.96

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.