Skip to content

Commit

Permalink
perf(es/minifier): Use Vec<u8> as a buffer for base54 (#3993)
Browse files Browse the repository at this point in the history
Description:
We are only using ASCII characters so we can avoid utf8 logics by using `Vec<u8>` as a buffer and converting it into `String` at the end.
  • Loading branch information
kdy1 authored Mar 13, 2022
1 parent 63177b7 commit f7b212b
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions crates/swc_ecma_minifier/src/util/base54.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,24 @@ pub(crate) fn encode(init: &mut usize, skip_reserved: bool) -> String {
base <<= 6;
}

let mut ret = String::new();
let mut ret = vec![];

base /= 54;
let mut c = BASE54_DEFAULT_CHARS[n / base] as char;
let mut c = BASE54_DEFAULT_CHARS[n / base];
ret.push(c);

while base > 1 {
n %= base;
base >>= 6;
c = BASE54_DEFAULT_CHARS[n / base] as char;
c = BASE54_DEFAULT_CHARS[n / base];

ret.push(c);
}

ret
unsafe {
// Safety: We are only using ascii characters
String::from_utf8_unchecked(ret)
}
}

#[allow(unused)]
Expand Down

2 comments on commit f7b212b

@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: f7b212b Previous: 48f2fd8 Ratio
base_tr_fixer 22042 ns/iter (± 798) 26983 ns/iter (± 986) 0.82
base_tr_resolver_and_hygiene 106643 ns/iter (± 7362) 152215 ns/iter (± 27810) 0.70
codegen_es2015 26932 ns/iter (± 307) 33056 ns/iter (± 432) 0.81
codegen_es2016 26937 ns/iter (± 366) 33060 ns/iter (± 227) 0.81
codegen_es2017 26950 ns/iter (± 315) 33133 ns/iter (± 297) 0.81
codegen_es2018 27001 ns/iter (± 320) 33206 ns/iter (± 479) 0.81
codegen_es2019 27017 ns/iter (± 940) 33163 ns/iter (± 320) 0.81
codegen_es2020 27020 ns/iter (± 268) 33430 ns/iter (± 308) 0.81
codegen_es3 26917 ns/iter (± 217) 33036 ns/iter (± 501) 0.81
codegen_es5 26941 ns/iter (± 257) 33176 ns/iter (± 989) 0.81
full_es2015 149418673 ns/iter (± 5486243) 169219831 ns/iter (± 15624262) 0.88
full_es2016 151088651 ns/iter (± 8585538) 167632085 ns/iter (± 8316579) 0.90
full_es2017 150558097 ns/iter (± 6783729) 167556189 ns/iter (± 9316406) 0.90
full_es2018 149111158 ns/iter (± 6607460) 165701242 ns/iter (± 11560472) 0.90
full_es2019 147977972 ns/iter (± 15067140) 165619059 ns/iter (± 12145638) 0.89
full_es2020 142071674 ns/iter (± 9101136) 158969848 ns/iter (± 10659510) 0.89
full_es3 206246495 ns/iter (± 14540748) 226875639 ns/iter (± 11333093) 0.91
full_es5 196325551 ns/iter (± 16156082) 214012568 ns/iter (± 18792593) 0.92
parser 587542 ns/iter (± 25344) 682642 ns/iter (± 14168) 0.86
ser_ast_node 146 ns/iter (± 1) 185 ns/iter (± 3) 0.79
ser_serde 151 ns/iter (± 2) 188 ns/iter (± 4) 0.80
emit_colors 5654668 ns/iter (± 4286655) 13245295 ns/iter (± 8985966) 0.43
emit_large 100085012 ns/iter (± 134268243) 128924876 ns/iter (± 206867744) 0.78
base_clone 7621948 ns/iter (± 4879319) 2726824 ns/iter (± 321657) 2.80
fold_span 11080854 ns/iter (± 5729039) 4828155 ns/iter (± 911200) 2.30
fold_span_panic 10976258 ns/iter (± 6746706) 5176950 ns/iter (± 760540) 2.12
visit_mut_span 9436881 ns/iter (± 5605349) 3363850 ns/iter (± 327400) 2.81
visit_mut_span_panic 9488842 ns/iter (± 6022084) 3437921 ns/iter (± 457092) 2.76
usage_builtin_type 14940181 ns/iter (± 9334557) 18699816 ns/iter (± 11595138) 0.80
usage_property 355824 ns/iter (± 4928) 440858 ns/iter (± 4481) 0.81
boxing_boxed 156 ns/iter (± 2) 211 ns/iter (± 1) 0.74
boxing_boxed_clone 76 ns/iter (± 2) 115 ns/iter (± 0) 0.66
boxing_unboxed 113 ns/iter (± 1) 145 ns/iter (± 0) 0.78
boxing_unboxed_clone 62 ns/iter (± 0) 79 ns/iter (± 0) 0.78
time_10 314 ns/iter (± 6) 409 ns/iter (± 11) 0.77
time_15 671 ns/iter (± 22) 849 ns/iter (± 40) 0.79
time_20 1271 ns/iter (± 32) 1417 ns/iter (± 18) 0.90
time_40 6667 ns/iter (± 85) 5127 ns/iter (± 42) 1.30
time_5 101 ns/iter (± 2) 123 ns/iter (± 2) 0.82
time_60 15472 ns/iter (± 168) 10750 ns/iter (± 55) 1.44
total 0 ns/iter (± 0) 0 ns/iter (± 0) NaN

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

@github-actions
Copy link

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: f7b212b Previous: 48f2fd8 Ratio
base_clone 7621948 ns/iter (± 4879319) 2726824 ns/iter (± 321657) 2.80
fold_span 11080854 ns/iter (± 5729039) 4828155 ns/iter (± 911200) 2.30
fold_span_panic 10976258 ns/iter (± 6746706) 5176950 ns/iter (± 760540) 2.12
visit_mut_span 9436881 ns/iter (± 5605349) 3363850 ns/iter (± 327400) 2.81
visit_mut_span_panic 9488842 ns/iter (± 6022084) 3437921 ns/iter (± 457092) 2.76

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

CC: @kdy1

Please sign in to comment.