zstddec-wasm icon indicating copy to clipboard operation
zstddec-wasm copied to clipboard

Compile Issues / Guide

Open wiedehopf opened this issue 2 years ago • 1 comments

Hello, i wanted to reproduce compilation and ran into 2 issues.

First of all zstd dev branch seems to no longer work with this code, i don't know exactly why. I'm just using zstd tag 1.5.0. (tags 1.4.8/1.4.9 work as well) 1.5.1 and later gives the following error when loading the js:

Uncaught (in promise) TypeError: WebAssembly.instantiate(): Import #1 module="wasi_snapshot_preview1" error: module is not an object or function

This isn't important as the decompression code shouldn't need the most recent version.

If you get Uncaught RuntimeError: memory access out of bounds, then try compiling without -s MALLOC=emmalloc. The filesize goes a bit up but i have no clue how to fix this. This is necessary for me both with emcc 3.1.15 and emcc 1.40.1 (tried that one due to it being from around the time this github project was started).

Used the emscriptend docker image to avoid installing that stuff: https://hub.docker.com/r/emscripten/emsdk

In newer emcc versions --no-entry needs to be added to the emcc options, but that's pretty straight forward.

This should at least give you a working start point for compiling this.

command line used:

wget https://github.com/facebook/zstd/releases/download/v1.5.0/zstd-1.5.0.tar.gz
tar xf zstd-1.5.0.tar.gz
cd zstd-1.5.0/build/single_file_libs
./combine.sh -r ../../lib -o zstddeclib.c zstddeclib-in.c
docker pull emscripten/emsdk:3.1.15
docker run --rm -v $(pwd):/src -u $(id -u):$(id -g) emscripten/emsdk:3.1.15 emcc zstddeclib.c --no-entry -O3 -s EXPORTED_FUNCTIONS="['_ZSTD_decompress', '_ZSTD_findDecompressedSize', '_ZSTD_isError', '_malloc', '_free']" -s ALLOW_MEMORY_GROWTH=1 -o zstddec.wasm

Depending on your application, consider testing -O3 vs -Oz (O3 optimizes for speed, Oz optimizes for binary size). Now for the 4016 KB json, compressed to 712KB, the decompression time on my 11th gen Intel laptop processor in Google Chrome under Linux was 20ms vs 28ms decompression time with wasm binary size 48KB vs 35KB. This is both very quick, so it will probably not matter to most people, but someone might find it useful.

wiedehopf avatar Jul 07 '22 07:07 wiedehopf

Thank you! Great to know about the emscripten docker container, I wasn't aware of that. I'll leave a few more comments in #16 and #3.

donmccurdy avatar Jul 29 '22 01:07 donmccurdy