Skip to content

Commit 0a42695

Browse files
committed
perf(parser): pass span starts (u32) around instead of Span (2x u32) (#10433)
It it useless to pass around the end span, because it's always empty. Pass `Span`s around when it definitely contains an end span. This should reduce a bit of stack usage.
1 parent c538efa commit 0a42695

File tree

13 files changed

+81
-79
lines changed

13 files changed

+81
-79
lines changed

crates/oxc_parser/src/cursor.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,15 @@ pub struct ParserCheckpoint<'a> {
2020

2121
impl<'a> ParserImpl<'a> {
2222
#[inline]
23-
pub(crate) fn start_span(&self) -> Span {
24-
let token = self.cur_token();
25-
Span::new(token.start, 0)
23+
pub(crate) fn start_span(&self) -> u32 {
24+
self.token.start
2625
}
2726

2827
#[inline]
29-
pub(crate) fn end_span(&self, mut span: Span) -> Span {
30-
span.end = self.prev_token_end;
31-
debug_assert!(span.end >= span.start);
32-
span
28+
pub(crate) fn end_span(&self, start: u32) -> Span {
29+
let end = self.prev_token_end;
30+
debug_assert!(end >= start);
31+
Span::new(start, end)
3332
}
3433

3534
/// Get current token

crates/oxc_parser/src/js/arrow.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use oxc_allocator::Box;
22
use oxc_ast::{NONE, ast::*};
33
use oxc_diagnostics::Result;
4-
use oxc_span::{GetSpan, Span};
4+
use oxc_span::GetSpan;
55
use oxc_syntax::precedence::Precedence;
66

77
use super::Tristate;
@@ -12,7 +12,7 @@ struct ArrowFunctionHead<'a> {
1212
params: Box<'a, FormalParameters<'a>>,
1313
return_type: Option<Box<'a, TSTypeAnnotation<'a>>>,
1414
r#async: bool,
15-
span: Span,
15+
span: u32,
1616
has_return_colon: bool,
1717
}
1818

@@ -213,7 +213,7 @@ impl<'a> ParserImpl<'a> {
213213

214214
pub(crate) fn parse_simple_arrow_function_expression(
215215
&mut self,
216-
span: Span,
216+
span: u32,
217217
ident: Expression<'a>,
218218
r#async: bool,
219219
allow_return_type_in_arrow_function: bool,
@@ -229,7 +229,7 @@ impl<'a> ParserImpl<'a> {
229229
}
230230
_ => unreachable!(),
231231
};
232-
let params_span = self.end_span(ident.span);
232+
let params_span = self.end_span(ident.span.start);
233233
let ident = BindingPatternKind::BindingIdentifier(ident);
234234
let pattern = self.ast.binding_pattern(ident, NONE, false);
235235
let formal_parameter = self.ast.plain_formal_parameter(params_span, pattern);

crates/oxc_parser/src/js/binding.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use oxc_ast::{NONE, ast::*};
22
use oxc_diagnostics::Result;
3-
use oxc_span::{GetSpan, Span};
3+
use oxc_span::GetSpan;
44

55
use crate::{Context, ParserImpl, diagnostics, lexer::Kind};
66

@@ -165,7 +165,7 @@ impl<'a> ParserImpl<'a> {
165165
/// = `AssignmentExpression`[?In, ?Yield, ?Await]
166166
fn parse_initializer(
167167
&mut self,
168-
span: Span,
168+
span: u32,
169169
left: BindingPattern<'a>,
170170
) -> Result<BindingPattern<'a>> {
171171
if self.eat(Kind::Eq) {

crates/oxc_parser/src/js/class.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ impl<'a> ParserImpl<'a> {
1919
pub(crate) fn parse_class_statement(
2020
&mut self,
2121
stmt_ctx: StatementContext,
22-
start_span: Span,
22+
start_span: u32,
2323
) -> Result<Statement<'a>> {
2424
let modifiers = self.parse_modifiers(
2525
/* allow_decorators */ true, /* permit_const_as_modifier */ false,
@@ -40,7 +40,7 @@ impl<'a> ParserImpl<'a> {
4040
/// Section 15.7 Class Definitions
4141
pub(crate) fn parse_class_declaration(
4242
&mut self,
43-
start_span: Span,
43+
start_span: u32,
4444
modifiers: &Modifiers<'a>,
4545
) -> Result<Box<'a, Class<'a>>> {
4646
self.parse_class(start_span, ClassType::ClassDeclaration, modifiers)
@@ -57,14 +57,14 @@ impl<'a> ParserImpl<'a> {
5757

5858
fn parse_class(
5959
&mut self,
60-
start_span: Span,
60+
start_span: u32,
6161
r#type: ClassType,
6262
modifiers: &Modifiers<'a>,
6363
) -> Result<Box<'a, Class<'a>>> {
6464
self.bump_any(); // advance `class`
6565

6666
let decorators = self.consume_decorators();
67-
let start_span = decorators.iter().next().map_or(start_span, |d| d.span);
67+
let start_span = decorators.iter().next().map_or(start_span, |d| d.span.start);
6868

6969
let id = if self.cur_kind().is_binding_identifier() && !self.at(Kind::Implements) {
7070
Some(self.parse_binding_identifier()?)
@@ -391,7 +391,7 @@ impl<'a> ParserImpl<'a> {
391391

392392
fn parse_class_method_definition(
393393
&mut self,
394-
span: Span,
394+
span: u32,
395395
kind: MethodDefinitionKind,
396396
key: PropertyKey<'a>,
397397
computed: bool,
@@ -455,7 +455,7 @@ impl<'a> ParserImpl<'a> {
455455
/// `FieldDefinition`[?Yield, ?Await] ;
456456
fn parse_class_property_definition(
457457
&mut self,
458-
span: Span,
458+
span: u32,
459459
key: PropertyKey<'a>,
460460
computed: bool,
461461
r#static: bool,
@@ -497,7 +497,7 @@ impl<'a> ParserImpl<'a> {
497497

498498
/// `ClassStaticBlockStatementList` :
499499
/// `StatementList`[~Yield, +Await, ~Return]
500-
fn parse_class_static_block(&mut self, span: Span) -> Result<ClassElement<'a>> {
500+
fn parse_class_static_block(&mut self, span: u32) -> Result<ClassElement<'a>> {
501501
let block =
502502
self.context(Context::Await, Context::Yield | Context::Return, Self::parse_block)?;
503503
Ok(self.ast.class_element_static_block(self.end_span(span), block.unbox().body))
@@ -506,7 +506,7 @@ impl<'a> ParserImpl<'a> {
506506
/// <https://github.com/tc39/proposal-decorators>
507507
fn parse_class_accessor_property(
508508
&mut self,
509-
span: Span,
509+
span: u32,
510510
key: PropertyKey<'a>,
511511
computed: bool,
512512
r#static: bool,

crates/oxc_parser/src/js/declaration.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use oxc_allocator::Box;
22
use oxc_ast::{NONE, ast::*};
33
use oxc_diagnostics::Result;
4-
use oxc_span::{GetSpan, Span};
4+
use oxc_span::GetSpan;
55

66
use super::VariableDeclarationParent;
77
use crate::{
@@ -36,13 +36,13 @@ impl<'a> ParserImpl<'a> {
3636
pub(crate) fn parse_using_statement(&mut self) -> Result<Statement<'a>> {
3737
let mut decl = self.parse_using_declaration(StatementContext::StatementList)?;
3838
self.asi()?;
39-
decl.span = self.end_span(decl.span);
39+
decl.span = self.end_span(decl.span.start);
4040
Ok(Statement::VariableDeclaration(self.alloc(decl)))
4141
}
4242

4343
pub(crate) fn parse_variable_declaration(
4444
&mut self,
45-
start_span: Span,
45+
start_span: u32,
4646
decl_parent: VariableDeclarationParent,
4747
modifiers: &Modifiers<'a>,
4848
) -> Result<Box<'a, VariableDeclaration<'a>>> {

crates/oxc_parser/src/js/expression.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ impl<'a> ParserImpl<'a> {
197197
}
198198
}
199199

200-
fn parse_parenthesized_expression(&mut self, span: Span) -> Result<Expression<'a>> {
200+
fn parse_parenthesized_expression(&mut self, span: u32) -> Result<Expression<'a>> {
201201
self.expect(Kind::LParen)?;
202202
let expr_span = self.start_span();
203203
let mut expressions = self.context(Context::In, Context::Decorator, |p| {
@@ -436,7 +436,7 @@ impl<'a> ParserImpl<'a> {
436436
if self.at(Kind::Comma) {
437437
let comma_span = self.start_span();
438438
self.bump_any();
439-
self.state.trailing_commas.insert(span.start, self.end_span(comma_span));
439+
self.state.trailing_commas.insert(span, self.end_span(comma_span));
440440
}
441441
self.expect(Kind::RBrack)?;
442442
Ok(self.ast.expression_array(self.end_span(span), elements))
@@ -510,7 +510,7 @@ impl<'a> ParserImpl<'a> {
510510

511511
fn parse_tagged_template(
512512
&mut self,
513-
span: Span,
513+
span: u32,
514514
lhs: Expression<'a>,
515515
in_optional_chain: bool,
516516
type_parameters: Option<Box<'a, TSTypeParameterInstantiation<'a>>>,
@@ -708,7 +708,7 @@ impl<'a> ParserImpl<'a> {
708708
/// parse rhs of a member expression, starting from lhs
709709
fn parse_member_expression_rest(
710710
&mut self,
711-
lhs_span: Span,
711+
lhs_span: u32,
712712
lhs: Expression<'a>,
713713
in_optional_chain: &mut bool,
714714
) -> Result<Expression<'a>> {
@@ -784,7 +784,7 @@ impl<'a> ParserImpl<'a> {
784784
/// static member `a.b`
785785
fn parse_static_member_expression(
786786
&mut self,
787-
lhs_span: Span,
787+
lhs_span: u32,
788788
lhs: Expression<'a>,
789789
optional: bool,
790790
) -> Result<Expression<'a>> {
@@ -809,7 +809,7 @@ impl<'a> ParserImpl<'a> {
809809
/// `MemberExpression`[?Yield, ?Await] [ Expression[+In, ?Yield, ?Await] ]
810810
fn parse_computed_member_expression(
811811
&mut self,
812-
lhs_span: Span,
812+
lhs_span: u32,
813813
lhs: Expression<'a>,
814814
optional: bool,
815815
) -> Result<Expression<'a>> {
@@ -882,7 +882,7 @@ impl<'a> ParserImpl<'a> {
882882
/// Section 13.3 Call Expression
883883
fn parse_call_expression_rest(
884884
&mut self,
885-
lhs_span: Span,
885+
lhs_span: u32,
886886
lhs: Expression<'a>,
887887
in_optional_chain: &mut bool,
888888
) -> Result<Expression<'a>> {
@@ -923,7 +923,7 @@ impl<'a> ParserImpl<'a> {
923923

924924
fn parse_call_arguments(
925925
&mut self,
926-
lhs_span: Span,
926+
lhs_span: u32,
927927
lhs: Expression<'a>,
928928
optional: bool,
929929
type_parameters: Option<Box<'a, TSTypeParameterInstantiation<'a>>>,
@@ -958,7 +958,7 @@ impl<'a> ParserImpl<'a> {
958958
}
959959

960960
/// Section 13.4 Update Expression
961-
fn parse_update_expression(&mut self, lhs_span: Span) -> Result<Expression<'a>> {
961+
fn parse_update_expression(&mut self, lhs_span: u32) -> Result<Expression<'a>> {
962962
let kind = self.cur_kind();
963963
// ++ -- prefix update expressions
964964
if kind.is_update_operator() {
@@ -996,7 +996,7 @@ impl<'a> ParserImpl<'a> {
996996
/// Section 13.5 Unary Expression
997997
pub(crate) fn parse_unary_expression_or_higher(
998998
&mut self,
999-
lhs_span: Span,
999+
lhs_span: u32,
10001000
) -> Result<Expression<'a>> {
10011001
// ++ -- prefix update expressions
10021002
if self.is_update_expression() {
@@ -1007,7 +1007,7 @@ impl<'a> ParserImpl<'a> {
10071007

10081008
pub(crate) fn parse_simple_unary_expression(
10091009
&mut self,
1010-
lhs_span: Span,
1010+
lhs_span: u32,
10111011
) -> Result<Expression<'a>> {
10121012
match self.cur_kind() {
10131013
kind if kind.is_unary_operator() => self.parse_unary_expression(),
@@ -1066,7 +1066,7 @@ impl<'a> ParserImpl<'a> {
10661066
/// Section 13.6 - 13.13 Binary Expression
10671067
fn parse_binary_expression_rest(
10681068
&mut self,
1069-
lhs_span: Span,
1069+
lhs_span: u32,
10701070
lhs: Expression<'a>,
10711071
min_precedence: Precedence,
10721072
) -> Result<Expression<'a>> {
@@ -1143,7 +1143,7 @@ impl<'a> ParserImpl<'a> {
11431143
/// `ShortCircuitExpression`[?In, ?Yield, ?Await] ? `AssignmentExpression`[+In, ?Yield, ?Await] : `AssignmentExpression`[?In, ?Yield, ?Await]
11441144
fn parse_conditional_expression_rest(
11451145
&mut self,
1146-
lhs_span: Span,
1146+
lhs_span: u32,
11471147
lhs: Expression<'a>,
11481148
allow_return_type_in_arrow_function: bool,
11491149
) -> Result<Expression<'a>> {
@@ -1284,7 +1284,7 @@ impl<'a> ParserImpl<'a> {
12841284

12851285
fn parse_assignment_expression_recursive(
12861286
&mut self,
1287-
span: Span,
1287+
span: u32,
12881288
lhs: Expression<'a>,
12891289
allow_return_type_in_arrow_function: bool,
12901290
) -> Result<Expression<'a>> {
@@ -1305,7 +1305,7 @@ impl<'a> ParserImpl<'a> {
13051305
/// Section 13.16 Sequence Expression
13061306
fn parse_sequence_expression(
13071307
&mut self,
1308-
span: Span,
1308+
span: u32,
13091309
first_expression: Expression<'a>,
13101310
) -> Result<Expression<'a>> {
13111311
let mut expressions = self.ast.vec1(first_expression);
@@ -1318,7 +1318,7 @@ impl<'a> ParserImpl<'a> {
13181318

13191319
/// ``AwaitExpression`[Yield]` :
13201320
/// await `UnaryExpression`[?Yield, +Await]
1321-
fn parse_await_expression(&mut self, lhs_span: Span) -> Result<Expression<'a>> {
1321+
fn parse_await_expression(&mut self, lhs_span: u32) -> Result<Expression<'a>> {
13221322
let span = self.start_span();
13231323
if !self.ctx.has_await() {
13241324
self.error(diagnostics::await_expression(self.cur_token().span()));

crates/oxc_parser/src/js/function.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl<'a> ParserImpl<'a> {
111111

112112
pub(crate) fn parse_function(
113113
&mut self,
114-
span: Span,
114+
span: u32,
115115
id: Option<BindingIdentifier<'a>>,
116116
r#async: bool,
117117
generator: bool,
@@ -232,7 +232,7 @@ impl<'a> ParserImpl<'a> {
232232
/// at `function`
233233
pub(crate) fn parse_ts_function_impl(
234234
&mut self,
235-
start_span: Span,
235+
start_span: u32,
236236
func_kind: FunctionKind,
237237
modifiers: &Modifiers<'a>,
238238
) -> Result<Box<'a, Function<'a>>> {
@@ -254,7 +254,7 @@ impl<'a> ParserImpl<'a> {
254254
/// [Function Expression](https://tc39.es/ecma262/#prod-FunctionExpression)
255255
pub(crate) fn parse_function_expression(
256256
&mut self,
257-
span: Span,
257+
span: u32,
258258
r#async: bool,
259259
) -> Result<Expression<'a>> {
260260
let func_kind = FunctionKind::Expression;
@@ -309,7 +309,7 @@ impl<'a> ParserImpl<'a> {
309309

310310
let has_yield = self.ctx.has_yield();
311311
if !has_yield {
312-
self.error(diagnostics::yield_expression(Span::new(span.start, span.start + 5)));
312+
self.error(diagnostics::yield_expression(Span::new(span, span + 5)));
313313
}
314314

315315
let mut delegate = false;

0 commit comments

Comments
 (0)