Skip to content

Commit

Permalink
perf(codegen): fast path for options.print_comments()
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Dec 11, 2024
1 parent 2964a61 commit 932b760
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
6 changes: 4 additions & 2 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion crates/oxc_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
Expand All @@ -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![],
Expand All @@ -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
}
Expand Down

0 comments on commit 932b760

Please sign in to comment.