Skip to content

Commit 569c96b

Browse files
CanadaHonkanonrig
authored andcommitted
fs: improve error performance for mkdirSync
1 parent 00de2fa commit 569c96b

File tree

4 files changed

+68
-40
lines changed

4 files changed

+68
-40
lines changed

benchmark/fs/bench-mkdirSync.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const fs = require('fs');
5+
const tmpdir = require('../../test/common/tmpdir');
6+
tmpdir.refresh();
7+
8+
const bench = common.createBenchmark(main, {
9+
type: ['existing', 'non-existing'],
10+
recursive: ['true', 'false'],
11+
n: [1e3],
12+
});
13+
14+
function main({ n, type, recursive }) {
15+
recursive = recursive === 'true';
16+
let files;
17+
18+
switch (type) {
19+
case 'non-existing':
20+
files = [];
21+
22+
// Populate tmpdir with target dirs
23+
for (let i = 0; i < n; i++) {
24+
const path = tmpdir.resolve(recursive ? `rmdirsync-bench-dir-${process.pid}-${i}/a/b/c` : `rmdirsync-bench-dir-${process.pid}-${i}`);
25+
files.push(path);
26+
}
27+
break;
28+
case 'existing':
29+
files = new Array(n).fill(__dirname);
30+
break;
31+
default:
32+
new Error('Invalid type');
33+
}
34+
35+
bench.start();
36+
for (let i = 0; i < n; i++) {
37+
try {
38+
fs.mkdirSync(files[i], { recursive });
39+
} catch {
40+
// do nothing
41+
}
42+
}
43+
bench.end(n);
44+
}

