Skip to content

Commit

Permalink
feat(codegen): remove const generic MINIFY
Browse files Browse the repository at this point in the history
This is a premature optimization, makes the code complicated,
and bloats the final binary size.
  • Loading branch information
Boshen committed Aug 20, 2024
1 parent 081e2a3 commit da469d7
Show file tree
Hide file tree
Showing 17 changed files with 478 additions and 479 deletions.
16 changes: 6 additions & 10 deletions crates/oxc/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{mem, ops::ControlFlow, path::Path};

use oxc_allocator::Allocator;
use oxc_ast::{ast::Program, Trivias};
use oxc_codegen::{CodeGenerator, CodegenOptions, CommentOptions, WhitespaceRemover};
use oxc_codegen::{CodeGenerator, CodegenOptions, CommentOptions};
use oxc_diagnostics::OxcDiagnostic;
use oxc_parser::{ParseOptions, Parser, ParserReturn};
use oxc_span::SourceType;
Expand Down Expand Up @@ -216,14 +216,10 @@ pub trait CompilerInterface {
) -> String {
let comment_options = CommentOptions { preserve_annotate_comments: true };

if self.remove_whitespace() {
WhitespaceRemover::new().with_options(options).build(program).source_text
} else {
CodeGenerator::new()
.with_options(options)
.enable_comment(source_text, trivias.clone(), comment_options)
.build(program)
.source_text
}
CodeGenerator::new()
.with_options(CodegenOptions { minify: self.remove_whitespace(), ..options })
.enable_comment(source_text, trivias.clone(), comment_options)
.build(program)
.source_text
}
}
7 changes: 5 additions & 2 deletions crates/oxc_codegen/examples/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::{env, path::Path};

use oxc_allocator::Allocator;
use oxc_codegen::{CodeGenerator, CommentOptions, WhitespaceRemover};
use oxc_codegen::{CodeGenerator, CodegenOptions, CommentOptions};
use oxc_parser::Parser;
use oxc_span::SourceType;
use pico_args::Arguments;
Expand Down Expand Up @@ -68,7 +68,10 @@ fn main() -> std::io::Result<()> {
if minify {
let allocator = Allocator::default();
let ret = Parser::new(&allocator, &source_text, source_type).parse();
let minified = WhitespaceRemover::new().build(&ret.program).source_text;
let minified = CodeGenerator::new()
.with_options(CodegenOptions { minify: true, ..CodegenOptions::default() })
.build(&ret.program)
.source_text;
println!("Minified:");
println!("{minified}");
}
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_codegen/src/annotation_comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl From<(Comment, AnnotationKind)> for AnnotationComment {
}
}

impl<'a, const MINIFY: bool> Codegen<'a, MINIFY> {
impl<'a> Codegen<'a> {
pub(crate) fn get_leading_annotate_comments(
&mut self,
node_start: u32,
Expand Down
10 changes: 5 additions & 5 deletions crates/oxc_codegen/src/binary_expr_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ pub enum BinaryishOperator {
Logical(LogicalOperator),
}

impl<const MINIFY: bool> Gen<MINIFY> for BinaryishOperator {
fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) {
impl Gen for BinaryishOperator {
fn gen(&self, p: &mut Codegen, ctx: Context) {
match self {
Self::Binary(op) => op.gen(p, ctx),
Self::Logical(op) => op.gen(p, ctx),
Expand Down Expand Up @@ -89,7 +89,7 @@ pub struct BinaryExpressionVisitor<'a> {
}

impl<'a> BinaryExpressionVisitor<'a> {
pub fn gen_expr<const MINIFY: bool>(v: Self, p: &mut Codegen<'a, { MINIFY }>) {
pub fn gen_expr(v: Self, p: &mut Codegen<'a>) {
let mut v = v;
let stack_bottom = p.binary_expr_stack.len();
loop {
Expand Down Expand Up @@ -133,7 +133,7 @@ impl<'a> BinaryExpressionVisitor<'a> {
}
}

pub fn check_and_prepare<const MINIFY: bool>(&mut self, p: &mut Codegen<{ MINIFY }>) -> bool {
pub fn check_and_prepare(&mut self, p: &mut Codegen) -> bool {
let e = self.e;
self.operator = e.operator();

Expand Down Expand Up @@ -181,7 +181,7 @@ impl<'a> BinaryExpressionVisitor<'a> {
true
}

pub fn visit_right_and_finish<const MINIFY: bool>(&self, p: &mut Codegen<{ MINIFY }>) {
pub fn visit_right_and_finish(&self, p: &mut Codegen) {
p.print_soft_space();
self.operator.gen(p, Context::empty());
p.print_soft_space();
Expand Down
Loading

0 comments on commit da469d7

Please sign in to comment.