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<'_> { impl Gen for Statement<'_> {
fn gen(&self, p: &mut Codegen, ctx: Context) { 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 { match self {
Self::BlockStatement(stmt) => stmt.print(p, ctx), Self::BlockStatement(stmt) => stmt.print(p, ctx),
Self::BreakStatement(stmt) => stmt.print(p, ctx), Self::BreakStatement(stmt) => stmt.print(p, ctx),
@ -686,7 +688,7 @@ impl Gen for Function<'_> {
impl Gen for FunctionBody<'_> { impl Gen for FunctionBody<'_> {
fn gen(&self, p: &mut Codegen, ctx: Context) { fn gen(&self, p: &mut Codegen, ctx: Context) {
let span_end = self.span.end; 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) p.get_statement_comments(span_end - 1)
} else { } else {
None None

View file

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