perf(codegen): fast path for options.print_comments() (#7806)

This commit is contained in:
Boshen 2024-12-11 15:13:29 +00:00
parent afc5f1e8b2
commit afaaffa262
2 changed files with 11 additions and 3 deletions

View file

@ -98,7 +98,9 @@ impl Gen for Directive<'_> {
impl Gen for Statement<'_> {
fn gen(&self, p: &mut Codegen, ctx: Context) {
p.print_statement_comments(self.span().start);
if p.print_comments {
p.print_statement_comments(self.span().start);
}
match self {
Self::BlockStatement(stmt) => stmt.print(p, ctx),
Self::BreakStatement(stmt) => stmt.print(p, ctx),
@ -686,7 +688,7 @@ impl Gen for Function<'_> {
impl Gen for FunctionBody<'_> {
fn gen(&self, p: &mut Codegen, ctx: Context) {
let span_end = self.span.end;
let comments_at_end = if !p.options.minify && span_end > 0 {
let comments_at_end = if p.print_comments && span_end > 0 {
p.get_statement_comments(span_end - 1)
} else {
None

View file

@ -104,6 +104,8 @@ pub struct Codegen<'a> {
/// Fast path for [CodegenOptions::single_quote]
quote: u8,
/// Fast path for if print comments
print_comments: bool,
// Builders
comments: CommentsMap,
@ -151,8 +153,10 @@ impl<'a> Codegen<'a> {
/// This is equivalent to [`Codegen::default`].
#[must_use]
pub fn new() -> Self {
let options = CodegenOptions::default();
let print_comments = options.print_comments();
Self {
options: CodegenOptions::default(),
options,
source_text: "",
mangler: None,
code: CodeBuffer::default(),
@ -168,6 +172,7 @@ impl<'a> Codegen<'a> {
start_of_default_export: 0,
indent: 0,
quote: b'"',
print_comments,
comments: CommentsMap::default(),
start_of_annotation_comment: None,
legal_comments: vec![],
@ -179,6 +184,7 @@ impl<'a> Codegen<'a> {
#[must_use]
pub fn with_options(mut self, options: CodegenOptions) -> Self {
self.quote = if options.single_quote { b'\'' } else { b'"' };
self.print_comments = options.print_comments();
self.options = options;
self
}