From 41838d8adc79c06a3930dcb4f157e7a30c14f8af Mon Sep 17 00:00:00 2001 From: CountBleck Date: Wed, 28 May 2025 10:38:33 -0700 Subject: [PATCH 1/3] Shave ~20 bytes from math.js file This is just a small micro-optimization using converge and inline.always to remove a few bytes off of the final .wasm size. Sadly, we can't use "shrinkLevel": 3 since that gives imprecise results. --- asconfig.json | 1 + assembly/index.ts | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/asconfig.json b/asconfig.json index 00cca64..877b9f3 100644 --- a/asconfig.json +++ b/asconfig.json @@ -6,6 +6,7 @@ "debug": false, "noExportMemory": true, "optimize": true, + "converge": true, "noAssert": true } } diff --git a/assembly/index.ts b/assembly/index.ts index db8507f..4e23846 100644 --- a/assembly/index.ts +++ b/assembly/index.ts @@ -1,3 +1,3 @@ export function f64_pow(x: f64, y: f64): f64 { - return Math.pow(x, y); + return inline.always(Math.pow(x, y)); } From 227edf490f0934cba45b13ba70870db0df76b61c Mon Sep 17 00:00:00 2001 From: CountBleck Date: Wed, 28 May 2025 10:40:49 -0700 Subject: [PATCH 2/3] Fix the message for the 10^-5 test This confused me for a couple minutes when testing out "shrinkLevel": 3. --- test/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/index.js b/test/index.js index 084f564..1d940b7 100644 --- a/test/index.js +++ b/test/index.js @@ -2,4 +2,4 @@ import { f64_pow } from "../index.js"; import assert from "assert"; assert(f64_pow(2, 2) === 4, "2^2 == 4"); -assert(f64_pow(10, -5) === 0.00001, "2^2 == 4"); +assert(f64_pow(10, -5) === 0.00001, "10^-5 == 0.00001"); From dcb6261927fb3f7a83c998a82c9d99c06482d3fb Mon Sep 17 00:00:00 2001 From: CountBleck Date: Wed, 28 May 2025 10:49:02 -0700 Subject: [PATCH 3/3] Use `fetch` and `WebAssembly.instantiateStreaming`` to support browsers Browsers don't have Node.js's Buffer, nor do they have any reasonable way to decode base64. Luckily, `WebAssembly.instantiateStreaming` allows using the results of `fetch`, which in turn can accept base64 data URIs. --- index.js | 6 +----- scripts/postbuild.js | 2 +- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 3079171..11eaaff 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,3 @@ import { MathWasmBase64 } from "./build/math.js"; -const exports = (await WebAssembly.instantiate(Buffer.from(MathWasmBase64, "base64"))).instance.exports; - -export function f64_pow(value, exponent) { - return exports.f64_pow(value, exponent); -} +export const {f64_pow} = (await WebAssembly.instantiateStreaming(fetch(MathWasmBase64))).instance.exports; diff --git a/scripts/postbuild.js b/scripts/postbuild.js index 9cea09d..ff967a7 100644 --- a/scripts/postbuild.js +++ b/scripts/postbuild.js @@ -2,4 +2,4 @@ import { readFileSync, writeFileSync } from "fs"; const base64String = readFileSync("build/math.wasm", "base64"); -writeFileSync("build/math.js", `export const MathWasmBase64 = "${base64String}";`); +writeFileSync("build/math.js", `export const MathWasmBase64 = "data:application/wasm;base64,${base64String}";`);