Skip to content

Commit

Permalink
feat(es/typescript): Improve fast TS strip (#9167)
Browse files Browse the repository at this point in the history
  • Loading branch information
magic-akari authored Jul 6, 2024
1 parent 18080f7 commit 98af589
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@

exports[`transform in strip-only mode should remove declare enum 1`] = `" "`;

exports[`transform in strip-only mode should remove declare enum 2`] = `" "`;
exports[`transform in strip-only mode should remove declare enum 2`] = `
"
"
`;

exports[`transform in strip-only mode should remove declare enum 3`] = `" "`;
exports[`transform in strip-only mode should remove declare enum 3`] = `
"
"
`;

exports[`transform in strip-only mode should strip complex expressions 1`] = `
"const foo = {
Expand Down
50 changes: 31 additions & 19 deletions crates/swc_fast_ts_strip/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,22 +168,28 @@ impl TsStrip {
&self.src[(span.lo.0 - 1) as usize..(span.hi.0 - 1) as usize]
}

fn get_next_token(&self, pos: BytePos) -> &TokenAndSpan {
fn get_next_token_index(&self, pos: BytePos) -> usize {
let index = self.tokens.binary_search_by_key(&pos, |t| t.span.lo);
let index = match index {
match index {
Ok(index) => index,
Err(index) => index,
};
&self.tokens[index]
}
}

fn get_prev_token(&self, pos: BytePos) -> &TokenAndSpan {
fn get_next_token(&self, pos: BytePos) -> &TokenAndSpan {
&self.tokens[self.get_next_token_index(pos)]
}

fn get_prev_token_index(&self, pos: BytePos) -> usize {
let index = self.tokens.binary_search_by_key(&pos, |t| t.span.lo);
let index = match index {
match index {
Ok(index) => index,
Err(index) => index - 1,
};
&self.tokens[index]
}
}

fn get_prev_token(&self, pos: BytePos) -> &TokenAndSpan {
&self.tokens[self.get_prev_token_index(pos)]
}
}

Expand Down Expand Up @@ -218,7 +224,7 @@ impl Visit for TsStrip {
// 1;
// ```

self.add_overwrite(ret.span_hi(), b')');
self.add_overwrite(ret.span_hi() - BytePos(1), b')');
}
}

