Skip to content

Commit

Permalink
Wasm Size Optimisation
Browse files Browse the repository at this point in the history
- Introduced a new tool to optimise for size.
- Made console.log messages better.
- Fixed bug where `make debug` was emptying the contents of `scripts.lock`
  • Loading branch information
BlackAsLight committed Nov 24, 2023
1 parent 156943b commit ef7c937
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 11 deletions.
1 change: 1 addition & 0 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ jobs:
- uses: jetli/wasm-bindgen-action@v0.2.0
with:
version: 0.2.88
- run: cargo install wasm-opt@0.116.0
- uses: denoland/setup-deno@v1
- run: make release
- run: make clean
Expand Down
8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
[workspace]
members = ["trade"]
resolver = "2"
members = [
"trade"
]

[profile.release]
lto = true
opt-level = "z"
8 changes: 4 additions & 4 deletions bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ const promises: Promise<void>[] = [
/* Bundle Wasm Scripts
-------------------------*/
...members.map(async member => {
console.log(member)
const startTime = performance.now()
await Deno.writeTextFile(
`./static/wasm/${snakeToCamel(member)}.ts`,
`${(await Deno.readTextFile(`./${member}/prefix.ts`)).replace('<VERSION />', (parse(await Deno.readTextFile(`./${member}/Cargo.toml`)) as Package).package.version)}\n\
import x from './${member}.js'\n\
x(fetch('data:application/wasm;base64,${encodeBase64(await Deno.readFile(`./static/wasm/${member}_bg.wasm`))}'))`
)
await createScript(`./static/wasm/${snakeToCamel(member)}.ts`)
await createScript(`./static/wasm/${snakeToCamel(member)}.ts`, startTime)
})
]

Expand Down Expand Up @@ -75,8 +75,7 @@ async function esbuild(inPath: string, outPath: string) {
warnings.forEach(warning => console.warn(warning))
}

async function createScript(path: string) {
console.log(path)
async function createScript(path: string, startTime = performance.now()) {
const lines: string[] = []
{
let copyLine = false
Expand Down Expand Up @@ -106,6 +105,7 @@ async function createScript(path: string) {
await file.write(await Deno.readFile(`./static/scripts/${name}.min.js`))
file.close()
await Deno.remove(`./static/scripts/${name}.min.js`)
console.log(`(${(performance.now() - startTime).toLocaleString('en-US', { maximumFractionDigits: 2 })}ms):\t${path}`)
}

function snakeToCamel(text: string) {
Expand Down
10 changes: 7 additions & 3 deletions compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,23 @@ for (const member of members)
if (
await command(`cargo +nightly build --bin ${member}${releaseMode ? ' --release ' : ' '}--target wasm32-unknown-unknown`)
&& await command(`wasm-bindgen --out-dir static/wasm/ --out-name ${member} --target web --omit-default-module-path --no-typescript target/wasm32-unknown-unknown/${releaseMode ? 'release' : 'debug'}/${member}.wasm`)
&& await command(`wasm-opt -Oz -o static/wasm/${member}_bg.wasm static/wasm/${member}_bg.wasm`)
)
(console.log(`Success: ${member}`), cleanUp.push(updateScript(member)))
else
console.log(`Fail: ${member}`)

await Promise.allSettled(cleanUp)
await Deno.writeTextFile('./scripts.lock', JSON.stringify(scripts))
if (releaseMode)
await Deno.writeTextFile('./scripts.lock', JSON.stringify(scripts))
console.log(`${performance.now().toLocaleString('en-US', { maximumFractionDigits: 2 })}ms`)

async function command(command: string, env?: Record<string, string>): Promise<boolean> {
console.log(`Command: ${command}`)
const startTime = performance.now()
const args = command.split(' ').filter(x => x)
return (await new Deno.Command(args.shift()!, { env, args }).spawn().status).success
const success = (await new Deno.Command(args.shift()!, { env, args }).spawn().status).success
console.log(`(${(performance.now() - startTime).toLocaleString('en-US', { maximumFractionDigits: 2 })}ms):\t${command}`)
return success
}

async function updateScript(member: string): Promise<void> {
Expand Down
1 change: 0 additions & 1 deletion scripts.lock

This file was deleted.

0 comments on commit ef7c937

Please sign in to comment.