Skip to content

Commit 585e495

Browse files
authored
Add --no-source-compression option to reduce compile time (bytecodealliance#581)
* Add --dev option to reduce compile time close bytecodealliance#579 * refactor * cargo fmt
1 parent 45d7153 commit 585e495

File tree

5 files changed

+32
-10
lines changed

5 files changed

+32
-10
lines changed

crates/cli/src/commands.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ pub struct CompileCommandOpts {
3434
#[structopt(short = "n")]
3535
/// Optional WIT world name for WIT file. Must be specified if WIT is file path is specified.
3636
pub wit_world: Option<String>,
37+
38+
#[structopt(long = "no-source-compression")]
39+
/// Disable source code compression, which reduces compile time at the expense of generating larger WebAssembly files.
40+
pub no_source_compression: bool,
3741
}
3842

3943
#[derive(Debug, StructOpt)]

crates/cli/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ fn main() -> Result<()> {
2929
(Some(wit), Some(world)) => exports::process_exports(&js, wit, world),
3030
}?;
3131
let wasm = if opts.dynamic {
32-
dynamic_generator::generate(&js, exports)?
32+
dynamic_generator::generate(&js, exports, opts.no_source_compression)?
3333
} else {
34-
static_generator::generate(&js, exports)?
34+
static_generator::generate(&js, exports, opts.no_source_compression)?
3535
};
3636
fs::write(&opts.output, wasm)?;
3737
Ok(())

crates/cli/src/wasm_generator/dynamic.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ use walrus::{DataKind, FunctionBuilder, Module, ValType};
6464
// (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]")
6565
// (data (;1;) "foo")
6666
// )
67-
pub fn generate(js: &JS, exported_functions: Vec<Export>) -> Result<Vec<u8>> {
67+
pub fn generate(
68+
js: &JS,
69+
exported_functions: Vec<Export>,
70+
no_source_compression: bool,
71+
) -> Result<Vec<u8>> {
6872
let mut module = Module::with_config(transform::module_config());
6973

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

8892
transform::add_producers_section(&mut module.producers);
89-
module.customs.add(SourceCodeSection::new(js)?);
93+
if no_source_compression {
94+
module.customs.add(SourceCodeSection::uncompressed(js)?);
95+
} else {
96+
module.customs.add(SourceCodeSection::compressed(js)?);
97+
}
9098

9199
let bytecode = js.compile()?;
92100
let bytecode_len: i32 = bytecode.len().try_into()?;

crates/cli/src/wasm_generator/static.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use super::transform::{self, SourceCodeSection};
1414

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

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

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

9999
let mut module = transform::module_config().parse(&wasm)?;
100-
module.customs.add(SourceCodeSection::new(js)?);
100+
if no_source_compression {
101+
module.customs.add(SourceCodeSection::uncompressed(js)?);
102+
} else {
103+
module.customs.add(SourceCodeSection::compressed(js)?);
104+
}
101105
transform::add_producers_section(&mut module.producers);
102106
Ok(module.emit_wasm())
103107
}

crates/cli/src/wasm_generator/transform.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@ use crate::js::JS;
77

88
#[derive(Debug)]
99
pub struct SourceCodeSection {
10-
compressed_source_code: Vec<u8>,
10+
source_code: Vec<u8>,
1111
}
1212

1313
impl SourceCodeSection {
14-
pub fn new(js: &JS) -> Result<SourceCodeSection> {
14+
pub fn compressed(js: &JS) -> Result<SourceCodeSection> {
1515
Ok(SourceCodeSection {
16-
compressed_source_code: js.compress()?,
16+
source_code: js.compress()?,
17+
})
18+
}
19+
20+
pub fn uncompressed(js: &JS) -> Result<SourceCodeSection> {
21+
Ok(SourceCodeSection {
22+
source_code: js.as_bytes().to_vec(),
1723
})
1824
}
1925
}
@@ -24,7 +30,7 @@ impl CustomSection for SourceCodeSection {
2430
}
2531

2632
fn data(&self, _ids_to_indices: &IdsToIndices) -> Cow<[u8]> {
27-
(&self.compressed_source_code).into()
33+
(&self.source_code).into()
2834
}
2935
}
3036

0 commit comments

Comments
 (0)