lib/fs.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,11 +1385,12 @@ function mkdirSync(path, options) {
13851385
path = getValidatedPath(path);
13861386
validateBoolean(recursive, 'options.recursive');
13871387

1388-
const ctx = { path };
1389-
const result = binding.mkdir(pathModule.toNamespacedPath(path),
1390-
parseFileMode(mode, 'mode'), recursive,
1391-
undefined, ctx);
1392-
handleErrorFromBinding(ctx);
1388+
const result = binding.mkdir(
1389+
pathModule.toNamespacedPath(path),
1390+
parseFileMode(mode, 'mode'),
1391+
recursive,
1392+
);
1393+
13931394
if (recursive) {
13941395
return result;
13951396
}

src/node_file.cc

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,30 +1757,11 @@ int MKDirpAsync(uv_loop_t* loop,
17571757
return err;
17581758
}
17591759

1760-
int CallMKDirpSync(Environment* env, const FunctionCallbackInfo<Value>& args,
1761-
FSReqWrapSync* req_wrap, const char* path, int mode) {
1762-
env->PrintSyncTrace();
1763-
int err = MKDirpSync(env->event_loop(), &req_wrap->req, path, mode,
1764-
nullptr);
1765-
if (err < 0) {
1766-
v8::Local<v8::Context> context = env->context();
1767-
v8::Local<v8::Object> ctx_obj = args[4].As<v8::Object>();
1768-
v8::Isolate* isolate = env->isolate();
1769-
ctx_obj->Set(context,
1770-
env->errno_string(),
1771-
v8::Integer::New(isolate, err)).Check();
1772-
ctx_obj->Set(context,
1773-
env->syscall_string(),
1774-
OneByteString(isolate, "mkdir")).Check();
1775-
}
1776-
return err;
1777-
}
1778-
17791760
static void MKDir(const FunctionCallbackInfo<Value>& args) {
17801761
Environment* env = Environment::GetCurrent(args);
17811762

17821763
const int argc = args.Length();
1783-
CHECK_GE(argc, 4);
1764+
CHECK_GE(argc, 3);
17841765

17851766
BufferValue path(env->isolate(), args[0]);
17861767
CHECK_NOT_NULL(*path);
@@ -1793,37 +1774,39 @@ static void MKDir(const FunctionCallbackInfo<Value>& args) {
17931774
CHECK(args[2]->IsBoolean());
17941775
bool mkdirp = args[2]->IsTrue();
17951776

1796-
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
1797-
if (req_wrap_async != nullptr) { // mkdir(path, mode, req)
1777+
if (argc > 3) { // mkdir(path, mode, recursive, req)
1778+
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
17981779
FS_ASYNC_TRACE_BEGIN1(
17991780
UV_FS_UNLINK, req_wrap_async, "path", TRACE_STR_COPY(*path))
18001781
AsyncCall(env, req_wrap_async, args, "mkdir", UTF8,
18011782
mkdirp ? AfterMkdirp : AfterNoArgs,
18021783
mkdirp ? MKDirpAsync : uv_fs_mkdir, *path, mode);
1803-
} else { // mkdir(path, mode, undefined, ctx)
1804-
CHECK_EQ(argc, 5);
1805-
FSReqWrapSync req_wrap_sync;
1784+
} else { // mkdir(path, mode, recursive)
1785+
FSReqWrapSync req_wrap_sync("mkdir", *path);
18061786
FS_SYNC_TRACE_BEGIN(mkdir);
18071787
if (mkdirp) {
1808-
int err = CallMKDirpSync(env, args, &req_wrap_sync, *path, mode);
1809-
if (err == 0 &&
1810-
!req_wrap_sync.continuation_data()->first_path().empty()) {
1788+
env->PrintSyncTrace();
1789+
int err = MKDirpSync(
1790+
env->event_loop(), &req_wrap_sync.req, *path, mode, nullptr);
1791+
if (is_uv_error(err)) {
1792+
env->ThrowUVException(err, "mkdir", nullptr, *path);
1793+
return;
1794+
}
1795+
if (!req_wrap_sync.continuation_data()->first_path().empty()) {
18111796
Local<Value> error;
18121797
std::string first_path(req_wrap_sync.continuation_data()->first_path());
18131798
FromNamespacedPath(&first_path);
18141799
MaybeLocal<Value> path = StringBytes::Encode(env->isolate(),
18151800
first_path.c_str(),
18161801
UTF8, &error);
18171802
if (path.IsEmpty()) {
1818-
Local<Object> ctx = args[4].As<Object>();
1819-
ctx->Set(env->context(), env->error_string(), error).Check();
1803+
env->isolate()->ThrowException(error);
18201804
return;
18211805
}
18221806
args.GetReturnValue().Set(path.ToLocalChecked());
18231807
}
18241808
} else {
1825-
SyncCall(env, args[4], &req_wrap_sync, "mkdir",
1826-
uv_fs_mkdir, *path, mode);
1809+
SyncCallAndThrowOnError(env, &req_wrap_sync, uv_fs_mkdir, *path, mode);
18271810
}
18281811
FS_SYNC_TRACE_END(mkdir);
18291812
}

typings/internalBinding/fs.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,9 @@ declare namespace InternalFSBinding {
142142
function mkdir(path: string, mode: number, recursive: boolean, req: FSReqCallback<void | string>): void;
143143
function mkdir(path: string, mode: number, recursive: true, req: FSReqCallback<string>): void;
144144
function mkdir(path: string, mode: number, recursive: false, req: FSReqCallback<void>): void;
145-
function mkdir(path: string, mode: number, recursive: boolean, req: undefined, ctx: FSSyncContext): void | string;
146-
function mkdir(path: string, mode: number, recursive: true, req: undefined, ctx: FSSyncContext): string;
147-
function mkdir(path: string, mode: number, recursive: false, req: undefined, ctx: FSSyncContext): void;
145+
function mkdir(path: string, mode: number, recursive: boolean): void | string;
146+
function mkdir(path: string, mode: number, recursive: true): string;
147+
function mkdir(path: string, mode: number, recursive: false): void;
148148
function mkdir(path: string, mode: number, recursive: boolean, usePromises: typeof kUsePromises): Promise<void | string>;
149149
function mkdir(path: string, mode: number, recursive: true, usePromises: typeof kUsePromises): Promise<string>;
150150
function mkdir(path: string, mode: number, recursive: false, usePromises: typeof kUsePromises): Promise<void>;

0 commit comments

Comments
 (0)