Expand Down Expand Up @@ -264,6 +270,10 @@ impl Visit for TsStrip {
if !n.implements.is_empty() {
let implements =
self.get_prev_token(n.implements.first().unwrap().span_lo() - BytePos(1));
debug_assert_eq!(
implements.token,
Token::Word(Word::Ident(IdentLike::Known(KnownIdent::Implements)))
);

let last = n.implements.last().unwrap();
let span = span(implements.span.lo, last.span.hi);
Expand All @@ -279,13 +289,14 @@ impl Visit for TsStrip {
return;
}

let key_span = n.key.span_hi();
let method_pos = n.function.span_lo();
let key_pos = n.key.span_lo();
let mut pos = n.span_lo();
let mut index = self.get_next_token_index(pos);

let mut pos = method_pos;
while pos < key_span {
let TokenAndSpan { token, span, .. } = self.get_next_token(pos);
while pos < key_pos {
let TokenAndSpan { token, span, .. } = &self.tokens[index];
pos = span.hi;
index += 1;
match token {
Token::Word(Word::Ident(IdentLike::Known(
KnownIdent::Public | KnownIdent::Protected | KnownIdent::Private,
Expand All @@ -308,13 +319,14 @@ impl Visit for TsStrip {
return;
}

let key_span = n.key.span_hi();
let method_pos = n.span_lo();
let key_pos = n.key.span_lo();
let mut pos = n.span_lo();
let mut index = self.get_next_token_index(pos);

let mut pos = method_pos;
while pos < key_span {
let TokenAndSpan { token, span, .. } = self.get_next_token(pos);
while pos < key_pos {
let TokenAndSpan { token, span, .. } = &self.tokens[index];
pos = span.hi;
index += 1;
match token {
Token::Word(Word::Ident(IdentLike::Known(
KnownIdent::Readonly
Expand Down
19 changes: 12 additions & 7 deletions crates/swc_fast_ts_strip/tests/fixture/test-case-1.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,36 +142,41 @@ void 0;
// `=>` spanning line cases:
{
(
)=>
) =>
1
};
{
(
)=>
) =>
1
};
{
(

)=>
) =>
1
};
{
(


)=>
) =>
1
};
{
(


)=>
) =>
1
};
{
(a, b, c = [] /*comment-1*/ /*comment-2*/
)=>
) =>
1
};
};


(
)=>
1;
7 changes: 6 additions & 1 deletion crates/swc_fast_ts_strip/tests/fixture/test-case-1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,9 @@ void 0;
(a, b, c: D = [] as any/*comment-1*/)/*comment-2*/:
any =>
1
};
};


():
any=>
1;

1 comment on commit 98af589

@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: 98af589 Previous: d86ca2d Ratio
es/full/bugs-1 279747 ns/iter (± 13428) 295119 ns/iter (± 11477) 0.95
es/full/minify/libraries/antd 1646196145 ns/iter (± 34677965) 1775644746 ns/iter (± 56643561) 0.93
es/full/minify/libraries/d3 331937774 ns/iter (± 765466) 340892999 ns/iter (± 3358812) 0.97
es/full/minify/libraries/echarts 1353598287 ns/iter (± 3788796) 1497860758 ns/iter (± 16099921) 0.90
es/full/minify/libraries/jquery 100801543 ns/iter (± 159069) 101297817 ns/iter (± 292059) 1.00
es/full/minify/libraries/lodash 111892975 ns/iter (± 536352) 112665344 ns/iter (± 310835) 0.99
es/full/minify/libraries/moment 58678430 ns/iter (± 80728) 58604422 ns/iter (± 293827) 1.00
es/full/minify/libraries/react 18221564 ns/iter (± 17982) 18069175 ns/iter (± 28090) 1.01
es/full/minify/libraries/terser 266471908 ns/iter (± 1287438) 270704853 ns/iter (± 2517045) 0.98
es/full/minify/libraries/three 449447944 ns/iter (± 1452920) 479368458 ns/iter (± 10925179) 0.94
es/full/minify/libraries/typescript 3070510379 ns/iter (± 8002573) 3193928946 ns/iter (± 18782531) 0.96
es/full/minify/libraries/victory 675211414 ns/iter (± 3250835) 721997223 ns/iter (± 25152034) 0.94
es/full/minify/libraries/vue 142059341 ns/iter (± 211164) 143713069 ns/iter (± 335718) 0.99
es/full/codegen/es3 59250 ns/iter (± 320) 58274 ns/iter (± 213) 1.02
es/full/codegen/es5 59576 ns/iter (± 424) 58488 ns/iter (± 335) 1.02
es/full/codegen/es2015 58526 ns/iter (± 253) 58508 ns/iter (± 167) 1.00
es/full/codegen/es2016 58552 ns/iter (± 257) 58514 ns/iter (± 137) 1.00
es/full/codegen/es2017 58747 ns/iter (± 171) 58455 ns/iter (± 223) 1.00
es/full/codegen/es2018 58660 ns/iter (± 244) 58524 ns/iter (± 170) 1.00
es/full/codegen/es2019 58793 ns/iter (± 1400) 58467 ns/iter (± 125) 1.01
es/full/codegen/es2020 58869 ns/iter (± 155) 58460 ns/iter (± 164) 1.01
es/full/all/es3 162121076 ns/iter (± 592581) 159672332 ns/iter (± 1206038) 1.02
es/full/all/es5 156230364 ns/iter (± 737036) 153597885 ns/iter (± 671571) 1.02
es/full/all/es2015 115561632 ns/iter (± 1516425) 113840227 ns/iter (± 412558) 1.02
es/full/all/es2016 113773629 ns/iter (± 1141325) 113168126 ns/iter (± 562689) 1.01
es/full/all/es2017 113403233 ns/iter (± 564880) 111909656 ns/iter (± 1027459) 1.01
es/full/all/es2018 112244725 ns/iter (± 657689) 110184800 ns/iter (± 459501) 1.02
es/full/all/es2019 111456101 ns/iter (± 720982) 110179366 ns/iter (± 362566) 1.01
es/full/all/es2020 107170410 ns/iter (± 543371) 106006586 ns/iter (± 782515) 1.01
es/full/parser 450106 ns/iter (± 3481) 452424 ns/iter (± 3363) 0.99
es/full/base/fixer 15357 ns/iter (± 132) 15232 ns/iter (± 122) 1.01
es/full/base/resolver_and_hygiene 79202 ns/iter (± 363) 79082 ns/iter (± 241) 1.00
serialization of serde 276 ns/iter (± 2) 270 ns/iter (± 1) 1.02
css/minify/libraries/bootstrap 21896983 ns/iter (± 163526) 21815533 ns/iter (± 147316) 1.00
css/visitor/compare/clone 1403270 ns/iter (± 9095) 1397067 ns/iter (± 12755) 1.00
css/visitor/compare/visit_mut_span 1518491 ns/iter (± 4063) 1539515 ns/iter (± 7165) 0.99
css/visitor/compare/visit_mut_span_panic 1530607 ns/iter (± 34976) 1542651 ns/iter (± 33158) 0.99
css/visitor/compare/fold_span 2039972 ns/iter (± 103352) 2030906 ns/iter (± 5087) 1.00
css/visitor/compare/fold_span_panic 2171195 ns/iter (± 6660) 2179477 ns/iter (± 6682) 1.00
css/lexer/bootstrap_5_1_3 3456285 ns/iter (± 1496) 3501394 ns/iter (± 2457) 0.99
css/lexer/foundation_6_7_4 2806011 ns/iter (± 3934) 2825671 ns/iter (± 3411) 0.99
css/lexer/tailwind_3_1_1 554441 ns/iter (± 16993) 539464 ns/iter (± 954) 1.03
css/parser/bootstrap_5_1_3 15832861 ns/iter (± 195463) 15895196 ns/iter (± 44917) 1.00
css/parser/foundation_6_7_4 12698209 ns/iter (± 10264) 12738760 ns/iter (± 9899) 1.00
css/parser/tailwind_3_1_1 2514162 ns/iter (± 3242) 2520779 ns/iter (± 1875) 1.00
es/codegen/colors 724919 ns/iter (± 397881) 722232 ns/iter (± 395386) 1.00
es/codegen/large 2985794 ns/iter (± 1587263) 3019635 ns/iter (± 1603167) 0.99
es/codegen/with-parser/colors 40196 ns/iter (± 242) 40268 ns/iter (± 265) 1.00
es/codegen/with-parser/large 450938 ns/iter (± 14482) 451222 ns/iter (± 1227) 1.00
es/minify/libraries/antd 1245189322 ns/iter (± 15196193) 1322749062 ns/iter (± 31800470) 0.94
es/minify/libraries/d3 256842359 ns/iter (± 828296) 279523364 ns/iter (± 3453744) 0.92
es/minify/libraries/echarts 1016181399 ns/iter (± 22644870) 1115954809 ns/iter (± 9940987) 0.91
es/minify/libraries/jquery 76252012 ns/iter (± 159665) 82439143 ns/iter (± 694118) 0.92
es/minify/libraries/lodash 96738323 ns/iter (± 574460) 104084283 ns/iter (± 521134) 0.93
es/minify/libraries/moment 44527754 ns/iter (± 134227) 48131870 ns/iter (± 53996) 0.93
es/minify/libraries/react 15033363 ns/iter (± 93169) 16300315 ns/iter (± 20925) 0.92
es/minify/libraries/terser 198993595 ns/iter (± 544918) 217632281 ns/iter (± 1732247) 0.91
es/minify/libraries/three 328873866 ns/iter (± 2186209) 364442731 ns/iter (± 8934658) 0.90
es/minify/libraries/typescript 2472230218 ns/iter (± 7662752) 2709439852 ns/iter (± 38323716) 0.91
es/minify/libraries/victory 493954709 ns/iter (± 1851372) 583294375 ns/iter (± 6496535) 0.85
es/minify/libraries/vue 111225108 ns/iter (± 437516) 121403417 ns/iter (± 258866) 0.92
es/visitor/compare/clone 1730322 ns/iter (± 7307) 1715677 ns/iter (± 16566) 1.01
es/visitor/compare/visit_mut_span 2072527 ns/iter (± 4838) 2059705 ns/iter (± 7783) 1.01
es/visitor/compare/visit_mut_span_panic 2070966 ns/iter (± 36253) 2064244 ns/iter (± 44802) 1.00
es/visitor/compare/fold_span 2781277 ns/iter (± 12302) 2785959 ns/iter (± 5344) 1.00
es/visitor/compare/fold_span_panic 2809787 ns/iter (± 6446) 2820661 ns/iter (± 27612) 1.00
es/lexer/colors 9492 ns/iter (± 64) 9523 ns/iter (± 48) 1.00
es/lexer/angular 5019568 ns/iter (± 52807) 4888647 ns/iter (± 20136) 1.03
es/lexer/backbone 654395 ns/iter (± 4937) 630329 ns/iter (± 3952) 1.04
es/lexer/jquery 3736101 ns/iter (± 25025) 3623818 ns/iter (± 24861) 1.03
es/lexer/jquery mobile 5399054 ns/iter (± 13351) 5411885 ns/iter (± 97626) 1.00
es/lexer/mootools 2858554 ns/iter (± 6508) 2926726 ns/iter (± 10424) 0.98
es/lexer/underscore 526924 ns/iter (± 5820) 544893 ns/iter (± 2179) 0.97
es/lexer/three 16339279 ns/iter (± 443188) 16090295 ns/iter (± 55692) 1.02
es/lexer/yui 2917421 ns/iter (± 104669) 2910548 ns/iter (± 7917) 1.00
es/lexer/cal-com 13490645 ns/iter (± 31834) 13091606 ns/iter (± 42868) 1.03
es/lexer/typescript 91326151 ns/iter (± 185461) 92046289 ns/iter (± 199711) 0.99
es/parser/colors 24152 ns/iter (± 74) 24946 ns/iter (± 287) 0.97
es/parser/angular 12840290 ns/iter (± 123396) 13026302 ns/iter (± 87007) 0.99
es/parser/backbone 1854525 ns/iter (± 9649) 1875331 ns/iter (± 5826) 0.99
es/parser/jquery 10225457 ns/iter (± 37976) 10313793 ns/iter (± 67065) 0.99
es/parser/jquery mobile 15307388 ns/iter (± 145953) 15576035 ns/iter (± 119235) 0.98
es/parser/mootools 7937966 ns/iter (± 35479) 8072900 ns/iter (± 20738) 0.98
es/parser/underscore 1617242 ns/iter (± 8199) 1637369 ns/iter (± 7805) 0.99
es/parser/three 43924573 ns/iter (± 148144) 45315272 ns/iter (± 1337242) 0.97
es/parser/yui 7536198 ns/iter (± 80375) 7667967 ns/iter (± 51838) 0.98
es/parser/cal-com 39446194 ns/iter (± 529619) 39961073 ns/iter (± 173886) 0.99
es/parser/typescript 271009544 ns/iter (± 954782) 278733123 ns/iter (± 1843730) 0.97
es/preset-env/usage/builtin_type 136382 ns/iter (± 33605) 136426 ns/iter (± 34340) 1.00
es/preset-env/usage/property 16546 ns/iter (± 138) 16880 ns/iter (± 127) 0.98
es/resolver/typescript 100507206 ns/iter (± 1790862) 96506420 ns/iter (± 2366147) 1.04
es/fixer/typescript 81166695 ns/iter (± 2185767) 80986274 ns/iter (± 2830278) 1.00
es/hygiene/typescript 185681833 ns/iter (± 3225470) 183918627 ns/iter (± 4873219) 1.01
es/resolver_with_hygiene/typescript 308478157 ns/iter (± 2218875) 309651541 ns/iter (± 7286160) 1.00
es/visitor/base-perf/module_clone 56135 ns/iter (± 710) 55972 ns/iter (± 2971) 1.00
es/visitor/base-perf/fold_empty 59408 ns/iter (± 411) 59628 ns/iter (± 844) 1.00
es/visitor/base-perf/fold_noop_impl_all 59538 ns/iter (± 1699) 59258 ns/iter (± 1115) 1.00
es/visitor/base-perf/fold_noop_impl_vec 59534 ns/iter (± 1138) 60276 ns/iter (± 1379) 0.99
es/visitor/base-perf/boxing_boxed_clone 61 ns/iter (± 0) 59 ns/iter (± 0) 1.03
es/visitor/base-perf/boxing_unboxed_clone 51 ns/iter (± 0) 47 ns/iter (± 0) 1.09
es/visitor/base-perf/boxing_boxed 109 ns/iter (± 1) 106 ns/iter (± 0) 1.03
es/visitor/base-perf/boxing_unboxed 83 ns/iter (± 0) 80 ns/iter (± 0) 1.04
es/visitor/base-perf/visit_contains_this 2486 ns/iter (± 18) 2474 ns/iter (± 15) 1.00
es/base/parallel/resolver/typescript 2471445510 ns/iter (± 210741766) 2390466154 ns/iter (± 95425775) 1.03
es/base/parallel/hygiene/typescript 3238427314 ns/iter (± 41892381) 3241152681 ns/iter (± 55892693) 1.00
babelify-only 495308 ns/iter (± 3980) 493711 ns/iter (± 4680) 1.00
parse_and_babelify_angular 41578688 ns/iter (± 2845464) 42519576 ns/iter (± 2347778) 0.98
parse_and_babelify_backbone 4546138 ns/iter (± 172656) 4388380 ns/iter (± 150629) 1.04
parse_and_babelify_jquery 29165408 ns/iter (± 481907) 28218797 ns/iter (± 164229) 1.03
parse_and_babelify_jquery_mobile 50009754 ns/iter (± 1205735) 50014115 ns/iter (± 826839) 1.00
parse_and_babelify_mootools 26156472 ns/iter (± 553798) 25647318 ns/iter (± 255211) 1.02
parse_and_babelify_underscore 3661376 ns/iter (± 15031) 3663490 ns/iter (± 14018) 1.00
parse_and_babelify_yui 24597710 ns/iter (± 551744) 27292044 ns/iter (± 1687458) 0.90
html/minify/document/css_spec 46525325 ns/iter (± 83221) 45973075 ns/iter (± 88168) 1.01
html/minify/document/github 18574072 ns/iter (± 145746) 18623930 ns/iter (± 33565) 1.00
html/minify/document/stackoverflow 16761976 ns/iter (± 531554) 16822472 ns/iter (± 659411) 1.00
html/minify/document_fragment/css_spec 44818555 ns/iter (± 226562) 44609656 ns/iter (± 51728) 1.00
html/minify/document_fragment/github 18172340 ns/iter (± 36805) 18174324 ns/iter (± 38679) 1.00
html/minify/document_fragment/stackoverflow 16420716 ns/iter (± 13268) 16466897 ns/iter (± 31356) 1.00
html/document/visitor/compare/clone 166082 ns/iter (± 991) 171375 ns/iter (± 1919) 0.97
html/document/visitor/compare/visit_mut_span 183867 ns/iter (± 9871) 188653 ns/iter (± 2400) 0.97
html/document/visitor/compare/visit_mut_span_panic 190000 ns/iter (± 3442) 194130 ns/iter (± 7874) 0.98
html/document/visitor/compare/fold_span 228709 ns/iter (± 936) 233871 ns/iter (± 1338) 0.98
html/document/visitor/compare/fold_span_panic 281544 ns/iter (± 1981) 287066 ns/iter (± 872) 0.98
html/document_fragment/visitor/compare/clone 167789 ns/iter (± 811) 169980 ns/iter (± 2145) 0.99
html/document_fragment/visitor/compare/visit_mut_span 183713 ns/iter (± 1786) 187422 ns/iter (± 1380) 0.98
html/document_fragment/visitor/compare/visit_mut_span_panic 186021 ns/iter (± 1306) 191641 ns/iter (± 765) 0.97
html/document_fragment/visitor/compare/fold_span 230472 ns/iter (± 1265) 235679 ns/iter (± 903) 0.98
html/document_fragment/visitor/compare/fold_span_panic 282184 ns/iter (± 2505) 281909 ns/iter (± 1155) 1.00
html/lexer/css_2021_spec 13753693 ns/iter (± 361178) 12929826 ns/iter (± 74872) 1.06
html/lexer/github_com_17_05_2022 5127661 ns/iter (± 8845) 4934252 ns/iter (± 4773) 1.04
html/lexer/stackoverflow_com_17_05_2022 4967075 ns/iter (± 9183) 4697624 ns/iter (± 3966) 1.06
html/parser/parser_document/css_2021_spec 25780142 ns/iter (± 281821) 26120252 ns/iter (± 422636) 0.99
html/parser/parser_document/github_com_17_05_2022 8549376 ns/iter (± 37257) 8716306 ns/iter (± 27645) 0.98
html/parser/parser_document/stackoverflow_com_17_05_2022 7351408 ns/iter (± 16474) 7610070 ns/iter (± 6666) 0.97
html/parser/parser_document_fragment/css_2021_spec 25379557 ns/iter (± 47474) 26036969 ns/iter (± 44590) 0.97
html/parser/parser_document_fragment/github_com_17_05_2022 8515917 ns/iter (± 15691) 8711680 ns/iter (± 12482) 0.98
html/parser/parser_document_fragment/stackoverflow_com_17_05_2022 7310810 ns/iter (± 7684) 7650312 ns/iter (± 8346) 0.96

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

Please sign in to comment.