Skip to content

Commit fd52c5c

Browse files
authored
perf(es/parser): Move found_module_item to Parser (#10388)
This may reduce the cost of creating `State`(72 bytes -> 64 bytes) in `parse_fn_body`.
1 parent d1770ac commit fd52c5c

File tree

6 files changed

+13
-11
lines changed

6 files changed

+13
-11
lines changed

Diff for: .changeset/selfish-pears-shake.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
swc_ecma_parser: patch
3+
swc_core: patch
4+
---
5+
6+
perf(es/parser): move `found_module_item` to `Parser`

Diff for: crates/swc_ecma_parser/src/parser/class_and_fn.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -1397,12 +1397,8 @@ impl<I: Tokens> Parser<I> {
13971397
},
13981398
);
13991399

1400-
let state = State {
1401-
labels: Vec::new(),
1402-
..Default::default()
1403-
};
14041400
self.with_ctx(ctx)
1405-
.with_state(state)
1401+
.with_state(State::default())
14061402
.parse_fn_body_inner(is_simple_parameter_list)
14071403
}
14081404
}

Diff for: crates/swc_ecma_parser/src/parser/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2069,7 +2069,7 @@ impl<I: Tokens> Parser<I> {
20692069
no_call: bool,
20702070
) -> PResult<Box<Expr>> {
20712071
if eat!(self, '.') {
2072-
self.state.found_module_item = true;
2072+
self.found_module_item = true;
20732073

20742074
let ident = self.parse_ident_name()?;
20752075

Diff for: crates/swc_ecma_parser/src/parser/expr/ops.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ impl<I: Tokens> Parser<I> {
399399

400400
// This has been checked if start_of_await_token == true,
401401
if start_of_await_token.is_none() && ctx.contains(Context::TopLevel) {
402-
self.state.found_module_item = true;
402+
self.found_module_item = true;
403403
if !ctx.contains(Context::CanBeModule) {
404404
self.emit_err(await_token, SyntaxError::TopLevelAwaitInScript);
405405
}

Diff for: crates/swc_ecma_parser/src/parser/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,14 @@ pub type PResult<T> = Result<T, Error>;
4848
pub struct Parser<I: Tokens> {
4949
state: State,
5050
input: Buffer<I>,
51+
found_module_item: bool,
5152
}
5253

5354
#[derive(Clone, Default)]
5455
struct State {
5556
labels: Vec<Atom>,
5657
/// Start position of an assignment expression.
5758
potential_arrow_start: Option<BytePos>,
58-
59-
found_module_item: bool,
6059
/// Start position of an AST node and the span of its trailing comma.
6160
trailing_commas: FxHashMap<BytePos, Span>,
6261
}
@@ -83,6 +82,7 @@ impl<I: Tokens> Parser<I> {
8382
Parser {
8483
state: Default::default(),
8584
input: Buffer::new(input),
85+
found_module_item: false,
8686
}
8787
}
8888

@@ -142,7 +142,7 @@ impl<I: Tokens> Parser<I> {
142142
let ctx = self.ctx() | Context::CanBeModule | Context::TopLevel;
143143

144144
let body: Vec<ModuleItem> = self.with_ctx(ctx).parse_block_body(true, None)?;
145-
let has_module_item = self.state.found_module_item
145+
let has_module_item = self.found_module_item
146146
|| body
147147
.iter()
148148
.any(|item| matches!(item, ModuleItem::ModuleDecl(..)));

Diff for: crates/swc_ecma_parser/src/parser/stmt.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ impl<'a, I: Tokens> Parser<I> {
124124
match cur!(self, true) {
125125
tok!("await") if include_decl || top_level => {
126126
if top_level {
127-
self.state.found_module_item = true;
127+
self.found_module_item = true;
128128
if !self.ctx().contains(Context::CanBeModule) {
129129
self.emit_err(self.input.cur_span(), SyntaxError::TopLevelAwaitInScript);
130130
}

0 commit comments

Comments
 (0)