fs: improve error performance of `readlinkSync` · nodejs/node@bc6f279 · GitHub
Skip to content

Commit bc6f279

Browse files
anonrigtargos
authored andcommitted
fs: improve error performance of readlinkSync
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>
1 parent 2759878 commit bc6f279

4 files changed

Lines changed: 69 additions & 19 deletions

File tree

benchmark/fs/bench-readlinkSync.js

Lines changed: 54 additions & 0 deletions

lib/fs.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1728,11 +1728,10 @@ function readlink(path, options, callback) {
17281728
function readlinkSync(path, options) {
17291729
options = getOptions(options);
17301730
path = getValidatedPath(path, 'oldPath');
1731-
const ctx = { path };
1732-
const result = binding.readlink(pathModule.toNamespacedPath(path),
1733-
options.encoding, undefined, ctx);
1734-
handleErrorFromBinding(ctx);
1735-
return result;
1731+
return binding.readlink(
1732+
pathModule.toNamespacedPath(path),
1733+
options.encoding,
1734+
);
17361735
}
17371736

17381737
/**
@@ -2714,10 +2713,8 @@ function realpathSync(p, options) {
27142713
}
27152714
}
27162715
if (linkTarget === null) {
2717-
const ctx = { path: base };
27182716
binding.stat(baseLong, false, undefined, true);
2719-
linkTarget = binding.readlink(baseLong, undefined, undefined, ctx);
2720-
handleErrorFromBinding(ctx);
2717+
linkTarget = binding.readlink(baseLong, undefined);
27212718
}
27222719
resolvedLink = pathModule.resolve(previous, linkTarget);
27232720

src/node_file.cc

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,7 +1383,7 @@ static void ReadLink(const FunctionCallbackInfo<Value>& args) {
13831383
Isolate* isolate = env->isolate();
13841384

13851385
const int argc = args.Length();
1386-
CHECK_GE(argc, 3);
1386+
CHECK_GE(argc, 2);
13871387

13881388
BufferValue path(isolate, args[0]);
13891389
CHECK_NOT_NULL(*path);
@@ -1392,21 +1392,20 @@ static void ReadLink(const FunctionCallbackInfo<Value>& args) {
13921392

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

1395-
FSReqBase* req_wrap_async = GetReqWrap(args, 2);
1396-
if (req_wrap_async != nullptr) { // readlink(path, encoding, req)
1395+
if (argc > 2) { // readlink(path, encoding, req)
1396+
FSReqBase* req_wrap_async = GetReqWrap(args, 2);
13971397
FS_ASYNC_TRACE_BEGIN1(
13981398
UV_FS_READLINK, req_wrap_async, "path", TRACE_STR_COPY(*path))
13991399
AsyncCall(env, req_wrap_async, args, "readlink", encoding, AfterStringPtr,
14001400
uv_fs_readlink, *path);
1401-
} else {
1402-
CHECK_EQ(argc, 4);
1403-
FSReqWrapSync req_wrap_sync;
1401+
} else { // readlink(path, encoding)
1402+
FSReqWrapSync req_wrap_sync("readlink", *path);
14041403
FS_SYNC_TRACE_BEGIN(readlink);
1405-
int err = SyncCall(env, args[3], &req_wrap_sync, "readlink",
1406-
uv_fs_readlink, *path);
1404+
int err =
1405+
SyncCallAndThrowOnError(env, &req_wrap_sync, uv_fs_readlink, *path);
14071406
FS_SYNC_TRACE_END(readlink);
14081407
if (err < 0) {
1409-
return; // syscall failed, no need to continue, error info is in ctx
1408+
return;
14101409
}
14111410
const char* link_path = static_cast<const char*>(req_wrap_sync.req.ptr);
14121411

@@ -1416,8 +1415,7 @@ static void ReadLink(const FunctionCallbackInfo<Value>& args) {
14161415
encoding,
14171416
&error);
14181417
if (rc.IsEmpty()) {
1419-
Local<Object> ctx = args[3].As<Object>();
1420-
ctx->Set(env->context(), env->error_string(), error).Check();
1418+
env->isolate()->ThrowException(error);
14211419
return;
14221420
}
14231421

typings/internalBinding/fs.d.ts

Lines changed: 1 addition & 0 deletions

0 commit comments

Comments
 (0)