forked from external-repos/squoosh
Parallel OxiPNG improvements
- Refactor to work around Chromium's issue with postMessage queuing. https://bugs.chromium.org/p/chromium/issues/detail?id=1075645 - Convert codec code to TypeScript. - Make separate parallel and non-parallel builds. - Switch to nightly Rust for OxiPNG to allow parallel builds (but also reuse it for regular builds to avoid installing two toolchains).
This commit is contained in:
committed by
Ingvar Stepanyan
parent
e462875807
commit
9420dba3bc
28
codecs/oxipng/spawn.ts
Normal file
28
codecs/oxipng/spawn.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import initOxiPNG, {
|
||||
worker_initializer,
|
||||
start_main_thread,
|
||||
optimise,
|
||||
} from './pkg-parallel';
|
||||
import wasmUrl from './pkg-parallel/oxipng_bg.wasm';
|
||||
import { WorkerInit } from './worker';
|
||||
|
||||
function initWorker(worker: Worker, workerInit: WorkerInit) {
|
||||
return new Promise((resolve) => {
|
||||
worker.postMessage(workerInit);
|
||||
worker.addEventListener('message', () => resolve(), { once: true });
|
||||
});
|
||||
}
|
||||
|
||||
async function startMainThread() {
|
||||
const num = navigator.hardwareConcurrency;
|
||||
const workers = Array.from({ length: num }, () => new Worker('./worker', { type: 'module' }));
|
||||
await initOxiPNG(fetch(wasmUrl), undefined as any);
|
||||
const workerInit: WorkerInit = worker_initializer();
|
||||
await Promise.all(workers.map(worker => initWorker(worker, workerInit)));
|
||||
start_main_thread(num);
|
||||
return {
|
||||
optimise,
|
||||
};
|
||||
}
|
||||
|
||||
export default startMainThread();
|
||||
Reference in New Issue
Block a user