Skip to content

Commit

Permalink
perf(es/parser): Remove needless strcmp ops (#8223)
Browse files Browse the repository at this point in the history
**Description:**


## The current `main`

```
es/parser/angular       time:   [7.9848 ms 8.0003 ms 8.0243 ms]
```

## This PR

```
es/parser/angular       time:   [7.3380 ms 7.3498 ms 7.3663 ms]
```
  • Loading branch information
kdy1 committed Nov 6, 2023
1 parent 968345b commit 3833cf4
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 171 deletions.
64 changes: 54 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/swc_ecma_ast/Cargo.toml
Expand Up @@ -46,6 +46,7 @@ unicode-id = "0.3"
string_enum = { version = "0.4.1", path = "../string_enum" }
swc_atoms = { version = "0.6.0", path = "../swc_atoms" }
swc_common = { version = "0.33.3", path = "../swc_common" }
phf = { version = "0.11.2", features = ["macros"] }

[dev-dependencies]
serde_json = "1"
146 changes: 75 additions & 71 deletions crates/swc_ecma_ast/src/ident.rs
@@ -1,5 +1,6 @@
use std::fmt::Display;

use phf::phf_set;
use scoped_tls::scoped_thread_local;
use swc_atoms::{js_word, Atom};
use swc_common::{
Expand Down Expand Up @@ -317,92 +318,95 @@ impl Ident {
}
}

static RESERVED: phf::Set<&str> = phf_set!(
"break",
"case",
"catch",
"class",
"const",
"continue",
"debugger",
"default",
"delete",
"do",
"else",
"enum",
"export",
"extends",
"false",
"finally",
"for",
"function",
"if",
"import",
"in",
"instanceof",
"new",
"null",
"package",
"return",
"super",
"switch",
"this",
"throw",
"true",
"try",
"typeof",
"var",
"void",
"while",
"with",
);

static RESSERVED_IN_STRICT_MODE: phf::Set<&str> = phf_set!(
"implements",
"interface",
"let",
"package",
"private",
"protected",
"public",
"static",
"yield",
);

static RESERVED_IN_ES3: phf::Set<&str> = phf_set!(
"abstract",
"boolean",
"byte",
"char",
"double",
"final",
"float",
"goto",
"int",
"long",
"native",
"short",
"synchronized",
"throws",
"transient",
"volatile",
);

pub trait IdentExt: AsRef<str> {
fn is_reserved(&self) -> bool {
[
"break",
"case",
"catch",
"class",
"const",
"continue",
"debugger",
"default",
"delete",
"do",
"else",
"enum",
"export",
"extends",
"false",
"finally",
"for",
"function",
"if",
"import",
"in",
"instanceof",
"new",
"null",
"package",
"return",
"super",
"switch",
"this",
"throw",
"true",
"try",
"typeof",
"var",
"void",
"while",
"with",
]
.contains(&self.as_ref())
RESERVED.contains(self.as_ref())
}

fn is_reserved_in_strict_mode(&self, is_module: bool) -> bool {
if is_module && self.as_ref() == "await" {
return true;
}
[
"implements",
"interface",
"let",
"package",
"private",
"protected",
"public",
"static",
"yield",
]
.contains(&self.as_ref())
RESSERVED_IN_STRICT_MODE.contains(self.as_ref())
}

fn is_reserved_in_strict_bind(&self) -> bool {
["eval", "arguments"].contains(&self.as_ref())
}

fn is_reserved_in_es3(&self) -> bool {
[
"abstract",
"boolean",
"byte",
"char",
"double",
"final",
"float",
"goto",
"int",
"long",
"native",
"short",
"synchronized",
"throws",
"transient",
"volatile",
]
.contains(&self.as_ref())
RESERVED_IN_ES3.contains(self.as_ref())
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/swc_ecma_parser/Cargo.toml
Expand Up @@ -37,6 +37,7 @@ swc_atoms = { version = "0.6.0", path = "../swc_atoms" }
swc_common = { version = "0.33.3", path = "../swc_common" }
swc_ecma_ast = { version = "0.110.3", path = "../swc_ecma_ast" }
swc_ecma_visit = { version = "0.96.3", path = "../swc_ecma_visit", optional = true }
phf = { version = "0.11.2", features = ["macros"] }

[target.'cfg(not(any(target_arch = "wasm32", target_arch = "arm")))'.dependencies]
stacker = { version = "0.1.15", optional = true }
Expand Down

0 comments on commit 3833cf4

Please sign in to comment.