From 986912c2aa575c0d5206639c969a2d933b77861e Mon Sep 17 00:00:00 2001 From: Fries Date: Mon, 14 Aug 2023 22:18:30 -0700 Subject: [PATCH] make initial memory smaller and let it grow i also enabled lto building which should make performance better. --- Makefile | 2 +- package.json | 2 +- src/library.ts | 18 ++++++++++++------ 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 6913dc0..d540f4e 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ LIBRARY=src/blurhash-decode.wasm $(LIBRARY): blurhash/decode.c - emcc -ffast-math -O3 --no-entry -Wl,--export,decodeToArray -Wl,--export,malloc -Wl,--export,free -Wl,--export,isValidBlurhash -o $@ blurhash/decode.c + emcc -flto -ffast-math -O3 --no-entry -Wl,--export,decodeToArray -Wl,--export,malloc -Wl,--export,free -Wl,--export,isValidBlurhash -s ALLOW_MEMORY_GROWTH -s INITIAL_MEMORY=128KB -o $@ blurhash/decode.c .PHONY: clean clean: diff --git a/package.json b/package.json index 088de38..6c5813c 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "type": "module", "name": "@fries/blurhash-c-wasm", - "version": "0.1.0", + "version": "0.1.1", "description": "A WASM module using the Blurhash C library", "main": "dist/blurhash-c-wasm.cjs", "module": "dist/blurhash-c-wasm.js", diff --git a/src/library.ts b/src/library.ts index f66f894..52e1b4b 100644 --- a/src/library.ts +++ b/src/library.ts @@ -46,7 +46,13 @@ function encodeStringToWasmArray(string: string): number { */ export async function initWasm() { wasmInstance = await WebAssembly.instantiate( - (await wasm()) as WebAssembly.Module + (await wasm()) as WebAssembly.Module, + { + env: { + // stub for a emscripten import + emscripten_notify_memory_growth: () => {}, + }, + } ); instance = wasmInstance.exports as InstanceType; } @@ -77,11 +83,6 @@ export function decode( const pixelsPtrSize = width * 4 * width; const pixelsPtr = instance.malloc(pixelsPtrSize); - const pixelsBuf = new Uint8ClampedArray( - instance.memory.buffer, - pixelsPtr, - pixelsPtrSize - ); const stringPtr = encodeStringToWasmArray(blurhashString); @@ -99,6 +100,11 @@ export function decode( throw Error("Decoding the Blurhash string has failed."); } + const pixelsBuf = new Uint8ClampedArray( + instance.memory.buffer, + pixelsPtr, + pixelsPtrSize + ); const clonedBuffer = pixelsBuf.slice(0); instance.free(stringPtr);