perf(parser): move annotation comment handling out of hot paths (#28139)

Applying `@__PURE__` and `@__NO_SIDE_EFFECTS__` comments is rare, so move it into `#[cold]` helpers to keep `with_pure_comments`, `parse_assignment_expression` and `parse_statement_list_item` small.
This commit is contained in:
camc314 committed 2026-10-23 22:58:23 +00:00
1 parent 7f3c908642
commit a22c9f2abc
3 files changed
+49 -17

No files matched your search

+30 -8
View File
@@ -1,3 +1,5 @@
use std::num::NonZeroU32;
use cow_utils::CowUtils;
use oxc_allocator::{ArenaBox, ArenaVec, GetAllocator, ReplaceWith};
use oxc_ast::ast::*;
@@ -785,10 +787,8 @@ impl<'a, C: Config> ParserImpl<'a, C> {
) -> Expression<'a> {
let pure_comments = self.lexer.trivia_builder.previous_token_pure_comments();
let mut expr = parse(self);
if let Some(comments) = pure_comments
&& Self::set_pure_on_call_or_new_expr(&mut expr)
{
self.lexer.trivia_builder.mark_pure_comments_applied(comments);
if let Some(comments) = pure_comments {
self.apply_pure_comments_to_call(comments, &mut expr);
}
expr
}
@@ -1556,15 +1556,37 @@ impl<'a, C: Config> ParserImpl<'a, C> {
let mut expr =
self.parse_conditional_expression_rest(start, lhs, allow_return_type_in_arrow_function);
if let Some(comments) = no_side_effects_comments
&& Self::set_pure_on_function_expr(&mut expr)
{
self.lexer.trivia_builder.mark_no_side_effects_comments_applied(comments);
if let Some(comments) = no_side_effects_comments {
self.apply_no_side_effects_comments_to_function(comments, &mut expr);
}
expr
}
#[cold]
#[inline(never)]
fn apply_pure_comments_to_call(
&mut self,
comments: (u32, NonZeroU32),
expr: &mut Expression<'a>,
) {
if Self::set_pure_on_call_or_new_expr(expr) {
self.lexer.trivia_builder.mark_pure_comments_applied(comments);
}
}
#[cold]
#[inline(never)]
fn apply_no_side_effects_comments_to_function(
&mut self,
comments: (u32, NonZeroU32),
expr: &mut Expression<'a>,
) {
if Self::set_pure_on_function_expr(expr) {
self.lexer.trivia_builder.mark_no_side_effects_comments_applied(comments);
}
}
fn set_pure_on_call_or_new_expr(expr: &mut Expression<'a>) -> bool {
match expr.get_inner_expression_mut() {
Expression::CallExpression(call_expr) => {
+2 -4
View File
@@ -561,10 +561,8 @@ impl<'a, C: Config> ParserImpl<'a, C> {
}
};
let mut stmt = Statement::from(decl);
if let Some(comments) = no_side_effects_comments
&& Self::set_pure_on_function_stmt(&mut stmt)
{
self.lexer.trivia_builder.mark_no_side_effects_comments_applied(comments);
if let Some(comments) = no_side_effects_comments {
self.apply_no_side_effects_comments_to_statement(comments, &mut stmt);
}
stmt
}
+17 -5
View File
@@ -1,3 +1,5 @@
use std::num::NonZeroU32;
use oxc_allocator::{ArenaBox, ArenaVec};
use oxc_ast::ast::*;
use oxc_span::{GetSpan, Span};
@@ -198,16 +200,26 @@ impl<'a, C: Config> ParserImpl<'a, C> {
_ => self.parse_expression_or_labeled_statement(),
};
if let Some(comments) = no_side_effects_comments
&& Self::set_pure_on_function_stmt(&mut stmt)
{
self.lexer.trivia_builder.mark_no_side_effects_comments_applied(comments);
if let Some(comments) = no_side_effects_comments {
self.apply_no_side_effects_comments_to_statement(comments, &mut stmt);
}
stmt
}
pub(super) fn set_pure_on_function_stmt(stmt: &mut Statement<'a>) -> bool {
#[cold]
#[inline(never)]
pub(super) fn apply_no_side_effects_comments_to_statement(
&mut self,
comments: (u32, NonZeroU32),
stmt: &mut Statement<'a>,
) {
if Self::set_pure_on_function_stmt(stmt) {
self.lexer.trivia_builder.mark_no_side_effects_comments_applied(comments);
}
}
fn set_pure_on_function_stmt(stmt: &mut Statement<'a>) -> bool {
match stmt {
Statement::FunctionDeclaration(func) => {
func.pure = true;