From ef3231380786b172f14f9a3275087938775ea2ee Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Wed, 31 Jul 2024 14:04:29 +0530 Subject: [PATCH] x4 --- web/apps/cast/src/services/pair.ts | 3 +- web/packages/new/photos/services/ml/worker.ts | 31 +++++++++++++++---- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/web/apps/cast/src/services/pair.ts b/web/apps/cast/src/services/pair.ts index b5646698cc..287122c456 100644 --- a/web/apps/cast/src/services/pair.ts +++ b/web/apps/cast/src/services/pair.ts @@ -82,7 +82,8 @@ export const register = async (): Promise => { // Register keypair with museum to get a pairing code. let pairingCode: string | undefined; - // TODO: eslint has fixed this spurious warning, but we're not on the latest + // [TODO: spurious while(true) eslint warning]. + // eslint has fixed this spurious warning, but we're not on the latest // version yet, so add a disable. // https://github.com/eslint/eslint/pull/18286 /* eslint-disable no-constant-condition */ diff --git a/web/packages/new/photos/services/ml/worker.ts b/web/packages/new/photos/services/ml/worker.ts index 5de8c08390..ca5f112468 100644 --- a/web/packages/new/photos/services/ml/worker.ts +++ b/web/packages/new/photos/services/ml/worker.ts @@ -288,15 +288,34 @@ const indexNextBatch = async ( // Nothing to do. if (items.length == 0) return false; - // Index, keeping track if any of the items failed. + // Keep track if any of the items failed. let allSuccess = true; - for (const item of items) { - try { - await index(item, electron); - } catch { - allSuccess = false; + + // Index up to 4 items simultaneously. + const tasks = new Array | undefined>(4).fill(undefined); + + let i = 0; + while (i < items.length) { + for (let j = 0; j < tasks.length; j++) { + if (!tasks[j]) { + tasks[j] = index(ensure(items[i++]), electron) + .then(() => { + tasks[j] = undefined; + }) + .catch(() => { + allSuccess = false; + tasks[j] = undefined; + }); + } } + + // Wait for at least one to complete (the other runners continue running + // even if one promise reaches the finish line). + await Promise.race(tasks); + + // Let the main thread now we're doing something. delegate?.workerDidProcessFile(); + // Let us drain the microtask queue. This also gives a chance for other // interactive tasks like `clipMatches` to run. await wait(0);