Skip to content

Commit

Permalink
fs: improve error performance of realpathSync
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 6eeaa02 commit c5ff000
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
8 changes: 4 additions & 4 deletions lib/fs.js
Expand Up @@ -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,
);
};

/**
Expand Down
18 changes: 8 additions & 10 deletions src/node_file.cc
Expand Up @@ -1828,28 +1828,27 @@ static void RealPath(const FunctionCallbackInfo<Value>& 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<const char*>(req_wrap_sync.req.ptr);
Expand All @@ -1860,8 +1859,7 @@ static void RealPath(const FunctionCallbackInfo<Value>& args) {
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;
}

Expand Down
1 change: 1 addition & 0 deletions typings/internalBinding/fs.d.ts
Expand Up @@ -184,6 +184,7 @@ declare namespace InternalFSBinding {
function realpath(path: StringOrBuffer, encoding: unknown, req: FSReqCallback<string | Buffer>): void;
function realpath(path: StringOrBuffer, encoding: unknown, req: undefined, ctx: FSSyncContext): string | Buffer;
function realpath(path: StringOrBuffer, encoding: unknown, usePromises: typeof kUsePromises): Promise<string | Buffer>;
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;
Expand Down

0 comments on commit c5ff000

Please sign in to comment.