Skip to content

fs: Performance Enhancement: Concurrent Execution of Async Tasks with Promise.all #51215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/internal/fs/cp/cp.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,13 +313,16 @@ async function mkDirAndCopy(srcMode, src, dest, opts) {

async function copyDir(src, dest, opts) {
const dir = await opendir(src);
const promises = [];

for await (const { name } of dir) {
const srcItem = join(src, name);
const destItem = join(dest, name);
const { destStat, skipped } = await checkPaths(srcItem, destItem, opts);
if (!skipped) await getStatsForCopy(destStat, srcItem, destItem, opts);
if (!skipped) PromisePrototypePush(promises, getStatsForCopy(destStat, srcItem, destItem, opts));
}

await SafePromiseAllReturnVoid(promises);
}

async function onLink(destStat, src, dest, opts) {
Expand Down
4 changes: 3 additions & 1 deletion lib/internal/fs/recursive_watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ class FSWatcher extends EventEmitter {

try {
const files = await opendir(folder);
const promises = [];

for await (const file of files) {
if (this.#closed) {
Expand All @@ -159,10 +160,11 @@ class FSWatcher extends EventEmitter {
if (file.isFile()) {
this.#watchFile(f);
} else if (file.isDirectory() && !file.isSymbolicLink()) {
await this.#watchFolder(f);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving to Promise.all, this code will not probably throw EMFILE, too many open files?

PromisePrototypePush(promises, this.#watchFolder(f));
}
}
}
await SafePromiseAllReturnVoid(promises);
} catch (error) {
this.emit('error', error);
}
Expand Down
4 changes: 3 additions & 1 deletion lib/internal/main/watch_mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,16 @@ async function restart() {

(async () => {
emitExperimentalWarning('Watch mode');
const promises = [];

try {
start();

// eslint-disable-next-line no-unused-vars
for await (const _ of on(watcher, 'changed')) {
await restart();
PromisePrototypePush(promises, restart());
}
await SafePromiseAllReturnVoid(promises);
} catch (error) {
triggerUncaughtException(error, true /* fromPromise */);
}
Expand Down