Skip to content

Commit

Permalink
Add --no-source-compression option to reduce compile time (#581)
Browse files Browse the repository at this point in the history
* Add --dev option to reduce compile time

close #579

* refactor

* cargo fmt
  • Loading branch information
orisano authored Jan 5, 2024
1 parent 45d7153 commit 585e495
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 10 deletions.
4 changes: 4 additions & 0 deletions crates/cli/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ pub struct CompileCommandOpts {
#[structopt(short = "n")]
/// Optional WIT world name for WIT file. Must be specified if WIT is file path is specified.
pub wit_world: Option<String>,

#[structopt(long = "no-source-compression")]
/// Disable source code compression, which reduces compile time at the expense of generating larger WebAssembly files.
pub no_source_compression: bool,
}

#[derive(Debug, StructOpt)]
Expand Down
4 changes: 2 additions & 2 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ fn main() -> Result<()> {
(Some(wit), Some(world)) => exports::process_exports(&js, wit, world),
}?;
let wasm = if opts.dynamic {
dynamic_generator::generate(&js, exports)?
dynamic_generator::generate(&js, exports, opts.no_source_compression)?
} else {
static_generator::generate(&js, exports)?
static_generator::generate(&js, exports, opts.no_source_compression)?
};
fs::write(&opts.output, wasm)?;
Ok(())
Expand Down
12 changes: 10 additions & 2 deletions crates/cli/src/wasm_generator/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ use walrus::{DataKind, FunctionBuilder, Module, ValType};
// (data (;0;) "\02\05\18function.mjs\06foo\0econsole\06log\06bar\0f\bc\03\00\01\00\00\be\03\00\00\0e\00\06\01\a0\01\00\00\00\03\01\01\1a\00\be\03\00\01\08\ea\05\c0\00\e1)8\e0\00\00\00B\e1\00\00\00\04\e2\00\00\00$\01\00)\bc\03\01\04\01\00\07\0a\0eC\06\01\be\03\00\00\00\03\00\00\13\008\e0\00\00\00B\e1\00\00\00\04\df\00\00\00$\01\00)\bc\03\01\02\03]")
// (data (;1;) "foo")
// )
pub fn generate(js: &JS, exported_functions: Vec<Export>) -> Result<Vec<u8>> {
pub fn generate(
js: &JS,
exported_functions: Vec<Export>,
no_source_compression: bool,
) -> Result<Vec<u8>> {
let mut module = Module::with_config(transform::module_config());

const IMPORT_NAMESPACE: &str = "javy_quickjs_provider_v1";
Expand All @@ -86,7 +90,11 @@ pub fn generate(js: &JS, exported_functions: Vec<Export>) -> Result<Vec<u8>> {
let (memory, _) = module.add_import_memory(IMPORT_NAMESPACE, "memory", false, 0, None);

transform::add_producers_section(&mut module.producers);
module.customs.add(SourceCodeSection::new(js)?);
if no_source_compression {
module.customs.add(SourceCodeSection::uncompressed(js)?);
} else {
module.customs.add(SourceCodeSection::compressed(js)?);
}

let bytecode = js.compile()?;
let bytecode_len: i32 = bytecode.len().try_into()?;
Expand Down
8 changes: 6 additions & 2 deletions crates/cli/src/wasm_generator/static.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use super::transform::{self, SourceCodeSection};

static mut WASI: OnceLock<WasiCtx> = OnceLock::new();

pub fn generate(js: &JS, exports: Vec<Export>) -> Result<Vec<u8>> {
pub fn generate(js: &JS, exports: Vec<Export>, no_source_compression: bool) -> Result<Vec<u8>> {
let wasm = include_bytes!(concat!(env!("OUT_DIR"), "/engine.wasm"));

let wasi = WasiCtxBuilder::new()
Expand Down Expand Up @@ -97,7 +97,11 @@ pub fn generate(js: &JS, exports: Vec<Export>) -> Result<Vec<u8>> {
let wasm = module.write();

let mut module = transform::module_config().parse(&wasm)?;
module.customs.add(SourceCodeSection::new(js)?);
if no_source_compression {
module.customs.add(SourceCodeSection::uncompressed(js)?);
} else {
module.customs.add(SourceCodeSection::compressed(js)?);
}
transform::add_producers_section(&mut module.producers);
Ok(module.emit_wasm())
}
Expand Down
14 changes: 10 additions & 4 deletions crates/cli/src/wasm_generator/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@ use crate::js::JS;

#[derive(Debug)]
pub struct SourceCodeSection {
compressed_source_code: Vec<u8>,
source_code: Vec<u8>,
}

impl SourceCodeSection {
pub fn new(js: &JS) -> Result<SourceCodeSection> {
pub fn compressed(js: &JS) -> Result<SourceCodeSection> {
Ok(SourceCodeSection {
compressed_source_code: js.compress()?,
source_code: js.compress()?,
})
}

pub fn uncompressed(js: &JS) -> Result<SourceCodeSection> {
Ok(SourceCodeSection {
source_code: js.as_bytes().to_vec(),
})
}
}
Expand All @@ -24,7 +30,7 @@ impl CustomSection for SourceCodeSection {
}

fn data(&self, _ids_to_indices: &IdsToIndices) -> Cow<[u8]> {
(&self.compressed_source_code).into()
(&self.source_code).into()
}
}

Expand Down

0 comments on commit 585e495

Please sign in to comment.