From e616f9feb35da217a6e6d1397370ce432e68efb3 Mon Sep 17 00:00:00 2001 From: Ingvar Stepanyan Date: Wed, 20 Jan 2021 21:50:29 +0000 Subject: [PATCH] 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. --- codecs/hqx/Cargo.toml | 3 + codecs/hqx/pkg/squooshhqx.d.ts | 54 +++---- codecs/hqx/pkg/squooshhqx.js | 136 +++++++++--------- codecs/hqx/pkg/squooshhqx_bg.wasm | Bin 220121 -> 220115 bytes codecs/oxipng/Cargo.toml | 2 +- codecs/oxipng/build.sh | 8 +- .../pkg-parallel/squoosh_oxipng_bg.wasm | Bin 348541 -> 346787 bytes codecs/oxipng/pkg/squoosh_oxipng_bg.wasm | Bin 269114 -> 268810 bytes codecs/png/Cargo.toml | 2 +- codecs/png/pkg/squoosh_png_bg.wasm | Bin 127134 -> 126970 bytes codecs/resize/Cargo.toml | 6 +- codecs/resize/pkg/squoosh_resize_bg.wasm | Bin 37004 -> 36995 bytes codecs/rust.Dockerfile | 9 +- 13 files changed, 106 insertions(+), 114 deletions(-) diff --git a/codecs/hqx/Cargo.toml b/codecs/hqx/Cargo.toml index 742d0751..6109503b 100644 --- a/codecs/hqx/Cargo.toml +++ b/codecs/hqx/Cargo.toml @@ -3,6 +3,9 @@ name = "squooshhqx" version = "0.1.0" authors = ["Surma "] +[package.metadata.wasm-pack.profile.release] +wasm-opt = false + [lib] crate-type = ["cdylib"] diff --git a/codecs/hqx/pkg/squooshhqx.d.ts b/codecs/hqx/pkg/squooshhqx.d.ts index c4c43fa5..eaebddad 100644 --- a/codecs/hqx/pkg/squooshhqx.d.ts +++ b/codecs/hqx/pkg/squooshhqx.d.ts @@ -1,48 +1,30 @@ /* tslint:disable */ /* eslint-disable */ /** - * @param {Uint32Array} input_image - * @param {number} input_width - * @param {number} input_height - * @param {number} factor - * @returns {Uint32Array} - */ -export function resize( - input_image: Uint32Array, - input_width: number, - input_height: number, - factor: number, -): Uint32Array; +* @param {Uint32Array} input_image +* @param {number} input_width +* @param {number} input_height +* @param {number} factor +* @returns {Uint32Array} +*/ +export function resize(input_image: Uint32Array, input_width: number, input_height: number, factor: number): Uint32Array; -export type InitInput = - | RequestInfo - | URL - | Response - | BufferSource - | WebAssembly.Module; +export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module; export interface InitOutput { readonly memory: WebAssembly.Memory; - readonly resize: ( - a: number, - b: number, - c: number, - d: number, - e: number, - f: number, - ) => void; + readonly resize: (a: number, b: number, c: number, d: number, e: number, f: number) => void; readonly __wbindgen_malloc: (a: number) => number; readonly __wbindgen_free: (a: number, b: number) => void; } /** - * If `module_or_path` is {RequestInfo} or {URL}, makes a request and - * for everything else, calls `WebAssembly.instantiate` directly. - * - * @param {InitInput | Promise} module_or_path - * - * @returns {Promise} - */ -export default function init( - module_or_path?: InitInput | Promise, -): Promise; +* If `module_or_path` is {RequestInfo} or {URL}, makes a request and +* for everything else, calls `WebAssembly.instantiate` directly. +* +* @param {InitInput | Promise} module_or_path +* +* @returns {Promise} +*/ +export default function init (module_or_path?: InitInput | Promise): Promise; + \ No newline at end of file diff --git a/codecs/hqx/pkg/squooshhqx.js b/codecs/hqx/pkg/squooshhqx.js index 8fd6c83f..2d2f761b 100644 --- a/codecs/hqx/pkg/squooshhqx.js +++ b/codecs/hqx/pkg/squooshhqx.js @@ -1,107 +1,103 @@ + let wasm; let cachegetUint32Memory0 = null; function getUint32Memory0() { - if ( - cachegetUint32Memory0 === null || - cachegetUint32Memory0.buffer !== wasm.memory.buffer - ) { - cachegetUint32Memory0 = new Uint32Array(wasm.memory.buffer); - } - return cachegetUint32Memory0; + if (cachegetUint32Memory0 === null || cachegetUint32Memory0.buffer !== wasm.memory.buffer) { + cachegetUint32Memory0 = new Uint32Array(wasm.memory.buffer); + } + return cachegetUint32Memory0; } let WASM_VECTOR_LEN = 0; function passArray32ToWasm0(arg, malloc) { - const ptr = malloc(arg.length * 4); - getUint32Memory0().set(arg, ptr / 4); - WASM_VECTOR_LEN = arg.length; - return ptr; + const ptr = malloc(arg.length * 4); + getUint32Memory0().set(arg, ptr / 4); + WASM_VECTOR_LEN = arg.length; + return ptr; } let cachegetInt32Memory0 = null; function getInt32Memory0() { - if ( - cachegetInt32Memory0 === null || - cachegetInt32Memory0.buffer !== wasm.memory.buffer - ) { - cachegetInt32Memory0 = new Int32Array(wasm.memory.buffer); - } - return cachegetInt32Memory0; + if (cachegetInt32Memory0 === null || cachegetInt32Memory0.buffer !== wasm.memory.buffer) { + cachegetInt32Memory0 = new Int32Array(wasm.memory.buffer); + } + return cachegetInt32Memory0; } function getArrayU32FromWasm0(ptr, len) { - return getUint32Memory0().subarray(ptr / 4, ptr / 4 + len); + return getUint32Memory0().subarray(ptr / 4, ptr / 4 + len); } /** - * @param {Uint32Array} input_image - * @param {number} input_width - * @param {number} input_height - * @param {number} factor - * @returns {Uint32Array} - */ +* @param {Uint32Array} input_image +* @param {number} input_width +* @param {number} input_height +* @param {number} factor +* @returns {Uint32Array} +*/ export function resize(input_image, input_width, input_height, factor) { - var ptr0 = passArray32ToWasm0(input_image, wasm.__wbindgen_malloc); - var len0 = WASM_VECTOR_LEN; - wasm.resize(8, ptr0, len0, input_width, input_height, factor); - var r0 = getInt32Memory0()[8 / 4 + 0]; - var r1 = getInt32Memory0()[8 / 4 + 1]; - var v1 = getArrayU32FromWasm0(r0, r1).slice(); - wasm.__wbindgen_free(r0, r1 * 4); - return v1; + var ptr0 = passArray32ToWasm0(input_image, wasm.__wbindgen_malloc); + var len0 = WASM_VECTOR_LEN; + wasm.resize(8, ptr0, len0, input_width, input_height, factor); + var r0 = getInt32Memory0()[8 / 4 + 0]; + var r1 = getInt32Memory0()[8 / 4 + 1]; + var v1 = getArrayU32FromWasm0(r0, r1).slice(); + wasm.__wbindgen_free(r0, r1 * 4); + return v1; } async function load(module, imports) { - if (typeof Response === 'function' && module instanceof Response) { - if (typeof WebAssembly.instantiateStreaming === 'function') { - try { - return await WebAssembly.instantiateStreaming(module, imports); - } catch (e) { - if (module.headers.get('Content-Type') != 'application/wasm') { - 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, - ); - } else { - throw e; + if (typeof Response === 'function' && module instanceof Response) { + + if (typeof WebAssembly.instantiateStreaming === 'function') { + try { + return await WebAssembly.instantiateStreaming(module, imports); + + } catch (e) { + if (module.headers.get('Content-Type') != 'application/wasm') { + 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); + + } else { + throw e; + } + } } - } - } - const bytes = await module.arrayBuffer(); - return await WebAssembly.instantiate(bytes, imports); - } else { - const instance = await WebAssembly.instantiate(module, imports); + const bytes = await module.arrayBuffer(); + return await WebAssembly.instantiate(bytes, imports); - if (instance instanceof WebAssembly.Instance) { - return { instance, module }; } else { - return instance; + + const instance = await WebAssembly.instantiate(module, imports); + + if (instance instanceof WebAssembly.Instance) { + return { instance, module }; + + } else { + return instance; + } } - } } async function init(input) { - if (typeof input === 'undefined') { - input = import.meta.url.replace(/\.js$/, '_bg.wasm'); - } - const imports = {}; + if (typeof input === 'undefined') { + input = import.meta.url.replace(/\.js$/, '_bg.wasm'); + } + const imports = {}; - if ( - typeof input === 'string' || - (typeof Request === 'function' && input instanceof Request) || - (typeof URL === 'function' && input instanceof URL) - ) { - input = fetch(input); - } - const { instance, module } = await load(await input, imports); + if (typeof input === 'string' || (typeof Request === 'function' && input instanceof Request) || (typeof URL === 'function' && input instanceof URL)) { + input = fetch(input); + } - wasm = instance.exports; - init.__wbindgen_wasm_module = module; + const { instance, module } = await load(await input, imports); - return wasm; + wasm = instance.exports; + init.__wbindgen_wasm_module = module; + + return wasm; } export default init; + diff --git a/codecs/hqx/pkg/squooshhqx_bg.wasm b/codecs/hqx/pkg/squooshhqx_bg.wasm index ab28fd7eb345922f5215c28c08696a1888b1d639..182d46dcf51ea92e867b5590e1de1400fb9b410e 100644 GIT binary patch delta 226 zcmcb)o%ix~-VNrAjF+1&7~3rvL6~W~1tW8Y3S;y1g{sVo(~qh$zmv~nP+(ACa%ZVm zU~-fwQ((?fU;xpKIa!Vp3M|>%uc|R0C?eg9JsV zH#;(GGwM%Y@5pSx?zndqP|R@pGe>53F%}jE237{gC98o#jBE^!Ye5YA?aofjB8I#y(7+~vT<{n1b>Fh4dasrN*H-d~wU~s$x NVz_NLbzz<-4gex*M05ZE diff --git a/codecs/oxipng/Cargo.toml b/codecs/oxipng/Cargo.toml index 6ab15d3a..da8a1b8a 100644 --- a/codecs/oxipng/Cargo.toml +++ b/codecs/oxipng/Cargo.toml @@ -6,7 +6,7 @@ edition = "2018" publish = false [package.metadata.wasm-pack.profile.release] -wasm-opt = ["-O", "--no-validation"] +wasm-opt = false [lib] crate-type = ["cdylib"] diff --git a/codecs/oxipng/build.sh b/codecs/oxipng/build.sh index 200481fb..85031098 100755 --- a/codecs/oxipng/build.sh +++ b/codecs/oxipng/build.sh @@ -4,8 +4,12 @@ set -e rm -rf pkg,{-parallel} export CFLAGS="${CFLAGS} -DUNALIGNED_ACCESS_IS_FAST=1" -wasm-pack build -t web -RUSTFLAGS='-C target-feature=+atomics,+bulk-memory' wasm-pack build -t web -d pkg-parallel -- -Z build-std=panic_abort,std --features=parallel +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 --locked # Workaround https://github.com/rustwasm/wasm-bindgen/issues/2133: 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 diff --git a/codecs/oxipng/pkg-parallel/squoosh_oxipng_bg.wasm b/codecs/oxipng/pkg-parallel/squoosh_oxipng_bg.wasm index 49fe70f30c9b8f8d204c63f3af70a09ecccc5aae..13d3c9e788467f418ca4a889f7e2ab3e3a67c34f 100644 GIT binary patch delta 1240 zcmYjQdr*{B6#ve>cVS`ih2RJd_z8{;g0B&faRU3uAbhbkPFa{BC6$1?qQHnF7==hi zqaExP>5Nk;nyBbSFY!g%&NAW71OT4ar<^LgbiXDa?gLudEgO`Z(cSVC)L?qY0LC!4} z*Z1vZc(KJxlppM`VYuwXbg}t_1k=X6FyB}*R`r@<#MLq!v+C3Fe0>hi zI~k=XMq~BK5Ntfzfcd8?(IAsCRl3WB;UT$DIOQwPda-_g7ULQ}MO$MHZfLT4l8ns> zBBi;7;6Tk}yw@^X#GdIR!oS_cph}GS@jS!FmwkofryW$}{hUFu`qnt>>mEvEWKIcok@{UiHCHdpJJdHybT|Tkw8gmRNDM z6tMrd6(a6hE#P4POq^&>$0GYWylaob;OlQ;(e(z58Q6ukf#s;)7>Ip48kUNkk9q^4S-$TFCO%Xpe0UiRl4>*=jDaiC zxR&G__*M%km%dga0^t%rT}D>vZ-;Ipew$%Hjx|t90*!9)=AZ%k-Hi^4fWjA6s_c`;)e23C(Q9mS=!_Zkob3tIWNu3Y9JL z@wNlee`hf-naF0E;7DRXahM@d-ENiv)7kv-aGa;cv)76FC0P^CzBKyO0YHfgD&b3# zm?|wvY(fx}@bYw42I1J5=a9)^s`&EL2>GiS|=cXKS6t8+F)nlkfqvS;M3$+hHd`WFra_7MO8 delta 3064 zcmeHGe@v8R9KY}LJcoC15<&+}U!TwS zc|Py^epX@}`P5jwjOx6J9W1+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>{p=)q>Yh*k}5F=CLMM6YLqq&Nir zhzY@T1~v+UsZ*`#2STinD{h@iqek6|9+PYEiq_}LmUUBTvKRDr1x#EzKj?_TA zSTv7DD(9wov|Q(12>@0-=!x=?Qe&u|TgT82Kd_3s%`{8lelgQ6K9DHJB+)P=>`$Vr o{XxeXGii0j#4)moyapjuepK?~B|jsg>KY!67&VJk1gKKTU%8gXAmyj%W8{RCT}p%*}PO+ka_Y>y`;@@ z`u&VRk=c{|40|WvGjyMB(2l_tAdXl*XC*u*$l-AZqAgjL++omM%MHLP`jl7W*~TW<#%D?PcvrfBk48~4ff zwjiUGCeO5W+5EsZ0A#Pu)jqM#j6FS3Y8A+PwM6RWZg5n>&9WWSs2#*K%{`-+Mlc_orugF{*B#;Kevc zkg;~UZVY27W5D($F^p*fd?qS-^&FHh)gRfJ9>4wYZ%W8{Rm>uN{C*QZz*}OFxFE_dira>le zFrErANW$7=a;2#xSYncC9hep{O9j!JOU)W#ij*cxSZIL-oh&v%1ix77fmJJ6#ewMt zs~j->#!3fFOIQbj=}hbGU|PYZ7fi3UfjIGt4Z?}uwk{yK&Hc6kF#o-WYLcG((!LnX zj&WEHrkNcf7Uer8fHj?W+|2}*IH;w&dArLsFlTeGd$Tc9r`BYl>O`<)arJJnYP}j6 zh-$AoE3iOIT{bw7H@|Nz=Kw2B>jDSZ=EYqc5T&boAWEP1gg^w|H_C60=oeOI+_*Vs zQyn*0*Q>o|U|M0{eXzOz_nnYqJiqzG-ExQ?`v>-5@wx|77#SaI7JkGI7T+xOBcb<%M1R3k5cSkdpGA(!A&KSd( wCc-l1A0ylL>;gt_Hm08S+jo>P_JwmePT6yif%_!i^!ZmACAO!YW1Ol80M!xPqW}N^ diff --git a/codecs/resize/Cargo.toml b/codecs/resize/Cargo.toml index 76d2b153..0c0d10ff 100644 --- a/codecs/resize/Cargo.toml +++ b/codecs/resize/Cargo.toml @@ -4,8 +4,10 @@ version = "0.1.0" authors = ["Surma "] publish = false +[package.metadata.wasm-pack.profile.release] +wasm-opt = false + [lib] -#crate-type = ["cdylib", "rlib"] crate-type = ["cdylib"] [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 # compared to the default allocator's ~10K. It is slower than the default # allocator, however. -# -# Unfortunately, `wee_alloc` requires nightly Rust when targeting wasm for now. wee_alloc = { version = "0.4.2", optional = true } [dev-dependencies] diff --git a/codecs/resize/pkg/squoosh_resize_bg.wasm b/codecs/resize/pkg/squoosh_resize_bg.wasm index 0a10cca382dccd4cf661442e834b3d7256d02aa3..f67556851025f8f641a5a5f5ca379bc5e6420d61 100644 GIT binary patch delta 75 zcmV-R0JQ&%p#p=U0