From c5ff000cb1a2235d5ca64374023176c416c5800d Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Sat, 30 Sep 2023 20:18:32 -0400 Subject: [PATCH] fs: improve error performance of `realpathSync` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/49962 Refs: https://github.com/nodejs/performance/issues/106 Reviewed-By: Matteo Collina Reviewed-By: Stephen Belanger Reviewed-By: Vinícius Lourenço Claro Cardoso --- lib/fs.js | 8 ++++---- src/node_file.cc | 18 ++++++++---------- typings/internalBinding/fs.d.ts | 1 + 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index d4bf1d80fc5c7d..7324c2c2036a08 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -2752,10 +2752,10 @@ function realpathSync(p, options) { realpathSync.native = (path, options) => { options = getOptions(options); path = getValidatedPath(path); - const ctx = { path }; - const result = binding.realpath(pathModule.toNamespacedPath(path), options.encoding, undefined, ctx); - handleErrorFromBinding(ctx); - return result; + return binding.realpath( + pathModule.toNamespacedPath(path), + options.encoding, + ); }; /** diff --git a/src/node_file.cc b/src/node_file.cc index e892b8a8659f9c..a9b9231009c144 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -1828,28 +1828,27 @@ static void RealPath(const FunctionCallbackInfo& args) { Isolate* isolate = env->isolate(); const int argc = args.Length(); - CHECK_GE(argc, 3); + CHECK_GE(argc, 2); BufferValue path(isolate, args[0]); CHECK_NOT_NULL(*path); const enum encoding encoding = ParseEncoding(isolate, args[1], UTF8); - FSReqBase* req_wrap_async = GetReqWrap(args, 2); - if (req_wrap_async != nullptr) { // realpath(path, encoding, req) + if (argc > 2) { // realpath(path, encoding, req) + FSReqBase* req_wrap_async = GetReqWrap(args, 2); FS_ASYNC_TRACE_BEGIN1( UV_FS_REALPATH, req_wrap_async, "path", TRACE_STR_COPY(*path)) AsyncCall(env, req_wrap_async, args, "realpath", encoding, AfterStringPtr, uv_fs_realpath, *path); } else { // realpath(path, encoding, undefined, ctx) - CHECK_EQ(argc, 4); - FSReqWrapSync req_wrap_sync; + FSReqWrapSync req_wrap_sync("realpath", *path); FS_SYNC_TRACE_BEGIN(realpath); - int err = SyncCall(env, args[3], &req_wrap_sync, "realpath", - uv_fs_realpath, *path); + int err = + SyncCallAndThrowOnError(env, &req_wrap_sync, uv_fs_realpath, *path); FS_SYNC_TRACE_END(realpath); if (err < 0) { - return; // syscall failed, no need to continue, error info is in ctx + return; } const char* link_path = static_cast(req_wrap_sync.req.ptr); @@ -1860,8 +1859,7 @@ static void RealPath(const FunctionCallbackInfo& args) { encoding, &error); if (rc.IsEmpty()) { - Local ctx = args[3].As(); - ctx->Set(env->context(), env->error_string(), error).Check(); + env->isolate()->ThrowException(error); return; } diff --git a/typings/internalBinding/fs.d.ts b/typings/internalBinding/fs.d.ts index f746ca582327d0..6bed01519fa2b0 100644 --- a/typings/internalBinding/fs.d.ts +++ b/typings/internalBinding/fs.d.ts @@ -184,6 +184,7 @@ declare namespace InternalFSBinding { function realpath(path: StringOrBuffer, encoding: unknown, req: FSReqCallback): void; function realpath(path: StringOrBuffer, encoding: unknown, req: undefined, ctx: FSSyncContext): string | Buffer; function realpath(path: StringOrBuffer, encoding: unknown, usePromises: typeof kUsePromises): Promise; + function realpath(path: StringOrBuffer, encoding: unknown): StringOrBuffer; function rename(oldPath: string, newPath: string, req: FSReqCallback): void; function rename(oldPath: string, newPath: string, req: undefined, ctx: FSSyncContext): void;