Skip to content

Commit 2759878

Browse files
anonrigtargos
authored andcommitted
fs: improve error performance of mkdtempSync
PR-URL: #49962 Refs: nodejs/performance#106 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Stephen Belanger <[email protected]> Reviewed-By: Vinícius Lourenço Claro Cardoso <[email protected]>
1 parent 81f1527 commit 2759878

File tree

3 files changed

+13
-17
lines changed

3 files changed

+13
-17
lines changed

lib/fs.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -2966,11 +2966,7 @@ function mkdtempSync(prefix, options) {
29662966
path = Buffer.concat([prefix, Buffer.from('XXXXXX')]);
29672967
}
29682968

2969-
const ctx = { path };
2970-
const result = binding.mkdtemp(path, options.encoding,
2971-
undefined, ctx);
2972-
handleErrorFromBinding(ctx);
2973-
return result;
2969+
return binding.mkdtemp(path, options.encoding);
29742970
}
29752971

29762972
/**

src/node_file.cc

+11-12
Original file line numberDiff line numberDiff line change
@@ -2760,27 +2760,26 @@ static void Mkdtemp(const FunctionCallbackInfo<Value>& args) {
27602760

27612761
const enum encoding encoding = ParseEncoding(isolate, args[1], UTF8);
27622762

2763-
FSReqBase* req_wrap_async = GetReqWrap(args, 2);
2764-
if (req_wrap_async != nullptr) { // mkdtemp(tmpl, encoding, req)
2763+
if (argc > 2) { // mkdtemp(tmpl, encoding, req)
2764+
FSReqBase* req_wrap_async = GetReqWrap(args, 2);
27652765
FS_ASYNC_TRACE_BEGIN1(
27662766
UV_FS_MKDTEMP, req_wrap_async, "path", TRACE_STR_COPY(*tmpl))
27672767
AsyncCall(env, req_wrap_async, args, "mkdtemp", encoding, AfterStringPath,
27682768
uv_fs_mkdtemp, *tmpl);
2769-
} else { // mkdtemp(tmpl, encoding, undefined, ctx)
2770-
CHECK_EQ(argc, 4);
2771-
FSReqWrapSync req_wrap_sync;
2769+
} else { // mkdtemp(tmpl, encoding)
2770+
FSReqWrapSync req_wrap_sync("mkdtemp", *tmpl);
27722771
FS_SYNC_TRACE_BEGIN(mkdtemp);
2773-
SyncCall(env, args[3], &req_wrap_sync, "mkdtemp",
2774-
uv_fs_mkdtemp, *tmpl);
2772+
int result =
2773+
SyncCallAndThrowOnError(env, &req_wrap_sync, uv_fs_mkdtemp, *tmpl);
27752774
FS_SYNC_TRACE_END(mkdtemp);
2776-
const char* path = req_wrap_sync.req.path;
2777-
2775+
if (is_uv_error(result)) {
2776+
return;
2777+
}
27782778
Local<Value> error;
27792779
MaybeLocal<Value> rc =
2780-
StringBytes::Encode(isolate, path, encoding, &error);
2780+
StringBytes::Encode(isolate, req_wrap_sync.req.path, encoding, &error);
27812781
if (rc.IsEmpty()) {
2782-
Local<Object> ctx = args[3].As<Object>();
2783-
ctx->Set(env->context(), env->error_string(), error).Check();
2782+
env->isolate()->ThrowException(error);
27842783
return;
27852784
}
27862785
args.GetReturnValue().Set(rc.ToLocalChecked());

typings/internalBinding/fs.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ declare namespace InternalFSBinding {
140140
function mkdtemp(prefix: string, encoding: unknown, req: FSReqCallback<string>): void;
141141
function mkdtemp(prefix: string, encoding: unknown, req: undefined, ctx: FSSyncContext): string;
142142
function mkdtemp(prefix: string, encoding: unknown, usePromises: typeof kUsePromises): Promise<string>;
143+
function mkdtemp(prefix: string, encoding: unknown): string;
143144

144145
function mkdir(path: string, mode: number, recursive: boolean, req: FSReqCallback<void | string>): void;
145146
function mkdir(path: string, mode: number, recursive: true, req: FSReqCallback<string>): void;

0 commit comments

Comments
 (0)