feat(wasm): C5 release-wasm size profile and build reporting
Add opt-level=z/LTO strip profile, print KiB from marqdo wasm build, optional wasm-opt, and document tree-only browser sessions. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
ab52db2037
commit
04890e0c73
9 files changed
+134
-30
No files matched your search
@@ -8,8 +8,12 @@ use crate::host::{HostCaps, HostContext};
|
||||
use crate::interp::Interpreter;
|
||||
use crate::load::load_module_from_source;
|
||||
use crate::value::Value;
|
||||
use crate::Backend;
|
||||
|
||||
/// Persistent Marqdo program for the browser host bridge.
|
||||
///
|
||||
/// Session calls (`call`) use the **tree** interpreter so entry bindings persist
|
||||
/// across handlers (C3). One-shot [`crate::run_source`] may still use bytecode.
|
||||
pub struct BrowserSession {
|
||||
module: Module,
|
||||
interp: Interpreter,
|
||||
@@ -18,9 +22,17 @@ pub struct BrowserSession {
|
||||
impl BrowserSession {
|
||||
/// Parse source, run `# main`, keep entry bindings for later [`Self::call`].
|
||||
pub fn boot(source: &str) -> Result<(Self, Value)> {
|
||||
Self::boot_with(source, Backend::Tree)
|
||||
}
|
||||
|
||||
/// Like [`Self::boot`]. `Backend::Bytecode` is rejected for sessions (no shared entry env yet).
|
||||
pub fn boot_with(source: &str, backend: Backend) -> Result<(Self, Value)> {
|
||||
if source.trim().is_empty() {
|
||||
bail!("source is empty");
|
||||
}
|
||||
if backend != Backend::Tree {
|
||||
bail!("browser session requires tree backend (bytecode has no entry-env invoke yet)");
|
||||
}
|
||||
let module = load_module_from_source(source)?;
|
||||
let caps = HostCaps {
|
||||
fs_write: false,
|
||||
@@ -129,4 +141,12 @@ import json:lib/json.mq.md
|
||||
let jd = value_as_json(&done).unwrap();
|
||||
assert!(jd["set_text"]["#status"].as_str().unwrap().contains("uuid"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn session_rejects_bytecode_backend() {
|
||||
let err = BrowserSession::boot_with("# main\n\n> print text=x\n", Backend::Bytecode)
|
||||
.err()
|
||||
.expect("err");
|
||||
assert!(err.to_string().contains("tree backend"));
|
||||
}
|
||||
}
|
||||
@@ -369,4 +369,12 @@ mod tests {
|
||||
.expect("run_source");
|
||||
assert_eq!(cap.stdout, "Hello World!\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_source_hello_bytecode() {
|
||||
let mut opts = RunOptions::default();
|
||||
opts.backend = Backend::Bytecode;
|
||||
let cap = run_source("# main\n\n> print text=Hello World!\n", &opts).expect("bc");
|
||||
assert_eq!(cap.stdout, "Hello World!\n");
|
||||
}
|
||||
}
|
||||
+60
-8
@@ -5,7 +5,7 @@ use std::process::Command;
|
||||
|
||||
use anyhow::{bail, Context, Result};
|
||||
|
||||
/// Build release wasm32 artifact and copy `marqdo_wasm.wasm` into `out_dir`.
|
||||
/// Build size-optimized wasm32 artifact and copy `marqdo_wasm.wasm` into `out_dir`.
|
||||
pub fn build_wasm(out_dir: &Path) -> Result<()> {
|
||||
let status = Command::new("cargo")
|
||||
.args([
|
||||
@@ -14,18 +14,24 @@ pub fn build_wasm(out_dir: &Path) -> Result<()> {
|
||||
"marqdo-wasm",
|
||||
"--target",
|
||||
"wasm32-unknown-unknown",
|
||||
"--release",
|
||||
"--profile",
|
||||
"release-wasm",
|
||||
])
|
||||
.status()
|
||||
.context("failed to spawn cargo (is it on PATH?)")?;
|
||||
if !status.success() {
|
||||
bail!("cargo build -p marqdo-wasm failed with {status}");
|
||||
bail!("cargo build -p marqdo-wasm --profile release-wasm failed with {status}");
|
||||
}
|
||||
|
||||
let artifact = find_wasm_artifact()?;
|
||||
let mut artifact = find_wasm_artifact()?;
|
||||
std::fs::create_dir_all(out_dir)
|
||||
.with_context(|| format!("create {}", out_dir.display()))?;
|
||||
let dest = out_dir.join("marqdo_wasm.wasm");
|
||||
|
||||
if let Some(optimized) = try_wasm_opt(&artifact)? {
|
||||
artifact = optimized;
|
||||
}
|
||||
|
||||
std::fs::copy(&artifact, &dest).with_context(|| {
|
||||
format!(
|
||||
"copy {} → {}",
|
||||
@@ -33,22 +39,68 @@ pub fn build_wasm(out_dir: &Path) -> Result<()> {
|
||||
dest.display()
|
||||
)
|
||||
})?;
|
||||
println!("wrote {}", dest.display());
|
||||
let bytes = std::fs::metadata(&dest)
|
||||
.map(|m| m.len())
|
||||
.unwrap_or(0);
|
||||
println!(
|
||||
"wrote {} ({:.1} KiB)",
|
||||
dest.display(),
|
||||
bytes as f64 / 1024.0
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Run `wasm-opt -Oz` when Binaryen is installed; returns path to optimized file.
|
||||
fn try_wasm_opt(input: &Path) -> Result<Option<PathBuf>> {
|
||||
let which = Command::new("wasm-opt").arg("--version").output();
|
||||
let Ok(out) = which else {
|
||||
return Ok(None);
|
||||
};
|
||||
if !out.status.success() {
|
||||
return Ok(None);
|
||||
}
|
||||
let tmp = input.with_extension("opt.wasm");
|
||||
let status = Command::new("wasm-opt")
|
||||
.args(["-Oz", "--enable-bulk-memory"])
|
||||
.arg(input)
|
||||
.arg("-o")
|
||||
.arg(&tmp)
|
||||
.status()
|
||||
.context("wasm-opt spawn failed")?;
|
||||
if !status.success() {
|
||||
// Retry without bulk-memory flag for older binaryen.
|
||||
let status2 = Command::new("wasm-opt")
|
||||
.args(["-Oz"])
|
||||
.arg(input)
|
||||
.arg("-o")
|
||||
.arg(&tmp)
|
||||
.status()
|
||||
.context("wasm-opt spawn failed")?;
|
||||
if !status2.success() {
|
||||
eprintln!("warning: wasm-opt failed; keeping cargo artifact");
|
||||
return Ok(None);
|
||||
}
|
||||
}
|
||||
println!("wasm-opt: {}", tmp.display());
|
||||
Ok(Some(tmp))
|
||||
}
|
||||
|
||||
fn find_wasm_artifact() -> Result<PathBuf> {
|
||||
let mut candidates = Vec::new();
|
||||
if let Ok(td) = std::env::var("CARGO_TARGET_DIR") {
|
||||
candidates.push(
|
||||
PathBuf::from(td)
|
||||
.join("wasm32-unknown-unknown/release/marqdo_wasm.wasm"),
|
||||
PathBuf::from(&td).join("wasm32-unknown-unknown/release-wasm/marqdo_wasm.wasm"),
|
||||
);
|
||||
candidates.push(
|
||||
PathBuf::from(td).join("wasm32-unknown-unknown/release/marqdo_wasm.wasm"),
|
||||
);
|
||||
}
|
||||
candidates.push(PathBuf::from(
|
||||
"target/wasm32-unknown-unknown/release-wasm/marqdo_wasm.wasm",
|
||||
));
|
||||
candidates.push(PathBuf::from(
|
||||
"target/wasm32-unknown-unknown/release/marqdo_wasm.wasm",
|
||||
));
|
||||
// Workspace may place target next to crates when invoked oddly — still try root.
|
||||
for c in &candidates {
|
||||
if c.is_file() {
|
||||
return Ok(c.clone());
|
||||
|
||||
Reference in new issue
Block a user