Skip to content

Commit

Permalink
perf(es/hygiene): Fix performance bug (#3090)
Browse files Browse the repository at this point in the history
swc_ecma_transforms_base:
 - `hygiene`: Skip checking a symbol if we are sure that it will be discarded.

swc_ecma_laoder:
 - Optimize `NodeModulesResolver` by trying `.js` first.
  • Loading branch information
kdy1 authored Dec 21, 2021
1 parent c9def54 commit a81661c
Show file tree
Hide file tree
Showing 12 changed files with 819 additions and 801 deletions.
4 changes: 2 additions & 2 deletions crates/swc/tests/tsc-references/ES5For-of19_es5.1.normal.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ try {
var _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = undefined;
try {
for(var _iterator2 = [][Symbol.iterator](), _step2; !(_iteratorNormalCompletion1 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion1 = true){
var v2 = _step2.value;
v2;
var v3 = _step2.value;
v3;
}
} catch (err) {
_didIteratorError1 = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function hasKey(obj, keys) {
const key1 = keys[keys.length - 1];
return key1 in o;
}
function parse(args1, { "--": doubleDash = false , alias: alias2 = {
function parse(args1, { "--": doubleDash = false , alias: alias3 = {
} , boolean: __boolean = false , default: defaults = {
} , stopEarly =false , string =[] , unknown =(i)=>i
} = {
Expand All @@ -60,9 +60,9 @@ function parse(args1, { "--": doubleDash = false , alias: alias2 = {
}
const aliases = {
};
if (alias2 !== undefined) {
for(const key in alias2){
const val = getForce(alias2, key);
if (alias3 !== undefined) {
for(const key in alias3){
const val = getForce(alias3, key);
if (typeof val === "string") {
aliases[key] = [
val
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function hasKey(obj, keys) {
const key1 = keys[keys.length - 1];
return key1 in o;
}
function parse(args1, { "--": doubleDash = false , alias: alias2 = {
function parse(args1, { "--": doubleDash = false , alias: alias3 = {
} , boolean: __boolean = false , default: defaults = {
} , stopEarly =false , string =[] , unknown =(i)=>i
} = {
Expand All @@ -60,9 +60,9 @@ function parse(args1, { "--": doubleDash = false , alias: alias2 = {
}
const aliases = {
};
if (alias2 !== undefined) {
for(const key in alias2){
const val = getForce(alias2, key);
if (alias3 !== undefined) {
for(const key in alias3){
const val = getForce(alias3, key);
if (typeof val === "string") {
aliases[key] = [
val
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function hasKey(obj, keys) {
const key1 = keys[keys.length - 1];
return key1 in o;
}
function parse(args, { "--": doubleDash = false , alias: alias2 = {
function parse(args, { "--": doubleDash = false , alias: alias3 = {
} , boolean: __boolean = false , default: defaults = {
} , stopEarly =false , string =[] , unknown =(i1)=>i1
} = {
Expand All @@ -60,9 +60,9 @@ function parse(args, { "--": doubleDash = false , alias: alias2 = {
}
const aliases = {
};
if (alias2 !== undefined) {
for(const key in alias2){
const val = getForce(alias2, key);
if (alias3 !== undefined) {
for(const key in alias3){
const val = getForce(alias3, key);
if (typeof val === "string") {
aliases[key] = [
val
Expand Down
8 changes: 4 additions & 4 deletions crates/swc_bundler/tests/fixture/deno-9591/output/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function hasKey(obj, keys) {
const key1 = keys[keys.length - 1];
return key1 in o;
}
function parse(args, { "--": doubleDash = false , alias: alias2 = {
function parse(args, { "--": doubleDash = false , alias: alias3 = {
} , boolean: __boolean = false , default: defaults = {
} , stopEarly =false , string =[] , unknown =(i1)=>i1
} = {
Expand All @@ -60,9 +60,9 @@ function parse(args, { "--": doubleDash = false , alias: alias2 = {
}
const aliases = {
};
if (alias2 !== undefined) {
for(const key in alias2){
const val = getForce(alias2, key);
if (alias3 !== undefined) {
for(const key in alias3){
const val = getForce(alias3, key);
if (typeof val === "string") {
aliases[key] = [
val
Expand Down
21 changes: 19 additions & 2 deletions crates/swc_ecma_loader/src/resolvers/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,25 @@ impl NodeModulesResolver {
/// Resolve a path as a file. If `path` refers to a file, it is returned;
/// otherwise the `path` + each extension is tried.
fn resolve_as_file(&self, path: &Path) -> Result<Option<PathBuf>, Error> {
if path.is_file() {
return Ok(Some(path.to_path_buf()));
let try_exact = path.extension().is_some();
if try_exact {
if path.is_file() {
return Ok(Some(path.to_path_buf()));
}
} else {
// We try `.js` first.
let mut path = path.to_path_buf();
path.set_extension("js");
if path.is_file() {
return Ok(Some(path));
}
}

// Try exact file after checking .js, for performance
if !try_exact {
if path.is_file() {
return Ok(Some(path.to_path_buf()));
}
}

if let Some(name) = path.file_name() {
Expand Down
Loading

1 comment on commit a81661c

@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: a81661c Previous: ba2563f Ratio
full_es2015 206207432 ns/iter (± 13589130) 216390385 ns/iter (± 14649968) 0.95
full_es2016 168746280 ns/iter (± 8221170) 185160047 ns/iter (± 13503985) 0.91
full_es2017 179735890 ns/iter (± 11667023) 187724716 ns/iter (± 19813565) 0.96
full_es2018 170482993 ns/iter (± 9776961) 172649718 ns/iter (± 13651751) 0.99
full_es2019 169737906 ns/iter (± 6648302) 171821453 ns/iter (± 15037310) 0.99
full_es2020 171396015 ns/iter (± 16562833) 172426342 ns/iter (± 14886998) 0.99
full_es3 236805957 ns/iter (± 16611377) 237646080 ns/iter (± 20946292) 1.00
full_es5 221380258 ns/iter (± 15490601) 217306388 ns/iter (± 16446117) 1.02
parser 759486 ns/iter (± 48945) 791618 ns/iter (± 118346) 0.96

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

Please sign in to comment.