Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recompile Emscripten binaries, implement WASM with fallback, and add instructions and scripts #740

Merged
merged 34 commits into from
Aug 22, 2021
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
d4710c2
Add instructions for compiling on Windows 10
Jaifroid Jul 20, 2021
8791575
Update readme.md
Jaifroid Jul 20, 2021
899d03d
Update Compile-Zstddec.ps1
Jaifroid Jul 20, 2021
a29c096
Add bash script to compile with emscripten docker image
mossroy Jul 20, 2021
4303c8a
Remove unnecessary interactive flag
Jaifroid Jul 20, 2021
7c85c89
Run the container with current uid/gid
mossroy Jul 21, 2021
908a095
Add required polyfill for IE11
Jaifroid Jul 21, 2021
581e999
Fix version of emsdk in PS compile script
Jaifroid Jul 21, 2021
9baa565
Add new compiled binaries
Jaifroid Jul 21, 2021
8503faa
Add conditional loading of wasm/asm modules
Jaifroid Jul 21, 2021
1957bc0
Add xzdec docker compile scripts for Windows
Jaifroid Jul 21, 2021
5778c0d
Cleanup old zstandard js file
mossroy Jul 21, 2021
c8be5bb
Add xzdec bash compile script and corresponding output
mossroy Jul 21, 2021
7df545e
Cleanup of old xzdec files
mossroy Jul 21, 2021
a96790c
Modularize xzdec and load asm/wasm dynamically
Jaifroid Jul 21, 2021
4af5f81
Recompile binaries
Jaifroid Jul 21, 2021
121763b
Copy binaries to www/js/lib
Jaifroid Jul 21, 2021
c31df07
Clean up
Jaifroid Jul 21, 2021
0e07e7b
Update implementation
Jaifroid Jul 22, 2021
34e96a2
Try reloading to asm if wasm compile failed
Jaifroid Jul 22, 2021
6fa2655
Fall back to ASM if WASM error
Jaifroid Jul 22, 2021
51c752b
Clean up after rebase
Jaifroid Jul 22, 2021
5e3ba09
Remove localStorage and extend fallback to zstddec
Jaifroid Jul 22, 2021
76e0158
Fix typos
Jaifroid Jul 22, 2021
9d9ea49
Add source of startsWith polyfill
Jaifroid Jul 24, 2021
b5b5c0a
Load ASM fallback whenever WASM fails
Jaifroid Jul 24, 2021
aaa0e20
Add API status panel entries
Jaifroid Jul 31, 2021
0665c0a
Add missing define function
Jaifroid Jul 31, 2021
9e7c1e9
Incorrect header comment
Jaifroid Jul 31, 2021
1f6c841
Corrections from self-review
Jaifroid Aug 21, 2021
27534ee
Set API colour to orange if suboptimal (ASM)
Jaifroid Aug 21, 2021
ca6d7b7
Simplify logic
Jaifroid Aug 21, 2021
c2d01ef
Remove test code
Jaifroid Aug 21, 2021
65588ba
Test apiName before adding decompressorLastUsed
Jaifroid Aug 22, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@
*.woff binary
*.woff2 binary
*.deb binary
*.wasm binary
6 changes: 5 additions & 1 deletion emscripten/xzdec/compile.sh
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
emcc --memory-init-file 0 -O3 -s WASM=0 -s MALLOC="emmalloc" -s TOTAL_MEMORY=83886080 -s NO_FILESYSTEM=1 -s AGGRESSIVE_VARIABLE_ELIMINATION=1 -s DOUBLE_MODE=0 -s PRECISE_I64_MATH=0 -s NO_DYNAMIC_EXECUTION=1 -s LEGACY_VM_SUPPORT=1 -s EXPORTED_FUNCTIONS="['_init', '_init_decompression', '_input_empty', '_get_in_buffer', '_set_new_input', '_decompress', '_get_out_pos', '_get_out_buffer', '_out_buffer_cleared', '_release']" -DXZ_USE_CRC64=1 -DXZ_INTERNAL_CRC64=1 *.c -o xzdec.js
echo "Compiling ASM version xzdec-asm.js"
emcc --memory-init-file 0 -O3 --closure 1 -s ENVIRONMENT="web" -s WASM=0 -s MALLOC="emmalloc" -s TOTAL_MEMORY=83886080 -s FILESYSTEM=0 -s DOUBLE_MODE=0 -s DYNAMIC_EXECUTION=0 -s MIN_IE_VERSION=11 -s EXPORT_NAME="XZ" -s MODULARIZE=1 -s AGGRESSIVE_VARIABLE_ELIMINATION=1 -s EXPORTED_FUNCTIONS="['_init', '_init_decompression', '_input_empty', '_get_in_buffer', '_set_new_input', '_decompress', '_get_out_pos', '_get_out_buffer', '_out_buffer_cleared', '_release']" -DXZ_USE_CRC64=1 -DXZ_INTERNAL_CRC64=1 ./*.c -o xzdec-asm.js
echo "Compiling WASM version xzdec-wasm.js"
emcc --memory-init-file 0 -O3 --closure 1 -s ENVIRONMENT="web" -s WASM=1 -s MALLOC="emmalloc" -s TOTAL_MEMORY=83886080 -s FILESYSTEM=0 -s DOUBLE_MODE=0 -s DYNAMIC_EXECUTION=0 -s EXPORT_NAME="XZ" -s MODULARIZE=1 -s AGGRESSIVE_VARIABLE_ELIMINATION=1 -s EXPORTED_FUNCTIONS="['_init', '_init_decompression', '_input_empty', '_get_in_buffer', '_set_new_input', '_decompress', '_get_out_pos', '_get_out_buffer', '_out_buffer_cleared', '_release']" -DXZ_USE_CRC64=1 -DXZ_INTERNAL_CRC64=1 ./*.c -o xzdec-wasm.js
echo "Finished."
54 changes: 54 additions & 0 deletions emscripten/xzdec/xzdec-asm.js

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions emscripten/xzdec/xzdec-wasm.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added emscripten/xzdec/xzdec-wasm.wasm
Binary file not shown.
16 changes: 0 additions & 16 deletions emscripten/xzdec/xzdec.js

This file was deleted.

Binary file removed emscripten/xzdec/xzdec.js.mem
Binary file not shown.
12 changes: 0 additions & 12 deletions emscripten/xzdec/xzdec_wrapper.js

This file was deleted.

6 changes: 5 additions & 1 deletion emscripten/zstandard/compile.sh
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
emcc --memory-init-file 0 -O3 --closure 1 -s ENVIRONMENT="web" -s WASM=0 -s MALLOC="emmalloc" -s TOTAL_MEMORY=140247040 -s FILESYSTEM=0 -s DOUBLE_MODE=0 -s DYNAMIC_EXECUTION=0 -s MIN_IE_VERSION=11 -s EXPORT_NAME="ZD" -s MODULARIZE=1 -s EXPORTED_FUNCTIONS="['_malloc', '_free', '_ZSTD_createDStream', '_ZSTD_initDStream', '_ZSTD_decompressStream', '_ZSTD_isError', '_ZSTD_getErrorName', '_ZSTD_freeDStream', '_ZSTD_DStreamInSize', '_ZSTD_DStreamOutSize']" -s EXPORTED_RUNTIME_METHODS="['cwrap']" *.c -o zstddec.js
echo "Compiling ASM version zstddec-asm.js"
emcc --memory-init-file 0 -O3 --closure 1 -s ENVIRONMENT="web" -s WASM=0 -s MALLOC="emmalloc" -s TOTAL_MEMORY=150994944 -s FILESYSTEM=0 -s DOUBLE_MODE=0 -s DYNAMIC_EXECUTION=0 -s MIN_IE_VERSION=11 -s EXPORT_NAME="ZD" -s MODULARIZE=1 -s EXPORTED_FUNCTIONS="['_malloc', '_free', '_ZSTD_createDStream', '_ZSTD_initDStream', '_ZSTD_decompressStream', '_ZSTD_isError', '_ZSTD_getErrorName', '_ZSTD_freeDStream', '_ZSTD_DStreamInSize', '_ZSTD_DStreamOutSize']" -s EXPORTED_RUNTIME_METHODS="['cwrap']" ./*.c -o zstddec-asm.js
echo "Compiling WASM version zstddec-wasm.js"
emcc --memory-init-file 0 -O3 --closure 1 -s ENVIRONMENT="web" -s WASM=1 -s MALLOC="emmalloc" -s TOTAL_MEMORY=150994944 -s FILESYSTEM=0 -s DOUBLE_MODE=0 -s DYNAMIC_EXECUTION=0 -s EXPORT_NAME="ZD" -s MODULARIZE=1 -s EXPORTED_FUNCTIONS="['_malloc', '_free', '_ZSTD_createDStream', '_ZSTD_initDStream', '_ZSTD_decompressStream', '_ZSTD_isError', '_ZSTD_getErrorName', '_ZSTD_freeDStream', '_ZSTD_DStreamInSize', '_ZSTD_DStreamOutSize']" -s EXPORTED_RUNTIME_METHODS="['cwrap']" ./*.c -o zstddec-wasm.js
echo "Finished."
2 changes: 2 additions & 0 deletions emscripten/zstandard/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ It has been combined by following the instructions at https://github.com/faceboo

The source code can be compiled with Emscripten by setting up the Emscripten SDK and running the Linux script `./compile.sh` in this directory. This will produce `zstddec.js`. Further optimization and minification of this file may be possible by adjusting the commandline options in `compile.sh`.

You may be able to compile easily using docker. Look in the `/scripts` directory for helper scripts for your platform.

For a fuller Emscripten distribution of the full zstandard library for `node.js`, see https://github.com/yoshihitoh/zstd-codec/tree/develop/js/example.
58 changes: 58 additions & 0 deletions emscripten/zstandard/zstddec-asm.js

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions emscripten/zstandard/zstddec-wasm.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added emscripten/zstandard/zstddec-wasm.wasm
Binary file not shown.
66 changes: 0 additions & 66 deletions emscripten/zstandard/zstddec_optimized.js

This file was deleted.

14 changes: 14 additions & 0 deletions scripts/Compile-Xzdec.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# This PowerShell script (principally for Windows 10/11) compiles the legacy xzdec.js using docker according to the values set in
# /emscripten/xzdec/compile.sh . Please be sure to review the commandline in compile.sh before running this script, to
# be sure it's doing what you want. If you want to compile with a later version of emnsdk, specify it below after 'emsdk:'.
#
# Prerequisites:
#
# * Ensure WSL 2 is enabled in Windows (WSL = Windows Subsystem for Linux)
# * Install docker desktop on Windows: choose the WSL 2 docker backend (not the legacy Hyper-V one)
# * Open docker and ensure the docker daemon is running correctly (icon should be green)
# * Ensure docker desktop is signed in to your dockerhub account (at least the first time you run this script)
# * Run this script by opening a PowerShell terminal, cd to the kiwix-js repo and run './scripts/Compile-Zstddec.ps1'

$repo_dir = ($PSScriptRoot -replace '[\\/]scripts[\\/]*$', '')
docker container run -v $repo_dir\:/project -w /project emscripten/emsdk:2.0.25 /bin/sh -c 'cd emscripten/xzdec/; ./compile.sh'
14 changes: 14 additions & 0 deletions scripts/Compile-Zstddec.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# This PowerShell script (principally for Windows 10/11) compiles zstddec.js using docker according to the values set in
# /emscripten/ztandard/compile.sh . Please be sure to review the commandline in compile.sh before running this script, to
# be sure it's doing what you want. If you want to compile with a later version of emnsdk, specify it below after 'emsdk:'.
#
# Prerequisites:
#
# * Ensure WSL 2 is enabled in Windows (WSL = Windows Subsystem for Linux)
# * Install docker desktop on Windows: choose the WSL 2 docker backend (not the legacy Hyper-V one)
# * Open docker and ensure the docker daemon is running correctly (icon should be green)
# * Ensure docker desktop is signed in to your dockerhub account (at least the first time you run this script)
# * Run this script by opening a PowerShell terminal, cd to the kiwix-js repo and run './scripts/Compile-Zstddec.ps1'
Jaifroid marked this conversation as resolved.
Show resolved Hide resolved

$repo_dir = ($PSScriptRoot -replace '[\\/]scripts[\\/]*$', '')
docker container run -v $repo_dir\:/project -w /project emscripten/emsdk:2.0.25 /bin/sh -c 'cd emscripten/zstandard/; ./compile.sh'
17 changes: 17 additions & 0 deletions scripts/compile_xzdec_with_docker_emscripten.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

# This bash script compiles xzdec.js using emscripten docker image, according to the values set in
# /emscripten/xzdec/compile.sh . Please be sure to review the commandline in compile.sh before running this script, to
# be sure it's doing what you want.
#
# Prerequisites:
#
# * Ensure Docker is installed and its daemon is running
# * If the current user is not in the docker group, you might need to run this script with sudo. See https://docs.docker.com/engine/install/linux-postinstall/#manage-docker-as-a-non-root-user

# Find the repo dir (it's the parent of the dir that contains this script)
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
REPO_DIR="$(dirname "$SCRIPT_DIR")"

# Run the emscripten compilation through docker
docker run -v "$REPO_DIR":/project -w /project -u $(id -u):$(id -g) emscripten/emsdk:2.0.25 /bin/sh -c 'cd emscripten/xzdec/; sh ./compile.sh'
Loading