Skip to content

Commit

Permalink
feat(es/typescript): Improve fast TS stripper (#9153)
Browse files Browse the repository at this point in the history
  • Loading branch information
magic-akari authored Jul 5, 2024
1 parent 1a71601 commit 732d748
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 15 deletions.
74 changes: 61 additions & 13 deletions crates/swc_fast_ts_strip/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use swc_common::{
BytePos, FileName, SourceMap, Span, Spanned,
};
use swc_ecma_ast::{
BindingIdent, Decorator, EsVersion, Ident, Param, Pat, Program, TsAsExpr, TsConstAssertion,
TsEnumDecl, TsInstantiation, TsModuleDecl, TsModuleName, TsNamespaceDecl, TsNonNullExpr,
TsParamPropParam, TsSatisfiesExpr, TsTypeAliasDecl, TsTypeAnn,
BindingIdent, Decorator, EsVersion, Ident, ImportDecl, ImportSpecifier, Param, Pat, Program,
TsAsExpr, TsConstAssertion, TsEnumDecl, TsInstantiation, TsModuleDecl, TsModuleName,
TsNamespaceDecl, TsNonNullExpr, TsParamPropParam, TsSatisfiesExpr, TsTypeAliasDecl, TsTypeAnn,
};
use swc_ecma_parser::{
parse_file_as_module, parse_file_as_program, parse_file_as_script, Syntax, TsSyntax,
Expand Down Expand Up @@ -83,7 +83,7 @@ pub fn operate(
}

// Strip typescript types
let mut ts_strip = TsStrip::default();
let mut ts_strip = TsStrip::new(fm.src.clone());
program.visit_with(&mut ts_strip);

let replacements = ts_strip.replacements;
Expand All @@ -92,22 +92,29 @@ pub fn operate(
return Ok(fm.src.to_string());
}

let mut code = <std::string::String as Clone>::clone(&fm.src).into_bytes();
let mut code = fm.src.to_string().into_bytes();

for r in replacements {
code[(r.0 .0 - 1) as usize..(r.1 .0 - 1) as usize]
.iter_mut()
.for_each(|b| *b = b' ');
code[(r.0 .0 - 1) as usize..(r.1 .0 - 1) as usize].fill(b' ');
}

String::from_utf8(code).map_err(|_| anyhow::anyhow!("failed to convert to utf-8"))
}

#[derive(Default)]
struct TsStrip {
src: Lrc<String>,
replacements: Vec<(BytePos, BytePos)>,
}

impl TsStrip {
fn new(src: Lrc<String>) -> Self {
TsStrip {
src,
replacements: Default::default(),
}
}
}

impl TsStrip {
fn add_replacement(&mut self, span: Span) {
self.replacements.push((span.lo, span.hi));
Expand Down Expand Up @@ -228,18 +235,59 @@ impl Visit for TsStrip {
}) if &**sym == "this"
)
}) {
let lo = p.span.lo;
let hi = n.get(1).map(|x| x.span.lo).unwrap_or(p.span.hi);
self.add_replacement(span(lo, hi));
let mut span = p.span;

if n.len() == 1 {
let bytes = self.src.as_bytes();
span.hi.0 = skip_until(bytes, span.hi.0, b')');
} else {
span.hi = n[1].span.lo;
n[1..].visit_children_with(self);
}

self.add_replacement(span);

return;
}

n.visit_children_with(self);
}

n[1..].visit_children_with(self);
fn visit_import_decl(&mut self, n: &ImportDecl) {
if n.type_only {
self.add_replacement(n.span);
return;
}

n.visit_children_with(self);
}

fn visit_import_specifiers(&mut self, n: &[ImportSpecifier]) {
for (i, import) in n.iter().enumerate() {
let ImportSpecifier::Named(import) = import else {
continue;
};

if import.is_type_only {
let mut span = import.span;
span.hi.0 = n.get(i + 1).map(|x| x.span_lo().0).unwrap_or_else(|| {
let bytes = self.src.as_bytes();
skip_until(bytes, span.hi.0, b'}')
});
self.add_replacement(span);
}
}
}
}

fn span(lo: BytePos, hi: BytePos) -> Span {
Span::new(lo, hi, Default::default())
}

fn skip_until(bytes: &[u8], mut pos: u32, stop: u8) -> u32 {
while bytes[(pos - 1) as usize] != stop {
pos += 1;
}

pos
}
5 changes: 4 additions & 1 deletion crates/swc_fast_ts_strip/tests/fixture/this-param.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
export function foo( ) {}
export function bar( x ) {}
export function bar( x ) {}

