make initial memory smaller and let it grow

i also enabled lto building which should make performance better.
This commit is contained in:
Fries 2023-08-14 22:18:30 -07:00
parent f7bbb92d85
commit 986912c2aa
3 changed files with 14 additions and 8 deletions

View file

@ -1,6 +1,6 @@
LIBRARY=src/blurhash-decode.wasm LIBRARY=src/blurhash-decode.wasm
$(LIBRARY): blurhash/decode.c $(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 .PHONY: clean
clean: clean:

View file

@ -1,7 +1,7 @@
{ {
"type": "module", "type": "module",
"name": "@fries/blurhash-c-wasm", "name": "@fries/blurhash-c-wasm",
"version": "0.1.0", "version": "0.1.1",
"description": "A WASM module using the Blurhash C library", "description": "A WASM module using the Blurhash C library",
"main": "dist/blurhash-c-wasm.cjs", "main": "dist/blurhash-c-wasm.cjs",
"module": "dist/blurhash-c-wasm.js", "module": "dist/blurhash-c-wasm.js",

View file

@ -46,7 +46,13 @@ function encodeStringToWasmArray(string: string): number {
*/ */
export async function initWasm() { export async function initWasm() {
wasmInstance = await WebAssembly.instantiate( 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; instance = wasmInstance.exports as InstanceType;
} }
@ -77,11 +83,6 @@ export function decode(
const pixelsPtrSize = width * 4 * width; const pixelsPtrSize = width * 4 * width;
const pixelsPtr = instance.malloc(pixelsPtrSize); const pixelsPtr = instance.malloc(pixelsPtrSize);
const pixelsBuf = new Uint8ClampedArray(
instance.memory.buffer,
pixelsPtr,
pixelsPtrSize
);
const stringPtr = encodeStringToWasmArray(blurhashString); const stringPtr = encodeStringToWasmArray(blurhashString);
@ -99,6 +100,11 @@ export function decode(
throw Error("Decoding the Blurhash string has failed."); throw Error("Decoding the Blurhash string has failed.");
} }
const pixelsBuf = new Uint8ClampedArray(
instance.memory.buffer,
pixelsPtr,
pixelsPtrSize
);
const clonedBuffer = pixelsBuf.slice(0); const clonedBuffer = pixelsBuf.slice(0);
instance.free(stringPtr); instance.free(stringPtr);