Skip to content

Commit a977b8c

Browse files
committed
Set Guetzli tasks simultaneous limit to 1
1 parent 989eb60 commit a977b8c

File tree

2 files changed

+65
-66
lines changed

2 files changed

+65
-66
lines changed

convert.js

Lines changed: 50 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -49,59 +49,57 @@ export async function convert({ filePaths, config }) {
4949

5050
const totalSize = { before: 0, after: 0 };
5151

52-
const avifConfig = isLossless
53-
? config?.avif?.lossless
54-
: config?.avif?.lossy;
55-
const webpConfig = isLossless
56-
? config?.webp?.lossless
57-
: config?.webp?.lossy;
58-
const webpGifConfig = isLossless
59-
? config?.webpGif?.lossless
60-
: config?.webpGif?.lossy;
61-
62-
const tasksSimultaneousLimit = pLimit(os.cpus().length);
63-
const tasksPromises = filePaths.reduce((accumulator, filePath) => {
64-
if (shouldConvertToAvif) {
65-
accumulator.push(
66-
tasksSimultaneousLimit(
67-
() => processFile({
68-
filePath,
69-
config: avifConfig || {},
70-
progressBarContainer,
71-
progressBar,
72-
totalSize,
73-
isForced,
74-
format: 'AVIF',
75-
processFunction: processAvif,
76-
}),
77-
),
78-
);
79-
}
80-
81-
if (shouldConvertToWebp) {
82-
accumulator.push(
83-
tasksSimultaneousLimit(
84-
() => processFile({
85-
filePath,
86-
config: (path.extname(filePath.input).toLowerCase() === '.gif'
87-
? webpGifConfig
88-
: webpConfig)
89-
|| {},
90-
progressBarContainer,
91-
progressBar,
92-
totalSize,
93-
isForced,
94-
format: 'WebP',
95-
processFunction: processWebp,
96-
}),
97-
),
98-
);
99-
}
100-
101-
return accumulator;
102-
}, []);
52+
const getConfig = format => config?.[format]?.[isLossless ? 'lossless' : 'lossy'];
53+
54+
const avifConfig = getConfig('avif');
55+
const webpConfig = getConfig('webp');
56+
const webpGifConfig = getConfig('webpGif');
57+
58+
const cpuCount = os.cpus().length;
59+
const tasksSimultaneousLimit = pLimit(cpuCount);
60+
61+
await Promise.all(
62+
filePaths.reduce((accumulator, filePath) => {
63+
if (shouldConvertToAvif) {
64+
accumulator.push(
65+
tasksSimultaneousLimit(
66+
() => processFile({
67+
filePath,
68+
config: avifConfig || {},
69+
progressBarContainer,
70+
progressBar,
71+
totalSize,
72+
isForced,
73+
format: 'AVIF',
74+
processFunction: processAvif,
75+
}),
76+
),
77+
);
78+
}
79+
80+
if (shouldConvertToWebp) {
81+
const isGif = path.extname(filePath.input).toLowerCase() === '.gif';
82+
83+
accumulator.push(
84+
tasksSimultaneousLimit(
85+
() => processFile({
86+
filePath,
87+
config: (isGif ? webpGifConfig : webpConfig) || {},
88+
progressBarContainer,
89+
progressBar,
90+
totalSize,
91+
isForced,
92+
format: 'WebP',
93+
processFunction: processWebp,
94+
}),
95+
),
96+
);
97+
}
98+
99+
return accumulator;
100+
}, []),
101+
);
103102

104-
await Promise.all(tasksPromises);
105103
progressBarContainer.update(); // Prevent logs lost. See: https://github.com/npkgz/cli-progress/issues/145#issuecomment-1859594159
106104
progressBarContainer.stop();
107105

optimize.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,29 @@ export async function optimize({ filePaths, config }) {
4242
const totalSize = { before: 0, after: 0 };
4343

4444
const cpuCount = os.cpus().length;
45-
const tasksSimultaneousLimit = pLimit(
46-
/*
47-
Guetzli uses a large amount of memory and a significant amount of CPU time.
48-
To reduce the processor load in lossless mode, we reduce the number
49-
of simultaneous tasks by half.
50-
*/
51-
isLossless ? Math.round(cpuCount / 2) : cpuCount,
52-
);
53-
const tasksPromises = filePaths.map(
54-
filePath => tasksSimultaneousLimit(
55-
() => processFile({
45+
const tasksSimultaneousLimit = pLimit(cpuCount);
46+
const guetzliTasksSimultaneousLimit = pLimit(1); // Guetzli uses a large amount of memory and a significant amount of CPU time. To reduce system load, we only allow one instance of guetzli to run at the same time.
47+
48+
await Promise.all(
49+
filePaths.map(filePath => {
50+
const extension = path.extname(filePath.input).toLowerCase();
51+
const isJpeg = extension === '.jpg' || extension === '.jpeg';
52+
53+
const limit = isJpeg && isLossless
54+
? guetzliTasksSimultaneousLimit
55+
: tasksSimultaneousLimit;
56+
57+
return limit(() => processFile({
5658
filePath,
5759
config,
5860
progressBarContainer,
5961
progressBar,
6062
totalSize,
6163
isLossless,
62-
}),
63-
),
64+
}));
65+
}),
6466
);
6567

66-
await Promise.all(tasksPromises);
6768
progressBarContainer.update(); // Prevent logs lost. See: https://github.com/npkgz/cli-progress/issues/145#issuecomment-1859594159
6869
progressBarContainer.stop();
6970

0 commit comments

Comments
 (0)