Skip to content

Commit 2f25ca6

Browse files
authored
perf(parser): optimize code around parsing delimited list and object (#11755)
parse_object_expression: - remove unnecessary `bump(Kind::Comma)` parse_delimited_list: - avoid `first` variable and related check by unrolling first iteration of loop - avoid `trailing_separator` variable by restructuring code - avoid mostly wasted call to `start_span()` (only possible if `separator` is always one character) parse_object_literal_element - avoid redundant kind check when parsing getter and setter
1 parent c53a7bd commit 2f25ca6

File tree

2 files changed

+16
-22
lines changed

2 files changed

+16
-22
lines changed

crates/oxc_parser/src/cursor.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -346,25 +346,21 @@ impl<'a> ParserImpl<'a> {
346346
F: Fn(&mut Self) -> T,
347347
{
348348
let mut list = self.ast.vec();
349-
let mut first = true;
350-
let mut trailing_separator = None;
349+
if self.at(close) || self.has_fatal_error() {
350+
return (list, None);
351+
}
352+
list.push(f(self));
351353
loop {
352-
if self.cur_kind() == close || self.has_fatal_error() {
353-
break;
354+
if self.at(close) || self.has_fatal_error() {
355+
return (list, None);
354356
}
355-
if first {
356-
first = false;
357-
} else {
358-
let separator_span = self.start_span();
359-
self.expect(separator);
360-
if self.at(close) {
361-
trailing_separator = Some(separator_span);
362-
break;
363-
}
357+
self.expect(separator);
358+
if self.at(close) {
359+
let trailing_separator = self.prev_token_end - 1;
360+
return (list, Some(trailing_separator));
364361
}
365362
list.push(f(self));
366363
}
367-
(list, trailing_separator)
368364
}
369365

370366
pub(crate) fn parse_delimited_list_with_rest<E, A, D>(

crates/oxc_parser/src/js/object.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ impl<'a> ParserImpl<'a> {
2626
Self::parse_object_expression_property,
2727
)
2828
});
29-
self.bump(Kind::Comma); // Trailing Comma
3029
self.expect(Kind::RCurly);
3130
self.ast.alloc_object_expression(self.end_span(span), object_expression_properties)
3231
}
@@ -47,13 +46,12 @@ impl<'a> ParserImpl<'a> {
4746
/* stop_on_start_of_class_static_block */ false,
4847
);
4948

50-
let kind = self.cur_kind();
51-
if self.parse_contextual_modifier(Kind::Get) || self.parse_contextual_modifier(Kind::Set) {
52-
return match kind {
53-
Kind::Get => self.parse_method_getter_setter(span, PropertyKind::Get, &modifiers),
54-
Kind::Set => self.parse_method_getter_setter(span, PropertyKind::Set, &modifiers),
55-
_ => unreachable!(),
56-
};
49+
if self.parse_contextual_modifier(Kind::Get) {
50+
return self.parse_method_getter_setter(span, PropertyKind::Get, &modifiers);
51+
}
52+
53+
if self.parse_contextual_modifier(Kind::Set) {
54+
return self.parse_method_getter_setter(span, PropertyKind::Set, &modifiers);
5755
}
5856

5957
let asterisk_token = self.eat(Kind::Star);

0 commit comments

Comments
 (0)