mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-12 00:37:19 +00:00
Move image decoding into the worker pool
This commit is contained in:
@@ -104,11 +104,33 @@ async function encodeFile({
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// both decoding and encoding go through the worker pool
|
||||||
|
function handleJob(params) {
|
||||||
|
const { operation } = params;
|
||||||
|
if (operation === 'encode') {
|
||||||
|
return encodeFile(params);
|
||||||
|
}
|
||||||
|
if (operation === 'decode') {
|
||||||
|
return decodeFile(params.file);
|
||||||
|
}
|
||||||
|
}
|
||||||
async function processFiles(files) {
|
async function processFiles(files) {
|
||||||
const workerPool = new WorkerPool(cpus().length, __filename);
|
const workerPool = new WorkerPool(cpus().length, __filename);
|
||||||
// Create output directory
|
// Create output directory
|
||||||
await fsp.mkdir(program.outputDir, { recursive: true });
|
await fsp.mkdir(program.outputDir, { recursive: true });
|
||||||
|
|
||||||
|
let decoded = 0;
|
||||||
|
const decodedFiles = await Promise.all(files.map(async file => {
|
||||||
|
const result = await workerPool.dispatchJob({ operation: 'decode', file });
|
||||||
|
results.set(file, {
|
||||||
|
file: result.file,
|
||||||
|
size: result.size,
|
||||||
|
outputs: []
|
||||||
|
});
|
||||||
|
progress.setProgress(++decoded, files.length);
|
||||||
|
return result;
|
||||||
|
}));
|
||||||
|
|
||||||
const decodedFiles = await Promise.all(files.map(file => decodeFile(file)));
|
const decodedFiles = await Promise.all(files.map(file => decodeFile(file)));
|
||||||
|
|
||||||
let jobsStarted = 0;
|
let jobsStarted = 0;
|
||||||
@@ -135,6 +157,7 @@ async function processFiles(files) {
|
|||||||
jobsStarted++;
|
jobsStarted++;
|
||||||
workerPool
|
workerPool
|
||||||
.dispatchJob({
|
.dispatchJob({
|
||||||
|
operation: 'encode',
|
||||||
file,
|
file,
|
||||||
size,
|
size,
|
||||||
bitmap,
|
bitmap,
|
||||||
@@ -195,5 +218,5 @@ if (isMainThread) {
|
|||||||
|
|
||||||
program.parse(process.argv);
|
program.parse(process.argv);
|
||||||
} else {
|
} else {
|
||||||
WorkerPool.useThisThreadAsWorker(encodeFile);
|
WorkerPool.useThisThreadAsWorker(handleJob);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user