From 750872aca60afc84453f23b96abbf71bb9656e30 Mon Sep 17 00:00:00 2001 From: Ingvar Stepanyan Date: Thu, 19 Nov 2020 17:28:03 +0000 Subject: [PATCH] Delete old oxipng files --- codecs/oxipng/index.ts | 10 -------- codecs/oxipng/spawn.ts | 52 ----------------------------------------- codecs/oxipng/worker.ts | 24 ------------------- 3 files changed, 86 deletions(-) delete mode 100644 codecs/oxipng/index.ts delete mode 100644 codecs/oxipng/spawn.ts delete mode 100644 codecs/oxipng/worker.ts diff --git a/codecs/oxipng/index.ts b/codecs/oxipng/index.ts deleted file mode 100644 index 285d916e..00000000 --- a/codecs/oxipng/index.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { threads } from 'wasm-feature-detect'; - -async function init() { - if (await threads()) { - return (await import('./spawn')).default; - } - return import('./pkg'); -} - -export default init(); diff --git a/codecs/oxipng/spawn.ts b/codecs/oxipng/spawn.ts deleted file mode 100644 index f6c77093..00000000 --- a/codecs/oxipng/spawn.ts +++ /dev/null @@ -1,52 +0,0 @@ -import initOxiPNG, { - worker_initializer, - start_main_thread, - optimise, -} from './pkg-parallel'; -// @ts-ignore -import wasmUrl from './pkg-parallel/squoosh_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; - - // First, let browser fetch and spawn Workers for our pool in the background. - // This is fairly expensive, so we want to start it as early as possible. - const workers = Array.from({ length: num }, () => new Worker('./worker', { type: 'module' })); - - // Meanwhile, asynchronously compile, instantiate and initialise Wasm on our main thread. - await initOxiPNG(fetch(wasmUrl), undefined as any); - - // Get module+memory from the Wasm instance. - // - // Ideally we wouldn't go via Wasm bindings here, since both are just JS variables, but memory is - // currently not exposed on the Wasm instance correctly by wasm-bindgen. - const workerInit: WorkerInit = worker_initializer(num); - - // Once done, we want to send module+memory to each Worker so that they instantiate Wasm too. - // While doing so, we need to wait for Workers to acknowledge that they have received our message. - // Ideally this shouldn't be necessary, but Chromium currently doesn't conform to the spec: - // https://bugs.chromium.org/p/chromium/issues/detail?id=1075645 - // - // If we didn't do this ping-pong game, the `start_main_thread` below would block the current - // thread on an atomic before even *sending* the `postMessage` containing memory, - // so Workers would never be able to unblock us back. - await Promise.all(workers.map(worker => initWorker(worker, workerInit))); - - // Finally, instantiate rayon pool - this will use shared Wasm memory to send tasks to the - // Workers and then block until they're all ready. - start_main_thread(); - - return { - optimise, - }; -} - -export default startMainThread(); diff --git a/codecs/oxipng/worker.ts b/codecs/oxipng/worker.ts deleted file mode 100644 index 69f60501..00000000 --- a/codecs/oxipng/worker.ts +++ /dev/null @@ -1,24 +0,0 @@ -/// - -import initOxiPNG, { start_worker_thread } from './pkg-parallel'; - -export type WorkerInit = [WebAssembly.Module, WebAssembly.Memory]; - -addEventListener( - 'message', - async (event) => { - // Tell the "main" thread that we've received the message. - // - // At this point, the "main" thread can run Wasm that - // will synchronously block waiting on other atomics. - // - // Note that we don't need to wait for Wasm instantiation here - it's - // better to start main thread as early as possible, and then it blocks - // on a shared atomic anyway until Worker is fully ready. - postMessage(null); - - await initOxiPNG(...(event.data as WorkerInit)); - start_worker_thread(); - }, - { once: true }, -);