Skip to content

Commit d0bf8c2

Browse files
joyeecheungBridgeAR
authored andcommitted
benchmark: add dir and withFileTypes option readdir benchmarks
PR-URL: #24125 Refs: v8/v8@0483e9a Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Yang Guo <yangguo@chromium.org> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Refael Ackermann <refack@gmail.com>
1 parent f49b4fc commit d0bf8c2

File tree

4 files changed

+28
-61
lines changed

4 files changed

+28
-61
lines changed

benchmark/fs/bench-readdir.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@ const fs = require('fs');
55
const path = require('path');
66

77
const bench = common.createBenchmark(main, {
8-
n: [1e4],
8+
n: [10],
9+
dir: [ 'lib', 'test/parallel'],
10+
withFileTypes: ['true', 'false']
911
});
1012

11-
12-
function main({ n }) {
13+
function main({ n, dir, withFileTypes }) {
14+
withFileTypes = withFileTypes === 'true';
15+
const fullPath = path.resolve(__dirname, '../../', dir);
1316
bench.start();
1417
(function r(cntr) {
1518
if (cntr-- <= 0)
1619
return bench.end(n);
17-
fs.readdir(path.resolve(__dirname, '../../lib/'), function() {
20+
fs.readdir(fullPath, { withFileTypes }, function() {
1821
r(cntr);
1922
});
2023
}(n));

benchmark/fs/bench-readdirSync.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@ const fs = require('fs');
55
const path = require('path');
66

77
const bench = common.createBenchmark(main, {
8-
n: [1e4],
8+
n: [10],
9+
dir: [ 'lib', 'test/parallel'],
10+
withFileTypes: ['true', 'false']
911
});
1012

1113

12-
function main({ n }) {
14+
function main({ n, dir, withFileTypes }) {
15+
withFileTypes = withFileTypes === 'true';
16+
const fullPath = path.resolve(__dirname, '../../', dir);
1317
bench.start();
1418
for (var i = 0; i < n; i++) {
15-
fs.readdirSync(path.resolve(__dirname, '../../lib/'));
19+
fs.readdirSync(fullPath, { withFileTypes });
1620
}
1721
bench.end(n);
1822
}

src/node_file.cc

+11-53
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ void AfterScanDir(uv_fs_t* req) {
571571
Environment* env = req_wrap->env();
572572
Local<Value> error;
573573
int r;
574-
std::vector<Local<Value>> name_argv;
574+
std::vector<Local<Value>> name_v;
575575

576576
for (int i = 0; ; i++) {
577577
uv_dirent_t ent;
@@ -593,12 +593,10 @@ void AfterScanDir(uv_fs_t* req) {
593593
if (filename.IsEmpty())
594594
return req_wrap->Reject(error);
595595

596-
name_argv.push_back(filename.ToLocalChecked());
596+
name_v.push_back(filename.ToLocalChecked());
597597
}
598598

599-
Local<Array> names =
600-
Array::New(env->isolate(), name_argv.data(), name_argv.size());
601-
req_wrap->Resolve(names);
599+
req_wrap->Resolve(Array::New(env->isolate(), name_v.data(), name_v.size()));
602600
}
603601

604602
void AfterScanDirWithTypes(uv_fs_t* req) {
@@ -613,13 +611,9 @@ void AfterScanDirWithTypes(uv_fs_t* req) {
613611
Isolate* isolate = env->isolate();
614612
Local<Value> error;
615613
int r;
616-
Local<Array> names = Array::New(isolate, 0);
617-
Local<Function> fn = env->push_values_to_array_function();
618-
Local<Value> name_argv[NODE_PUSH_VAL_TO_ARRAY_MAX];
619-
size_t name_idx = 0;
620-
Local<Value> types = Array::New(isolate, 0);
621-
Local<Value> type_argv[NODE_PUSH_VAL_TO_ARRAY_MAX];
622-
size_t type_idx = 0;
614+
615+
std::vector<Local<Value>> name_v;
616+
std::vector<Local<Value>> type_v;
623617

624618
for (int i = 0; ; i++) {
625619
uv_dirent_t ent;
@@ -641,48 +635,13 @@ void AfterScanDirWithTypes(uv_fs_t* req) {
641635
if (filename.IsEmpty())
642636
return req_wrap->Reject(error);
643637

644-
name_argv[name_idx++] = filename.ToLocalChecked();
645-
646-
if (name_idx >= arraysize(name_argv)) {
647-
MaybeLocal<Value> ret = fn->Call(env->context(), names, name_idx,
648-
name_argv);
649-
if (ret.IsEmpty()) {
650-
return;
651-
}
652-
name_idx = 0;
653-
}
654-
655-
type_argv[type_idx++] = Integer::New(isolate, ent.type);
656-
657-
if (type_idx >= arraysize(type_argv)) {
658-
MaybeLocal<Value> ret = fn->Call(env->context(), types, type_idx,
659-
type_argv);
660-
if (ret.IsEmpty()) {
661-
return;
662-
}
663-
type_idx = 0;
664-
}
665-
}
666-
667-
if (name_idx > 0) {
668-
MaybeLocal<Value> ret = fn->Call(env->context(), names, name_idx,
669-
name_argv);
670-
if (ret.IsEmpty()) {
671-
return;
672-
}
673-
}
674-
675-
if (type_idx > 0) {
676-
MaybeLocal<Value> ret = fn->Call(env->context(), types, type_idx,
677-
type_argv);
678-
if (ret.IsEmpty()) {
679-
return;
680-
}
638+
name_v.push_back(filename.ToLocalChecked());
639+
type_v.push_back(Integer::New(isolate, ent.type));
681640
}
682641

683642
Local<Array> result = Array::New(isolate, 2);
684-
result->Set(0, names);
685-
result->Set(1, types);
643+
result->Set(0, Array::New(isolate, name_v.data(), name_v.size()));
644+
result->Set(1, Array::New(isolate, type_v.data(), type_v.size()));
686645
req_wrap->Resolve(result);
687646
}
688647

@@ -1523,9 +1482,8 @@ static void ReadDir(const FunctionCallbackInfo<Value>& args) {
15231482
Local<Array> names = Array::New(isolate, name_v.data(), name_v.size());
15241483
if (with_types) {
15251484
Local<Array> result = Array::New(isolate, 2);
1526-
Local<Value> types = Array::New(isolate, type_v.data(), type_v.size());
15271485
result->Set(0, names);
1528-
result->Set(1, types);
1486+
result->Set(1, Array::New(isolate, type_v.data(), type_v.size()));
15291487
args.GetReturnValue().Set(result);
15301488
} else {
15311489
args.GetReturnValue().Set(names);

test/parallel/test-benchmark-fs.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,7 @@ runBenchmark('fs', [
1616
'statType=fstat',
1717
'statSyncType=fstatSync',
1818
'encodingType=buf',
19-
'filesize=1024'
19+
'filesize=1024',
20+
'dir=.github',
21+
'withFileTypes=false'
2022
], { NODE_TMPDIR: tmpdir.path, NODEJS_BENCHMARK_ZERO_ALLOWED: 1 });

0 commit comments

Comments
 (0)