Compare commits

...

1 Commits

Author SHA1 Message Date
Ingvar Stepanyan
e616f9feb3 Use latest wasm-opt instead of one in wasm-pack
wasm-pack auto-downloads and runs a fixed version of wasm-opt, but wasm-pack itself hasn't been maintained for a while, and so wasm-opt that comes with it is also severely outdated, leading to all sorts of hidden issues (compiler errors, broken atomics code, etc.) as well as missed optimisations.

This change disable wasm-opt feature of wasm-pack in all Rust codecs and runs the latest wasm-opt manually instead.
2021-01-20 21:50:29 +00:00
13 changed files with 106 additions and 114 deletions

View File

@@ -3,6 +3,9 @@ name = "squooshhqx"
version = "0.1.0" version = "0.1.0"
authors = ["Surma <surma@surma.link>"] authors = ["Surma <surma@surma.link>"]
[package.metadata.wasm-pack.profile.release]
wasm-opt = false
[lib] [lib]
crate-type = ["cdylib"] crate-type = ["cdylib"]

28
codecs/hqx/pkg/squooshhqx.d.ts generated vendored
View File

@@ -7,30 +7,13 @@
* @param {number} factor * @param {number} factor
* @returns {Uint32Array} * @returns {Uint32Array}
*/ */
export function resize( export function resize(input_image: Uint32Array, input_width: number, input_height: number, factor: number): Uint32Array;
input_image: Uint32Array,
input_width: number,
input_height: number,
factor: number,
): Uint32Array;
export type InitInput = export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
| RequestInfo
| URL
| Response
| BufferSource
| WebAssembly.Module;
export interface InitOutput { export interface InitOutput {
readonly memory: WebAssembly.Memory; readonly memory: WebAssembly.Memory;
readonly resize: ( readonly resize: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
a: number,
b: number,
c: number,
d: number,
e: number,
f: number,
) => void;
readonly __wbindgen_malloc: (a: number) => number; readonly __wbindgen_malloc: (a: number) => number;
readonly __wbindgen_free: (a: number, b: number) => void; readonly __wbindgen_free: (a: number, b: number) => void;
} }
@@ -43,6 +26,5 @@ export interface InitOutput {
* *
* @returns {Promise<InitOutput>} * @returns {Promise<InitOutput>}
*/ */
export default function init( export default function init (module_or_path?: InitInput | Promise<InitInput>): Promise<InitOutput>;
module_or_path?: InitInput | Promise<InitInput>,
): Promise<InitOutput>;

View File

@@ -1,11 +1,9 @@
let wasm; let wasm;
let cachegetUint32Memory0 = null; let cachegetUint32Memory0 = null;
function getUint32Memory0() { function getUint32Memory0() {
if ( if (cachegetUint32Memory0 === null || cachegetUint32Memory0.buffer !== wasm.memory.buffer) {
cachegetUint32Memory0 === null ||
cachegetUint32Memory0.buffer !== wasm.memory.buffer
) {
cachegetUint32Memory0 = new Uint32Array(wasm.memory.buffer); cachegetUint32Memory0 = new Uint32Array(wasm.memory.buffer);
} }
return cachegetUint32Memory0; return cachegetUint32Memory0;
@@ -22,10 +20,7 @@ function passArray32ToWasm0(arg, malloc) {
let cachegetInt32Memory0 = null; let cachegetInt32Memory0 = null;
function getInt32Memory0() { function getInt32Memory0() {
if ( if (cachegetInt32Memory0 === null || cachegetInt32Memory0.buffer !== wasm.memory.buffer) {
cachegetInt32Memory0 === null ||
cachegetInt32Memory0.buffer !== wasm.memory.buffer
) {
cachegetInt32Memory0 = new Int32Array(wasm.memory.buffer); cachegetInt32Memory0 = new Int32Array(wasm.memory.buffer);
} }
return cachegetInt32Memory0; return cachegetInt32Memory0;
@@ -54,15 +49,15 @@ export function resize(input_image, input_width, input_height, factor) {
async function load(module, imports) { async function load(module, imports) {
if (typeof Response === 'function' && module instanceof Response) { if (typeof Response === 'function' && module instanceof Response) {
if (typeof WebAssembly.instantiateStreaming === 'function') { if (typeof WebAssembly.instantiateStreaming === 'function') {
try { try {
return await WebAssembly.instantiateStreaming(module, imports); return await WebAssembly.instantiateStreaming(module, imports);
} catch (e) { } catch (e) {
if (module.headers.get('Content-Type') != 'application/wasm') { if (module.headers.get('Content-Type') != 'application/wasm') {
console.warn( console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
'`WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n',
e,
);
} else { } else {
throw e; throw e;
} }
@@ -71,11 +66,14 @@ async function load(module, imports) {
const bytes = await module.arrayBuffer(); const bytes = await module.arrayBuffer();
return await WebAssembly.instantiate(bytes, imports); return await WebAssembly.instantiate(bytes, imports);
} else { } else {
const instance = await WebAssembly.instantiate(module, imports); const instance = await WebAssembly.instantiate(module, imports);
if (instance instanceof WebAssembly.Instance) { if (instance instanceof WebAssembly.Instance) {
return { instance, module }; return { instance, module };
} else { } else {
return instance; return instance;
} }
@@ -88,11 +86,8 @@ async function init(input) {
} }
const imports = {}; const imports = {};
if (
typeof input === 'string' || if (typeof input === 'string' || (typeof Request === 'function' && input instanceof Request) || (typeof URL === 'function' && input instanceof URL)) {
(typeof Request === 'function' && input instanceof Request) ||
(typeof URL === 'function' && input instanceof URL)
) {
input = fetch(input); input = fetch(input);
} }
@@ -105,3 +100,4 @@ async function init(input) {
} }
export default init; export default init;

Binary file not shown.

View File

@@ -6,7 +6,7 @@ edition = "2018"
publish = false publish = false
[package.metadata.wasm-pack.profile.release] [package.metadata.wasm-pack.profile.release]
wasm-opt = ["-O", "--no-validation"] wasm-opt = false
[lib] [lib]
crate-type = ["cdylib"] crate-type = ["cdylib"]

View File

@@ -4,8 +4,12 @@ set -e
rm -rf pkg,{-parallel} rm -rf pkg,{-parallel}
export CFLAGS="${CFLAGS} -DUNALIGNED_ACCESS_IS_FAST=1" export CFLAGS="${CFLAGS} -DUNALIGNED_ACCESS_IS_FAST=1"
wasm-pack build -t web wasm-pack build -t web -- --locked
RUSTFLAGS='-C target-feature=+atomics,+bulk-memory' wasm-pack build -t web -d pkg-parallel -- -Z build-std=panic_abort,std --features=parallel RUSTFLAGS='-C target-feature=+atomics,+bulk-memory' wasm-pack build -t web -d pkg-parallel -- -Z build-std=panic_abort,std --features=parallel --locked
# Workaround https://github.com/rustwasm/wasm-bindgen/issues/2133: # Workaround https://github.com/rustwasm/wasm-bindgen/issues/2133:
sed -i "s|maybe_memory:|maybe_memory?:|" pkg-parallel/squoosh_oxipng.d.ts sed -i "s|maybe_memory:|maybe_memory?:|" pkg-parallel/squoosh_oxipng.d.ts
# Workaround wasm-pack using a very old wasm-opt.
echo "Optimizing wasm binaries with \`wasm-opt\`..."
wasm-opt -O pkg/squoosh_oxipng_bg.wasm -o pkg/squoosh_oxipng_bg.wasm
wasm-opt -O pkg-parallel/squoosh_oxipng_bg.wasm -o pkg-parallel/squoosh_oxipng_bg.wasm
rm pkg{,-parallel}/.gitignore rm pkg{,-parallel}/.gitignore

View File

@@ -6,7 +6,7 @@ edition = "2018"
publish = false publish = false
[package.metadata.wasm-pack.profile.release] [package.metadata.wasm-pack.profile.release]
wasm-opt = ["-O", "--no-validation"] wasm-opt = false
[lib] [lib]
crate-type = ["cdylib"] crate-type = ["cdylib"]

Binary file not shown.

View File

@@ -4,8 +4,10 @@ version = "0.1.0"
authors = ["Surma <surma@surma.link>"] authors = ["Surma <surma@surma.link>"]
publish = false publish = false
[package.metadata.wasm-pack.profile.release]
wasm-opt = false
[lib] [lib]
#crate-type = ["cdylib", "rlib"]
crate-type = ["cdylib"] crate-type = ["cdylib"]
[features] [features]
@@ -25,8 +27,6 @@ console_error_panic_hook = { version = "0.1.1", optional = true }
# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size # `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size
# compared to the default allocator's ~10K. It is slower than the default # compared to the default allocator's ~10K. It is slower than the default
# allocator, however. # allocator, however.
#
# Unfortunately, `wee_alloc` requires nightly Rust when targeting wasm for now.
wee_alloc = { version = "0.4.2", optional = true } wee_alloc = { version = "0.4.2", optional = true }
[dev-dependencies] [dev-dependencies]

View File

@@ -16,4 +16,11 @@ COPY --from=wasm-tools /opt/wasm-tools/wasm-pack /usr/local/cargo/bin/
ENV CPATH="/wasm32/include" ENV CPATH="/wasm32/include"
WORKDIR /src WORKDIR /src
CMD ["sh", "-c", "rm -rf pkg && wasm-pack build --target web -- --verbose --locked && rm pkg/.gitignore"] CMD ["sh", "-c", "\
rm -rf pkg && \
wasm-pack build --target web -- --locked && \
echo 'Optimising binaries...' && \
wasm-opt -O --enable-mutable-globals pkg/*.wasm -o pkg/*.wasm && \
rm pkg/.gitignore && \
echo Done \
"]