export function foo2( ) {}
export function bar2( x ,) {}
5 changes: 4 additions & 1 deletion crates/swc_fast_ts_strip/tests/fixture/this-param.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
export function foo(this: number): void {}
export function bar(this: number, x: string): void {}
export function bar(this: number, x: string): void {}

export function foo2(this: number,): void {}
export function bar2(this: number, x: string,): void {}
7 changes: 7 additions & 0 deletions crates/swc_fast_ts_strip/tests/fixture/type-import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@


import { x4, } from "m3";
import { x7, } from "m4";
import { x9, } from "m5";
import { } from "m6";
import { x17 } from "m7";
7 changes: 7 additions & 0 deletions crates/swc_fast_ts_strip/tests/fixture/type-import.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type x1 from "m1";
import type { x2, x3 } from "m2";
import { x4, type x5, } from "m3";
import { type x6, x7, } from "m4";
import { type x8, x9, type x10 } from "m5";
import { type x11, type x12, type x13, } from "m6";
import { type x14, type x15, type x16, x17 } from "m7";

1 comment on commit 732d748

@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: 732d748 Previous: 9fca4ab Ratio
es/full/bugs-1 274210 ns/iter (± 1660) 270512 ns/iter (± 1726) 1.01
es/full/minify/libraries/antd 1843049582 ns/iter (± 37592332) 1704871804 ns/iter (± 43602828) 1.08
es/full/minify/libraries/d3 349347277 ns/iter (± 5207278) 339198229 ns/iter (± 800549) 1.03
es/full/minify/libraries/echarts 1546699197 ns/iter (± 35588990) 1436247990 ns/iter (± 13776329) 1.08
es/full/minify/libraries/jquery 104386706 ns/iter (± 734362) 102104947 ns/iter (± 179830) 1.02
es/full/minify/libraries/lodash 114534827 ns/iter (± 1841283) 113658786 ns/iter (± 185516) 1.01
es/full/minify/libraries/moment 60124646 ns/iter (± 682074) 59389159 ns/iter (± 129712) 1.01
es/full/minify/libraries/react 18363782 ns/iter (± 113457) 18373779 ns/iter (± 12081) 1.00
es/full/minify/libraries/terser 279467132 ns/iter (± 4662224) 272212961 ns/iter (± 1901547) 1.03
es/full/minify/libraries/three 486202229 ns/iter (± 14186842) 462794571 ns/iter (± 2405291) 1.05
es/full/minify/libraries/typescript 3356121436 ns/iter (± 50965877) 3175513403 ns/iter (± 29205992) 1.06
es/full/minify/libraries/victory 883394000 ns/iter (± 26392767) 716828784 ns/iter (± 9823934) 1.23
es/full/minify/libraries/vue 147852049 ns/iter (± 1089317) 144620159 ns/iter (± 225995) 1.02
es/full/codegen/es3 60437 ns/iter (± 168) 58577 ns/iter (± 1004) 1.03
es/full/codegen/es5 60497 ns/iter (± 132) 58754 ns/iter (± 305) 1.03
es/full/codegen/es2015 60355 ns/iter (± 204) 58555 ns/iter (± 278) 1.03
es/full/codegen/es2016 60445 ns/iter (± 197) 58595 ns/iter (± 381) 1.03
es/full/codegen/es2017 60376 ns/iter (± 79) 58751 ns/iter (± 258) 1.03
es/full/codegen/es2018 60266 ns/iter (± 67) 58583 ns/iter (± 112) 1.03
es/full/codegen/es2019 60335 ns/iter (± 129) 58569 ns/iter (± 380) 1.03
es/full/codegen/es2020 60404 ns/iter (± 346) 58599 ns/iter (± 127) 1.03
es/full/all/es3 160716304 ns/iter (± 536984) 160917180 ns/iter (± 525601) 1.00
es/full/all/es5 155813660 ns/iter (± 694262) 155364270 ns/iter (± 832849) 1.00
es/full/all/es2015 114642849 ns/iter (± 694417) 114164120 ns/iter (± 725683) 1.00
es/full/all/es2016 114005643 ns/iter (± 746545) 113346012 ns/iter (± 807842) 1.01
es/full/all/es2017 112853633 ns/iter (± 442373) 112710119 ns/iter (± 1161781) 1.00
es/full/all/es2018 110297938 ns/iter (± 725199) 110901153 ns/iter (± 618805) 0.99
es/full/all/es2019 111026820 ns/iter (± 371649) 109573156 ns/iter (± 502462) 1.01
es/full/all/es2020 107267967 ns/iter (± 479840) 106412552 ns/iter (± 604929) 1.01
es/full/parser 454005 ns/iter (± 3136) 449396 ns/iter (± 3499) 1.01
es/full/base/fixer 15953 ns/iter (± 132) 15299 ns/iter (± 77) 1.04
es/full/base/resolver_and_hygiene 80664 ns/iter (± 634) 78693 ns/iter (± 110) 1.03
serialization of serde 270 ns/iter (± 2) 272 ns/iter (± 0) 0.99
css/minify/libraries/bootstrap 21964506 ns/iter (± 69992) 21980486 ns/iter (± 50313) 1.00
css/visitor/compare/clone 1353371 ns/iter (± 14823) 1382387 ns/iter (± 23334) 0.98
css/visitor/compare/visit_mut_span 1478139 ns/iter (± 9781) 1507293 ns/iter (± 9792) 0.98
css/visitor/compare/visit_mut_span_panic 1504073 ns/iter (± 24817) 1532702 ns/iter (± 33163) 0.98
css/visitor/compare/fold_span 1999607 ns/iter (± 6394) 2015960 ns/iter (± 9489) 0.99
css/visitor/compare/fold_span_panic 2128514 ns/iter (± 9955) 2149738 ns/iter (± 13447) 0.99
css/lexer/bootstrap_5_1_3 3558393 ns/iter (± 3142) 3506905 ns/iter (± 5769) 1.01
css/lexer/foundation_6_7_4 2890688 ns/iter (± 1784) 2821424 ns/iter (± 13304) 1.02
css/lexer/tailwind_3_1_1 548602 ns/iter (± 746) 541090 ns/iter (± 10365) 1.01
css/parser/bootstrap_5_1_3 16363021 ns/iter (± 196187) 16547244 ns/iter (± 103501) 0.99
css/parser/foundation_6_7_4 13145786 ns/iter (± 62154) 13262412 ns/iter (± 436700) 0.99
css/parser/tailwind_3_1_1 2562731 ns/iter (± 3821) 2600235 ns/iter (± 5444) 0.99
es/codegen/colors 727180 ns/iter (± 398782) 731344 ns/iter (± 394579) 0.99
es/codegen/large 3006928 ns/iter (± 1617511) 2999917 ns/iter (± 1575023) 1.00
es/codegen/with-parser/colors 41530 ns/iter (± 194) 40528 ns/iter (± 494) 1.02
es/codegen/with-parser/large 456219 ns/iter (± 8060) 453900 ns/iter (± 1482) 1.01
es/minify/libraries/antd 1396050129 ns/iter (± 12995229) 1382797393 ns/iter (± 12231161) 1.01
es/minify/libraries/d3 281893853 ns/iter (± 5356337) 282698508 ns/iter (± 1640007) 1.00
es/minify/libraries/echarts 1146416987 ns/iter (± 7357447) 1200193358 ns/iter (± 14231528) 0.96
es/minify/libraries/jquery 82496883 ns/iter (± 87713) 82492586 ns/iter (± 196919) 1.00
es/minify/libraries/lodash 103933448 ns/iter (± 357662) 104659284 ns/iter (± 355039) 0.99
es/minify/libraries/moment 48261360 ns/iter (± 46592) 48239722 ns/iter (± 93615) 1.00
es/minify/libraries/react 16206080 ns/iter (± 30115) 16285708 ns/iter (± 18983) 1.00
es/minify/libraries/terser 217461579 ns/iter (± 576077) 218589466 ns/iter (± 990803) 0.99
es/minify/libraries/three 365745967 ns/iter (± 2007419) 370192843 ns/iter (± 4581401) 0.99
es/minify/libraries/typescript 2691452666 ns/iter (± 16007475) 2755175491 ns/iter (± 7207369) 0.98
es/minify/libraries/victory 561797488 ns/iter (± 8470418) 560753528 ns/iter (± 6363001) 1.00
es/minify/libraries/vue 120756849 ns/iter (± 109407) 121194571 ns/iter (± 401546) 1.00
es/visitor/compare/clone 1734585 ns/iter (± 9546) 1711419 ns/iter (± 4476) 1.01
es/visitor/compare/visit_mut_span 2072147 ns/iter (± 16900) 2041443 ns/iter (± 4831) 1.02
es/visitor/compare/visit_mut_span_panic 2080561 ns/iter (± 12128) 2049048 ns/iter (± 40538) 1.02
es/visitor/compare/fold_span 2801241 ns/iter (± 10371) 2772275 ns/iter (± 2998) 1.01
es/visitor/compare/fold_span_panic 2836993 ns/iter (± 7659) 2798485 ns/iter (± 5262) 1.01
es/lexer/colors 9402 ns/iter (± 26) 9693 ns/iter (± 201) 0.97
es/lexer/angular 4936910 ns/iter (± 19727) 5074134 ns/iter (± 10312) 0.97
es/lexer/backbone 638615 ns/iter (± 3156) 620334 ns/iter (± 6404) 1.03
es/lexer/jquery 3651916 ns/iter (± 5222) 3520659 ns/iter (± 8415) 1.04
es/lexer/jquery mobile 5356228 ns/iter (± 43735) 5262000 ns/iter (± 10693) 1.02
es/lexer/mootools 2889352 ns/iter (± 6668) 2860616 ns/iter (± 17398) 1.01
es/lexer/underscore 536148 ns/iter (± 2143) 530687 ns/iter (± 3777) 1.01
es/lexer/three 15979241 ns/iter (± 43793) 16552021 ns/iter (± 129632) 0.97
es/lexer/yui 2884914 ns/iter (± 18516) 2873967 ns/iter (± 13466) 1.00
es/lexer/cal-com 13020604 ns/iter (± 119722) 12871390 ns/iter (± 42689) 1.01
es/lexer/typescript 90092957 ns/iter (± 339253) 90338249 ns/iter (± 566772) 1.00
es/parser/colors 23597 ns/iter (± 214) 23578 ns/iter (± 101) 1.00
es/parser/angular 12557844 ns/iter (± 65550) 12427169 ns/iter (± 63634) 1.01
es/parser/backbone 1834141 ns/iter (± 5829) 1809122 ns/iter (± 8960) 1.01
es/parser/jquery 10114958 ns/iter (± 60063) 10036643 ns/iter (± 121644) 1.01
es/parser/jquery mobile 15295980 ns/iter (± 123310) 15569488 ns/iter (± 147184) 0.98
es/parser/mootools 7886979 ns/iter (± 17157) 7829884 ns/iter (± 93697) 1.01
es/parser/underscore 1595267 ns/iter (± 9685) 1576901 ns/iter (± 6466) 1.01
es/parser/three 43997946 ns/iter (± 197293) 45652851 ns/iter (± 596736) 0.96
es/parser/yui 7434215 ns/iter (± 65580) 7396925 ns/iter (± 83053) 1.01
es/parser/cal-com 40173103 ns/iter (± 196200) 43389450 ns/iter (± 1101125) 0.93
es/parser/typescript 269483417 ns/iter (± 1291980) 272941071 ns/iter (± 1649650) 0.99
es/preset-env/usage/builtin_type 136720 ns/iter (± 33636) 136372 ns/iter (± 34831) 1.00
es/preset-env/usage/property 16192 ns/iter (± 67) 17196 ns/iter (± 158) 0.94
es/resolver/typescript 97159461 ns/iter (± 3467009) 103688420 ns/iter (± 2555143) 0.94
es/fixer/typescript 79967537 ns/iter (± 1984968) 87318459 ns/iter (± 2067669) 0.92
es/hygiene/typescript 195036797 ns/iter (± 3493265) 199478947 ns/iter (± 2903856) 0.98
es/resolver_with_hygiene/typescript 325762961 ns/iter (± 5182430) 324347730 ns/iter (± 5184552) 1.00
es/visitor/base-perf/module_clone 57789 ns/iter (± 246) 58446 ns/iter (± 330) 0.99
es/visitor/base-perf/fold_empty 61184 ns/iter (± 711) 61581 ns/iter (± 1937) 0.99
es/visitor/base-perf/fold_noop_impl_all 60837 ns/iter (± 964) 61085 ns/iter (± 390) 1.00
es/visitor/base-perf/fold_noop_impl_vec 60926 ns/iter (± 476) 60987 ns/iter (± 208) 1.00
es/visitor/base-perf/boxing_boxed_clone 58 ns/iter (± 0) 59 ns/iter (± 0) 0.98
es/visitor/base-perf/boxing_unboxed_clone 47 ns/iter (± 0) 48 ns/iter (± 0) 0.98
es/visitor/base-perf/boxing_boxed 106 ns/iter (± 0) 106 ns/iter (± 0) 1
es/visitor/base-perf/boxing_unboxed 81 ns/iter (± 0) 89 ns/iter (± 0) 0.91
es/visitor/base-perf/visit_contains_this 2505 ns/iter (± 17) 2658 ns/iter (± 20) 0.94
es/base/parallel/resolver/typescript 2473412904 ns/iter (± 161110049) 2430282701 ns/iter (± 159830506) 1.02
es/base/parallel/hygiene/typescript 3289557651 ns/iter (± 44300517) 3338719870 ns/iter (± 34781870) 0.99
babelify-only 508551 ns/iter (± 6937) 490318 ns/iter (± 7318) 1.04
parse_and_babelify_angular 45788602 ns/iter (± 614242) 41887588 ns/iter (± 883098) 1.09
parse_and_babelify_backbone 4418678 ns/iter (± 131903) 4317306 ns/iter (± 158561) 1.02
parse_and_babelify_jquery 30254548 ns/iter (± 616582) 28062347 ns/iter (± 220638) 1.08
parse_and_babelify_jquery_mobile 51162536 ns/iter (± 1022881) 51320545 ns/iter (± 767655) 1.00
parse_and_babelify_mootools 28295722 ns/iter (± 464150) 25592257 ns/iter (± 351296) 1.11
parse_and_babelify_underscore 3809260 ns/iter (± 63894) 3659473 ns/iter (± 13608) 1.04
parse_and_babelify_yui 25858880 ns/iter (± 304197) 23984242 ns/iter (± 714443) 1.08
html/minify/document/css_spec 46859668 ns/iter (± 300137) 45243261 ns/iter (± 165366) 1.04
html/minify/document/github 18843645 ns/iter (± 191259) 18105395 ns/iter (± 26121) 1.04
html/minify/document/stackoverflow 16972941 ns/iter (± 225777) 16543237 ns/iter (± 494652) 1.03
html/minify/document_fragment/css_spec 46300816 ns/iter (± 299604) 44106945 ns/iter (± 88387) 1.05
html/minify/document_fragment/github 18293368 ns/iter (± 68853) 17868957 ns/iter (± 71704) 1.02
html/minify/document_fragment/stackoverflow 16783094 ns/iter (± 73859) 16361699 ns/iter (± 31201) 1.03
html/document/visitor/compare/clone 163229 ns/iter (± 1529) 165235 ns/iter (± 2606) 0.99
html/document/visitor/compare/visit_mut_span 181503 ns/iter (± 1426) 180930 ns/iter (± 638) 1.00
html/document/visitor/compare/visit_mut_span_panic 186819 ns/iter (± 4675) 186835 ns/iter (± 4316) 1.00
html/document/visitor/compare/fold_span 228739 ns/iter (± 755) 230459 ns/iter (± 762) 0.99
html/document/visitor/compare/fold_span_panic 280927 ns/iter (± 2069) 279069 ns/iter (± 1473) 1.01
html/document_fragment/visitor/compare/clone 166032 ns/iter (± 1199) 166802 ns/iter (± 1137) 1.00
html/document_fragment/visitor/compare/visit_mut_span 179781 ns/iter (± 798) 181546 ns/iter (± 1087) 0.99
html/document_fragment/visitor/compare/visit_mut_span_panic 186170 ns/iter (± 1524) 188583 ns/iter (± 925) 0.99
html/document_fragment/visitor/compare/fold_span 231065 ns/iter (± 1271) 231253 ns/iter (± 907) 1.00
html/document_fragment/visitor/compare/fold_span_panic 279834 ns/iter (± 1847) 277680 ns/iter (± 1014) 1.01
html/lexer/css_2021_spec 12871129 ns/iter (± 232636) 12729494 ns/iter (± 24898) 1.01
html/lexer/github_com_17_05_2022 4989138 ns/iter (± 6176) 4988645 ns/iter (± 12905) 1.00
html/lexer/stackoverflow_com_17_05_2022 4708761 ns/iter (± 7592) 4689077 ns/iter (± 11416) 1.00
html/parser/parser_document/css_2021_spec 25447948 ns/iter (± 791030) 25683570 ns/iter (± 59237) 0.99
html/parser/parser_document/github_com_17_05_2022 8494947 ns/iter (± 13462) 8557447 ns/iter (± 16846) 0.99
html/parser/parser_document/stackoverflow_com_17_05_2022 7361921 ns/iter (± 21831) 7493081 ns/iter (± 12389) 0.98
html/parser/parser_document_fragment/css_2021_spec 25284271 ns/iter (± 71833) 25516761 ns/iter (± 67031) 0.99
html/parser/parser_document_fragment/github_com_17_05_2022 8486656 ns/iter (± 12724) 8508019 ns/iter (± 24814) 1.00
html/parser/parser_document_fragment/stackoverflow_com_17_05_2022 7366622 ns/iter (± 6105) 7457078 ns/iter (± 9075) 0.99

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

Please sign in to comment.