Skip to content

Commit

Permalink
fs: improve error performance of mkdtempSync
Browse files Browse the repository at this point in the history
PR-URL: #49962
Refs: nodejs/performance#106
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
  • Loading branch information
anonrig authored and targos committed Oct 23, 2023
1 parent 81f1527 commit 2759878
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
6 changes: 1 addition & 5 deletions lib/fs.js
Expand Up @@ -2966,11 +2966,7 @@ function mkdtempSync(prefix, options) {
path = Buffer.concat([prefix, Buffer.from('XXXXXX')]);
}

const ctx = { path };
const result = binding.mkdtemp(path, options.encoding,
undefined, ctx);
handleErrorFromBinding(ctx);
return result;
return binding.mkdtemp(path, options.encoding);
}

/**
Expand Down
23 changes: 11 additions & 12 deletions src/node_file.cc
Expand Up @@ -2760,27 +2760,26 @@ static void Mkdtemp(const FunctionCallbackInfo<Value>& args) {

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

FSReqBase* req_wrap_async = GetReqWrap(args, 2);
if (req_wrap_async != nullptr) { // mkdtemp(tmpl, encoding, req)
if (argc > 2) { // mkdtemp(tmpl, encoding, req)
FSReqBase* req_wrap_async = GetReqWrap(args, 2);
FS_ASYNC_TRACE_BEGIN1(
UV_FS_MKDTEMP, req_wrap_async, "path", TRACE_STR_COPY(*tmpl))
AsyncCall(env, req_wrap_async, args, "mkdtemp", encoding, AfterStringPath,
uv_fs_mkdtemp, *tmpl);
} else { // mkdtemp(tmpl, encoding, undefined, ctx)
CHECK_EQ(argc, 4);
FSReqWrapSync req_wrap_sync;
} else { // mkdtemp(tmpl, encoding)
FSReqWrapSync req_wrap_sync("mkdtemp", *tmpl);
FS_SYNC_TRACE_BEGIN(mkdtemp);
SyncCall(env, args[3], &req_wrap_sync, "mkdtemp",
uv_fs_mkdtemp, *tmpl);
int result =
SyncCallAndThrowOnError(env, &req_wrap_sync, uv_fs_mkdtemp, *tmpl);
FS_SYNC_TRACE_END(mkdtemp);
const char* path = req_wrap_sync.req.path;

if (is_uv_error(result)) {
return;
}
Local<Value> error;
MaybeLocal<Value> rc =
StringBytes::Encode(isolate, path, encoding, &error);
StringBytes::Encode(isolate, req_wrap_sync.req.path, encoding, &error);
if (rc.IsEmpty()) {
Local<Object> ctx = args[3].As<Object>();
ctx->Set(env->context(), env->error_string(), error).Check();
env->isolate()->ThrowException(error);
return;
}
args.GetReturnValue().Set(rc.ToLocalChecked());
Expand Down
1 change: 1 addition & 0 deletions typings/internalBinding/fs.d.ts
Expand Up @@ -140,6 +140,7 @@ declare namespace InternalFSBinding {
function mkdtemp(prefix: string, encoding: unknown, req: FSReqCallback<string>): void;
function mkdtemp(prefix: string, encoding: unknown, req: undefined, ctx: FSSyncContext): string;
function mkdtemp(prefix: string, encoding: unknown, usePromises: typeof kUsePromises): Promise<string>;
function mkdtemp(prefix: string, encoding: unknown): string;

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

0 comments on commit 2759878

Please sign in to comment.