Skip to content

Commit

Permalink
js: Update wasmpack to a non-Base64 palette
Browse files Browse the repository at this point in the history
We were just using Base64 for convenience, however there's a slightly
better palette we can use:

- By putting lowercase letters in front, we use many more lowercase
  letters - since our actual code is using lowercase letters as well,
  this results in better compression ratio
- By using :; instead of +/ for the last two characters, we simplify the
  decoding routine since we now only use three ASCII blocks (a-z, A-Z, 0-9:;)

This reduces the decoder post-gzip by ~60 extra bytes (~1%)
  • Loading branch information
zeux committed Aug 12, 2022
1 parent 46708ca commit 733760e
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 31 deletions.
20 changes: 10 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -121,24 +121,24 @@ build/decoder_simd.wasm: $(WASM_DECODER_SOURCES)
@mkdir -p build
$(WASMCC) $^ $(WASM_FLAGS) $(patsubst %,$(WASM_EXPORT_PREFIX)=%,$(WASM_DECODER_EXPORTS)) -o $@ -msimd128 -mbulk-memory

js/meshopt_decoder.js: build/decoder_base.wasm build/decoder_simd.wasm
sed -i "s#Built with clang.*#Built with $$($(WASMCC) --version | head -n 1 | sed 's/\s\+(.*//')#" $@
sed -i "s#\(var wasm_base = \)\".*\";#\\1\"$$(cat build/decoder_base.wasm | python3 tools/wasmpack.py)\";#" $@
sed -i "s#\(var wasm_simd = \)\".*\";#\\1\"$$(cat build/decoder_simd.wasm | python3 tools/wasmpack.py)\";#" $@

build/encoder.wasm: $(WASM_ENCODER_SOURCES)
@mkdir -p build
$(WASMCC) $^ $(WASM_FLAGS) $(patsubst %,$(WASM_EXPORT_PREFIX)=%,$(WASM_ENCODER_EXPORTS)) -lc -o $@

js/meshopt_encoder.js: build/encoder.wasm
sed -i "s#Built with clang.*#Built with $$($(WASMCC) --version | head -n 1 | sed 's/\s\+(.*//')#" $@
sed -i "s#\(var wasm = \)\".*\";#\\1\"$$(cat build/encoder.wasm | python3 tools/wasmpack.py)\";#" $@

build/simplifier.wasm: $(WASM_SIMPLIFIER_SOURCES)
@mkdir -p build
$(WASMCC) $^ $(WASM_FLAGS) $(patsubst %,$(WASM_EXPORT_PREFIX)=%,$(WASM_SIMPLIFIER_EXPORTS)) -lc -o $@

js/meshopt_simplifier.js: build/simplifier.wasm
js/meshopt_decoder.js: build/decoder_base.wasm build/decoder_simd.wasm tools/wasmpack.py
sed -i "s#Built with clang.*#Built with $$($(WASMCC) --version | head -n 1 | sed 's/\s\+(.*//')#" $@
sed -i "s#\(var wasm_base = \)\".*\";#\\1\"$$(cat build/decoder_base.wasm | python3 tools/wasmpack.py)\";#" $@
sed -i "s#\(var wasm_simd = \)\".*\";#\\1\"$$(cat build/decoder_simd.wasm | python3 tools/wasmpack.py)\";#" $@

js/meshopt_encoder.js: build/encoder.wasm tools/wasmpack.py
sed -i "s#Built with clang.*#Built with $$($(WASMCC) --version | head -n 1 | sed 's/\s\+(.*//')#" $@
sed -i "s#\(var wasm = \)\".*\";#\\1\"$$(cat build/encoder.wasm | python3 tools/wasmpack.py)\";#" $@

js/meshopt_simplifier.js: build/simplifier.wasm tools/wasmpack.py
sed -i "s#Built with clang.*#Built with $$($(WASMCC) --version | head -n 1 | sed 's/\s\+(.*//')#" $@
sed -i "s#\(var wasm = \)\".*\";#\\1\"$$(cat build/simplifier.wasm | python3 tools/wasmpack.py)\";#" $@

Expand Down
6 changes: 3 additions & 3 deletions js/meshopt_decoder.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions js/meshopt_decoder.module.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/meshopt_encoder.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/meshopt_encoder.module.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions js/meshopt_simplifier.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions js/meshopt_simplifier.module.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions tools/wasmpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
# regenerate with wasmpack.py generate
table = [32, 0, 65, 2, 1, 106, 34, 33, 3, 128, 11, 4, 13, 64, 6, 253, 10, 7, 15, 116, 127, 5, 8, 12, 40, 16, 19, 54, 20, 9, 27, 255, 113, 17, 42, 67, 24, 23, 146, 148, 18, 14, 22, 45, 70, 69, 56, 114, 101, 21, 25, 63, 75, 136, 108, 28, 118, 29, 73, 115]

base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
palette = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:;";

def encode(buffer):
result = ''

for ch in buffer.read():
if ch in table:
index = table.index(ch)
result += base64[index]
result += palette[index]
else:
result += base64[60 + ch // 64]
result += base64[ch % 64]
result += palette[60 + ch // 64]
result += palette[ch % 64]

return result

Expand Down

0 comments on commit 733760e

Please sign in to comment.