Skip to content

Commit ac9b36b

Browse files
committed
fs: add bufferSize option to fs.opendir()
Add an option that controls the size of the internal buffer. Fixes: #29941
1 parent 5fa55ee commit ac9b36b

File tree

5 files changed

+64
-18
lines changed

5 files changed

+64
-18
lines changed

benchmark/fs/bench-opendir.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,24 @@ const path = require('path');
77
const bench = common.createBenchmark(main, {
88
n: [100],
99
dir: [ 'lib', 'test/parallel'],
10-
mode: [ 'async', 'sync', 'callback' ]
10+
mode: [ 'async', 'sync', 'callback' ],
11+
bufferSize: [ 4, 32, 1024 ]
1112
});
1213

13-
async function main({ n, dir, mode }) {
14+
async function main({ n, dir, mode, bufferSize }) {
1415
const fullPath = path.resolve(__dirname, '../../', dir);
1516

1617
bench.start();
1718

1819
let counter = 0;
1920
for (let i = 0; i < n; i++) {
2021
if (mode === 'async') {
22+
const dir = await fs.promises.opendir(fullPath, { bufferSize });
2123
// eslint-disable-next-line no-unused-vars
22-
for await (const entry of await fs.promises.opendir(fullPath))
24+
for await (const entry of dir)
2325
counter++;
2426
} else if (mode === 'callback') {
25-
const dir = await fs.promises.opendir(fullPath);
27+
const dir = await fs.promises.opendir(fullPath, { bufferSize });
2628
await new Promise((resolve, reject) => {
2729
function read() {
2830
dir.read((err, entry) => {
@@ -40,7 +42,7 @@ async function main({ n, dir, mode }) {
4042
read();
4143
});
4244
} else {
43-
const dir = fs.opendirSync(fullPath);
45+
const dir = fs.opendirSync(fullPath, { bufferSize });
4446
while (dir.readSync() !== null)
4547
counter++;
4648
dir.closeSync();

doc/api/fs.md

+21
Original file line numberDiff line numberDiff line change
@@ -2625,11 +2625,18 @@ Functions based on `fs.open()` exhibit this behavior as well:
26252625
## fs.opendir(path\[, options\], callback)
26262626
<!-- YAML
26272627
added: v12.12.0
2628+
changes:
2629+
- version: REPLACME
2630+
pr-url: ???
2631+
description: The `bufferSize` option was introduced.
26282632
-->
26292633

26302634
* `path` {string|Buffer|URL}
26312635
* `options` {Object}
26322636
* `encoding` {string|null} **Default:** `'utf8'`
2637+
* `bufferSize` {number} Size of the internal buffer when reading
2638+
directory entries. Higher values lead to better performance but higher
2639+
memory usage. **Default:** `32`
26332640
* `callback` {Function}
26342641
* `err` {Error}
26352642
* `dir` {fs.Dir}
@@ -2645,11 +2652,18 @@ directory and subsequent read operations.
26452652
## fs.opendirSync(path\[, options\])
26462653
<!-- YAML
26472654
added: v12.12.0
2655+
changes:
2656+
- version: REPLACME
2657+
pr-url: ???
2658+
description: The `bufferSize` option was introduced.
26482659
-->
26492660

26502661
* `path` {string|Buffer|URL}
26512662
* `options` {Object}
26522663
* `encoding` {string|null} **Default:** `'utf8'`
2664+
* `bufferSize` {number} Size of the internal buffer when reading
2665+
directory entries. Higher values lead to better performance but higher
2666+
memory usage. **Default:** `32`
26532667
* Returns: {fs.Dir}
26542668

26552669
Synchronously open a directory. See opendir(3).
@@ -4829,11 +4843,18 @@ a colon, Node.js will open a file system stream, as described by
48294843
### fsPromises.opendir(path\[, options\])
48304844
<!-- YAML
48314845
added: v12.12.0
4846+
changes:
4847+
- version: REPLACME
4848+
pr-url: ???
4849+
description: The `bufferSize` option was introduced.
48324850
-->
48334851

48344852
* `path` {string|Buffer|URL}
48354853
* `options` {Object}
48364854
* `encoding` {string|null} **Default:** `'utf8'`
4855+
* `bufferSize` {number} Size of the internal buffer when reading
4856+
directory entries. Higher values lead to better performance but higher
4857+
memory usage. **Default:** `32`
48374858
* Returns: {Promise} containing {fs.Dir}
48384859

48394860
Asynchronously open a directory. See opendir(3).

lib/internal/fs/dir.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const {
99
codes: {
1010
ERR_DIR_CLOSED,
1111
ERR_INVALID_CALLBACK,
12+
ERR_INVALID_OPT_VALUE,
1213
ERR_MISSING_ARGS
1314
}
1415
} = require('internal/errors');
@@ -39,9 +40,19 @@ class Dir {
3940
this[kDirPath] = path;
4041
this[kDirClosed] = false;
4142

42-
this[kDirOptions] = getOptions(options, {
43-
encoding: 'utf8'
44-
});
43+
this[kDirOptions] = {
44+
bufferSize: 32,
45+
...getOptions(options, {
46+
encoding: 'utf8'
47+
})
48+
};
49+
50+
if (typeof this[kDirOptions].bufferSize !== 'number' ||
51+
!Number.isInteger(this[kDirOptions].bufferSize) ||
52+
this[kDirOptions].bufferSize <= 0) {
53+
throw new ERR_INVALID_OPT_VALUE('bufferSize',
54+
this[kDirOptions].bufferSize);
55+
}
4556

4657
this[kDirReadPromisified] =
4758
internalUtil.promisify(this[kDirReadImpl]).bind(this, false);
@@ -88,6 +99,7 @@ class Dir {
8899

89100
this[kDirHandle].read(
90101
this[kDirOptions].encoding,
102+
this[kDirOptions].bufferSize,
91103
req
92104
);
93105
}
@@ -105,6 +117,7 @@ class Dir {
105117
const ctx = { path: this[kDirPath] };
106118
const result = this[kDirHandle].read(
107119
this[kDirOptions].encoding,
120+
this[kDirOptions].bufferSize,
108121
undefined,
109122
ctx
110123
);

src/node_dir.cc

+18-8
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ using v8::Isolate;
3636
using v8::Local;
3737
using v8::MaybeLocal;
3838
using v8::Null;
39+
using v8::Number;
3940
using v8::Object;
4041
using v8::ObjectTemplate;
4142
using v8::String;
@@ -59,8 +60,8 @@ DirHandle::DirHandle(Environment* env, Local<Object> obj, uv_dir_t* dir)
5960
dir_(dir) {
6061
MakeWeak();
6162

62-
dir_->nentries = arraysize(dirents_);
63-
dir_->dirents = dirents_;
63+
dir_->nentries = 0;
64+
dir_->dirents = nullptr;
6465
}
6566

6667
DirHandle* DirHandle::New(Environment* env, uv_dir_t* dir) {
@@ -230,22 +231,31 @@ void DirHandle::Read(const FunctionCallbackInfo<Value>& args) {
230231
Isolate* isolate = env->isolate();
231232

232233
const int argc = args.Length();
233-
CHECK_GE(argc, 2);
234+
CHECK_GE(argc, 3);
234235

235236
const enum encoding encoding = ParseEncoding(isolate, args[0], UTF8);
236237

237238
DirHandle* dir;
238239
ASSIGN_OR_RETURN_UNWRAP(&dir, args.Holder());
239240

240-
FSReqBase* req_wrap_async = GetReqWrap(env, args[1]);
241-
if (req_wrap_async != nullptr) { // dir.read(encoding, req)
241+
CHECK(args[1]->IsNumber());
242+
uint64_t buffer_size = args[1].As<Number>()->Value();
243+
244+
if (buffer_size != dir->dirents_.size()) {
245+
dir->dirents_.resize(buffer_size);
246+
dir->dir_->nentries = buffer_size;
247+
dir->dir_->dirents = dir->dirents_.data();
248+
}
249+
250+
FSReqBase* req_wrap_async = GetReqWrap(env, args[2]);
251+
if (req_wrap_async != nullptr) { // dir.read(encoding, bufferSize, req)
242252
AsyncCall(env, req_wrap_async, args, "readdir", encoding,
243253
AfterDirRead, uv_fs_readdir, dir->dir());
244-
} else { // dir.read(encoding, undefined, ctx)
245-
CHECK_EQ(argc, 3);
254+
} else { // dir.read(encoding, bufferSize, undefined, ctx)
255+
CHECK_EQ(argc, 4);
246256
FSReqWrapSync req_wrap_sync;
247257
FS_DIR_SYNC_TRACE_BEGIN(readdir);
248-
int err = SyncCall(env, args[2], &req_wrap_sync, "readdir", uv_fs_readdir,
258+
int err = SyncCall(env, args[3], &req_wrap_sync, "readdir", uv_fs_readdir,
249259
dir->dir());
250260
FS_DIR_SYNC_TRACE_END(readdir);
251261
if (err < 0) {

src/node_dir.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ class DirHandle : public AsyncWrap {
4545
void GCClose();
4646

4747
uv_dir_t* dir_;
48-
// Up to 32 directory entries are read through a single libuv call.
49-
uv_dirent_t dirents_[32];
48+
// Multiple entries are read through a single libuv call.
49+
std::vector<uv_dirent_t> dirents_;
5050
bool closing_ = false;
5151
bool closed_ = false;
5252
};

0 commit comments

Comments
 (0)