Files
node/lib/internal/fs/dir.js
T
Anna Henningsen 216e200fa9 fs: buffer dir entries in opendir()
Read up to 32 directory entries in one batch when `dir.readSync()`
or `dir.read()` are called.

This increases performance significantly, although it introduces
quite a bit of edge case complexity.

                                                                 confidence improvement accuracy (*)    (**)    (***)
     fs/bench-opendir.js mode='async' dir='lib' n=100                  ***    155.93 %      ±30.05% ±40.34%  ±53.21%
     fs/bench-opendir.js mode='async' dir='test/parallel' n=100        ***    479.65 %      ±56.81% ±76.47% ±101.32%
     fs/bench-opendir.js mode='sync' dir='lib' n=100                           10.38 %      ±14.39% ±19.16%  ±24.96%
     fs/bench-opendir.js mode='sync' dir='test/parallel' n=100         ***     63.13 %      ±12.84% ±17.18%  ±22.58%

PR-URL: https://github.com/nodejs/node/pull/29893
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: David Carlier <devnexen@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
2019-11-10 14:50:07 +01:00

223 lines
5.1 KiB
JavaScript

'use strict';
const { Object } = primordials;
const pathModule = require('path');
const binding = internalBinding('fs');
const dirBinding = internalBinding('fs_dir');
const {
codes: {
ERR_DIR_CLOSED,
ERR_INVALID_CALLBACK,
ERR_MISSING_ARGS
}
} = require('internal/errors');
const { FSReqCallback } = binding;
const internalUtil = require('internal/util');
const {
getDirent,
getOptions,
getValidatedPath,
handleErrorFromBinding
} = require('internal/fs/utils');
const kDirHandle = Symbol('kDirHandle');
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');
class Dir {
constructor(handle, path, options) {
if (handle == null) throw new ERR_MISSING_ARGS('handle');
this[kDirHandle] = handle;
this[kDirBufferedEntries] = [];
this[kDirPath] = path;
this[kDirClosed] = false;
this[kDirOptions] = getOptions(options, {
encoding: 'utf8'
});
this[kDirReadPromisified] =
internalUtil.promisify(this[kDirReadImpl]).bind(this, false);
this[kDirClosePromisified] = internalUtil.promisify(this.close).bind(this);
}
get path() {
return this[kDirPath];
}
read(callback) {
return this[kDirReadImpl](true, callback);
}
[kDirReadImpl](maybeSync, callback) {
if (this[kDirClosed] === true) {
throw new ERR_DIR_CLOSED();
}
if (callback === undefined) {
return this[kDirReadPromisified]();
} else if (typeof callback !== 'function') {
throw new ERR_INVALID_CALLBACK(callback);
}
if (this[kDirBufferedEntries].length > 0) {
const [ name, type ] = this[kDirBufferedEntries].splice(0, 2);
if (maybeSync)
process.nextTick(getDirent, this[kDirPath], name, type, callback);
else
getDirent(this[kDirPath], name, type, callback);
return;
}
const req = new FSReqCallback();
req.oncomplete = (err, result) => {
if (err || result === null) {
return callback(err, result);
}
this[kDirBufferedEntries] = result.slice(2);
getDirent(this[kDirPath], result[0], result[1], callback);
};
this[kDirHandle].read(
this[kDirOptions].encoding,
req
);
}
readSync(options) {
if (this[kDirClosed] === true) {
throw new ERR_DIR_CLOSED();
}
if (this[kDirBufferedEntries].length > 0) {
const [ name, type ] = this[kDirBufferedEntries].splice(0, 2);
return getDirent(this[kDirPath], name, type);
}
const ctx = { path: this[kDirPath] };
const result = this[kDirHandle].read(
this[kDirOptions].encoding,
undefined,
ctx
);
handleErrorFromBinding(ctx);
if (result === null) {
return result;
}
this[kDirBufferedEntries] = result.slice(2);
return getDirent(this[kDirPath], result[0], result[1]);
}
close(callback) {
if (this[kDirClosed] === true) {
throw new ERR_DIR_CLOSED();
}
if (callback === undefined) {
return this[kDirClosePromisified]();
} else if (typeof callback !== 'function') {
throw new ERR_INVALID_CALLBACK(callback);
}
this[kDirClosed] = true;
const req = new FSReqCallback();
req.oncomplete = callback;
this[kDirHandle].close(req);
}
closeSync() {
if (this[kDirClosed] === true) {
throw new ERR_DIR_CLOSED();
}
this[kDirClosed] = true;
const ctx = { path: this[kDirPath] };
const result = this[kDirHandle].close(undefined, ctx);
handleErrorFromBinding(ctx);
return result;
}
async* entries() {
try {
while (true) {
const result = await this[kDirReadPromisified]();
if (result === null) {
break;
}
yield result;
}
} finally {
await this[kDirClosePromisified]();
}
}
}
Object.defineProperty(Dir.prototype, Symbol.asyncIterator, {
value: Dir.prototype.entries,
enumerable: false,
writable: true,
configurable: true,
});
function opendir(path, options, callback) {
callback = typeof options === 'function' ? options : callback;
if (typeof callback !== 'function') {
throw new ERR_INVALID_CALLBACK(callback);
}
path = getValidatedPath(path);
options = getOptions(options, {
encoding: 'utf8'
});
function opendirCallback(error, handle) {
if (error) {
callback(error);
} else {
callback(null, new Dir(handle, path, options));
}
}
const req = new FSReqCallback();
req.oncomplete = opendirCallback;
dirBinding.opendir(
pathModule.toNamespacedPath(path),
options.encoding,
req
);
}
function opendirSync(path, options) {
path = getValidatedPath(path);
options = getOptions(options, {
encoding: 'utf8'
});
const ctx = { path };
const handle = dirBinding.opendir(
pathModule.toNamespacedPath(path),
options.encoding,
undefined,
ctx
);
handleErrorFromBinding(ctx);
return new Dir(handle, path, options);
}
module.exports = {
Dir,
opendir,
opendirSync
};