Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,13 @@ opt-level = 2
# debug = true # good for profilers
panic = "abort" # This leads to better optimizations and smaller binaries (and is the default in Wasm anyways).

[profile.web-release]
panic = 'abort' # Removes panic handling code
inherits = "release"
lto = true
opt-level = 'z' # Optimize for size
codegen-units = 1


## Bench

Expand Down
13 changes: 8 additions & 5 deletions crates/build/re_dev_tools/src/build_web_viewer/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ fn target_directory() -> Utf8PathBuf {

#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Profile {
Release,
WebRelease,
Debug,
}

impl Profile {
pub fn as_str(&self) -> &'static str {
match self {
Self::Release => "release",
Self::WebRelease => "web-release",
Self::Debug => "debug",
}
}
Expand Down Expand Up @@ -126,8 +126,8 @@ pub fn build(
if !features.is_empty() {
cmd.arg(format!("--features={features}"));
}
if profile == Profile::Release {
cmd.arg("--release");
if profile == Profile::WebRelease {
cmd.arg("--profile=web-release");
}

// This is required for unstable WebGPU apis to work
Expand Down Expand Up @@ -200,7 +200,7 @@ pub fn build(
);
}

if profile == Profile::Release {
if profile == Profile::WebRelease {
eprintln!("Optimizing wasm with wasm-opt…");
let start_time = Instant::now();

Expand All @@ -213,9 +213,12 @@ pub fn build(
"--output",
wasm_path.as_str(),
"--enable-reference-types",
"--vacuum",
];
if debug_symbols {
args.push("-g");
} else {
args.push("--strip-debug");
}
cmd.args(args);
eprintln!("{root_dir}> {cmd:?}");
Expand Down
2 changes: 1 addition & 1 deletion crates/build/re_dev_tools/src/build_web_viewer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn default_features() -> String {

pub fn main(args: Args) -> anyhow::Result<()> {
let profile = if args.release && !args.debug {
Profile::Release
Profile::WebRelease
} else if !args.release && args.debug {
Profile::Debug
} else {
Expand Down
2 changes: 1 addition & 1 deletion pixi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ rerun-web-release = { cmd = "cargo run --package rerun-cli --no-default-features
#
# This installs the `wasm32-unknown-unknown` rust target if it's not already installed.
# (this looks heavy but takes typically below 0.1s!)
rerun-build-web-release = "rustup target add wasm32-unknown-unknown && cargo run --quiet -p re_dev_tools -- build-web-viewer --no-default-features --features analytics,map_view --release -g"
rerun-build-web-release = "rustup target add wasm32-unknown-unknown && cargo run --quiet -p re_dev_tools -- build-web-viewer --no-default-features --features analytics,map_view --release"

rs-check = { cmd = "rustup target add wasm32-unknown-unknown && python scripts/ci/rust_checks.py", depends-on = [
"rerun-build-web", # The checks require the web viewer wasm to be around.
Expand Down
36 changes: 28 additions & 8 deletions rerun_js/web-viewer/build-wasm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ function re_viewer_js() {
const end = `wasm_bindgen = Object.assign(__wbg_init, { initSync }, __exports);

})();`;
if (code.indexOf(start) === -1) {
throw new Error("failed to run js build script: failed to patch re_viewer.js, could not find replace start marker");
}
if (code.indexOf(end) === -1) {
throw new Error("failed to run js build script: failed to patch re_viewer.js, could not find replace end marker");
}
code = code.replace(start, "").replace(end, "");

code = `
Expand All @@ -66,10 +72,11 @@ ${code}
function deinit() {
__wbg_init.__wbindgen_wasm_module = null;
wasm = null;
cachedUint8ArrayMemory0 = null;
cachedFloat32ArrayMemory0 = null;
cachedInt32ArrayMemory0 = null;
cachedUint32ArrayMemory0 = null;
cachedUint8ArrayMemory0 = null;
cachedDataViewMemory0 = null;
}

return Object.assign(__wbg_init, { initSync, deinit }, __exports);
Expand All @@ -80,19 +87,32 @@ return Object.assign(__wbg_init, { initSync, deinit }, __exports);
// Otherwise we end up with an exceptioon during closure destruction which prevents the references from all being
// cleaned up properly.
// TODO(jprochazk): Can we force these to run before we null `wasm` instead?
const closure_dtors = `const CLOSURE_DTORS = (typeof FinalizationRegistry === 'undefined')
? { register: () => {}, unregister: () => {} }
: new FinalizationRegistry(state => {
wasm.__wbindgen_export_4.get(state.dtor)(state.a, state.b)
});`;
const closure_dtors_start_marker = "const CLOSURE_DTORS";
const closure_dtors_end_marker = "});";

const closure_dtors_start = code.indexOf(closure_dtors_start_marker);
if (closure_dtors_start === -1) {
throw new Error("failed to run js build script: failed to patch re_viewer.js, could not find CLOSURE_DTORS start");
}
const closure_dtors_end = code.indexOf(closure_dtors_end_marker, closure_dtors_start);
if (closure_dtors_end === -1) {
throw new Error("failed to run js build script: failed to patch re_viewer.js, could not find CLOSURE_DTORS end");
}

let m = code.substring(closure_dtors_start, closure_dtors_end).match(/__wbindgen_export_\d+/);
if (!m) {
throw new Error("failed to run js build script: failed to patch re_viewer.js, could not find __wbindgen_export within CLOSURE_DTORS");
}

let wbindgen_export = m[0];

const closure_dtors_patch = `const CLOSURE_DTORS = (typeof FinalizationRegistry === 'undefined')
? { register: () => {}, unregister: () => {} }
: new FinalizationRegistry(state => {
wasm?.__wbindgen_export_4.get(state.dtor)(state.a, state.b)
wasm?.${wbindgen_export}.get(state.dtor)(state.a, state.b)
});`;

code = code.replace(closure_dtors, closure_dtors_patch);
code = code.substring(0, closure_dtors_start) + closure_dtors_patch + code.slice(closure_dtors_end + closure_dtors_end_marker.length);

fs.writeFileSync(path.join(__dirname, "re_viewer.js"), code);
}
Expand Down
Loading