Skip to content
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fixup! fs: buffer dir entries in opendir()
  • Loading branch information
addaleax committed Oct 10, 2019
commit 59cf84d2322af4b7a0e5d8c7bbc7e3fb0b575f4b
20 changes: 18 additions & 2 deletions benchmark/fs/bench-opendir.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const path = require('path');
const bench = common.createBenchmark(main, {
n: [100],
dir: [ 'lib', 'test/parallel'],
mode: [ 'async', 'sync' ]
mode: [ 'async', 'sync', 'callback' ]
});

async function main({ n, dir, mode }) {
Expand All @@ -21,7 +21,23 @@ async function main({ n, dir, mode }) {
// eslint-disable-next-line no-unused-vars
for await (const entry of await fs.promises.opendir(fullPath))
counter++;
} else {
} else if (mode === 'callback') {
const dir = await fs.promises.opendir(fullPath);
await new Promise((resolve, reject) => {
function read() {
dir.read((err, entry) => {
if (err)
reject(err);
if (entry === null)
resolve(dir.close());
else
read();
});
}

read();
});
} {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@addaleax This patch seems wrong? There should be an else here... I think the benchmark you ran included readSync() along with the callback one...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Oops, yes … funny the linter wouldn’t complain about this? The correct benchmark results for changing to setImmediate are

                                                                confidence improvement accuracy (*)    (**)   (***)
 fs/bench-opendir.js mode='callback' dir='lib' n=100                           3.40 %      ±15.95% ±21.22% ±27.62%
 fs/bench-opendir.js mode='callback' dir='test/parallel' n=100        ***    -43.16 %      ±10.70% ±14.27% ±18.66%

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This result seems strange to me, how is that worse than before?

Regardless though after further though I think that nextTick is the reasonable thing to do.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@Fishrock123 I think it makes sense – as you noted, the readSync() part was run unconditionally before, so its (unchanged) performance was included in the -30 %. If we only benchmark what has been slowed down, seeing more negative impact seems about right to me.

const dir = fs.opendirSync(fullPath);
while (dir.readSync() !== null)
counter++;
Expand Down