Skip to content
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

fs: buffer dir entries in opendir() #29893

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
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 6d209fce082ce2ae73dc9555badcdeedb0079b41
32 changes: 15 additions & 17 deletions benchmark/fs/bench-opendir.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,24 @@ const bench = common.createBenchmark(main, {
mode: [ 'async', 'sync' ]
});

function main({ n, dir, mode }) {
async function main({ n, dir, mode }) {
const fullPath = path.resolve(__dirname, '../../', dir);

(async () => {
bench.start();
bench.start();

let counter = 0;
for (let i = 0; i < n; i++) {
if (mode === 'async') {
// eslint-disable-next-line no-unused-vars
for await (const entry of await fs.promises.opendir(fullPath))
counter++;
} else {
const dir = fs.opendirSync(fullPath);
while (dir.readSync() !== null)
counter++;
dir.closeSync();
}
let counter = 0;
for (let i = 0; i < n; i++) {
if (mode === 'async') {
// eslint-disable-next-line no-unused-vars
for await (const entry of await fs.promises.opendir(fullPath))
counter++;
} else {
const dir = fs.opendirSync(fullPath);
while (dir.readSync() !== null)
counter++;
dir.closeSync();
}
}

bench.end(counter);
})();
bench.end(counter);
}
12 changes: 10 additions & 2 deletions lib/internal/fs/dir.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const kDirPath = Symbol('kDirPath');
const kDirBufferedEntries = Symbol('kDirBufferedEntries');
const kDirClosed = Symbol('kDirClosed');
const kDirOptions = Symbol('kDirOptions');
const kDirReadImpl = Symbol('kDirReadImpl');
const kDirReadPromisified = Symbol('kDirReadPromisified');
const kDirClosePromisified = Symbol('kDirClosePromisified');

Expand All @@ -42,7 +43,7 @@ class Dir {
encoding: 'utf8'
});

this[kDirReadPromisified] = internalUtil.promisify(this.read).bind(this);
this[kDirReadPromisified] = internalUtil.promisify(this[kDirReadImpl]).bind(this, false);
this[kDirClosePromisified] = internalUtil.promisify(this.close).bind(this);
}

Expand All @@ -51,6 +52,10 @@ class Dir {
}

read(callback) {
return this[kDirReadImpl](true, callback);
}

[kDirReadImpl](maybeSync, callback) {
if (this[kDirClosed] === true) {
throw new ERR_DIR_CLOSED();
}
Expand All @@ -63,7 +68,10 @@ class Dir {

if (this[kDirBufferedEntries].length > 0) {
const [ name, type ] = this[kDirBufferedEntries].splice(0, 2);
getDirent(this[kDirPath], name, type, callback);
if (maybeSync)
process.nextTick(getDirent, this[kDirPath], name, type, callback);
else
getDirent(this[kDirPath], name, type, callback);
return;
}

Expand Down
14 changes: 12 additions & 2 deletions test/parallel/test-fs-opendir.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,27 @@ const dirclosedError = {
// Check the opendir async version
fs.opendir(testDir, common.mustCall(function(err, dir) {
assert.ifError(err);
dir.read(common.mustCall(function(err, dirent) {
let sync = true;
dir.read(common.mustCall((err, dirent) => {
assert(!sync);
assert.ifError(err);

// Order is operating / file system dependent
assert(files.includes(dirent.name), `'files' should include ${dirent}`);
assertDirent(dirent);

dir.close(common.mustCall(function(err) {
let syncInner = true;
dir.read(common.mustCall((err, dirent) => {
assert(!syncInner);
assert.ifError(err);

dir.close(common.mustCall(function(err) {
assert.ifError(err);
}));
}));
syncInner = false;
}));
sync = false;
}));

// opendir() on file should throw ENOTDIR
Expand Down