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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
'use strict';
2+
3+
constcommon=require('../common');
4+
constfs=require('fs');
5+
constassert=require('assert');
6+
consttmpdir=require('../../test/common/tmpdir');
7+
8+
if(process.platform==='win32'){
9+
console.log('Skipping: Windows does not play well with `symlinkSync`');
10+
process.exit(0);
11+
}
12+
13+
constbench=common.createBenchmark(main,{
14+
type: ['valid','invalid'],
15+
n: [1e3],
16+
});
17+
18+
functionmain({ n, type }){
19+
switch(type){
20+
case'valid': {
21+
tmpdir.refresh();
22+
consttmpfile=tmpdir.resolve(`.readlink-file-${process.pid}`);
23+
fs.writeFileSync(tmpfile,'data','utf8');
24+
letreturnValue;
25+
for(leti=0;i<n;i++){
26+
fs.symlinkSync(tmpfile,tmpdir.resolve(`.readlink-sync-${i}`),'file');
27+
}
28+
bench.start();
29+
for(leti=0;i<n;i++){
30+
returnValue=fs.readlinkSync(tmpdir.resolve(`.readlink-sync-${i}`),{encoding: 'utf8'});
31+
}
32+
bench.end(n);
33+
assert(returnValue);
34+
break;
35+
}
36+
37+
case'invalid': {
38+
lethasError=false;
39+
bench.start();
40+
for(leti=0;i<n;i++){
41+
try{
42+
fs.readlinkSync(tmpdir.resolve('.non-existing-file-for-readlinkSync'));
43+
}catch{
44+
hasError=true;
45+
}
46+
}
47+
bench.end(n);
48+
assert(hasError);
49+
break;
50+
}
51+
default:
52+
newError('Invalid type');
53+
}
54+
}

‎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
functionreadlinkSync(path,options){
17291729
options=getOptions(options);
17301730
path=getValidatedPath(path,'oldPath');
1731-
constctx={ path };
1732-
constresult=binding.readlink(pathModule.toNamespacedPath(path),
1733-
options.encoding,undefined,ctx);
1734-
handleErrorFromBinding(ctx);
1735-
returnresult;
1731+
returnbinding.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-
constctx={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
constint 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
constenum 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
constchar* link_path = static_cast<constchar*>(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
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ declare namespace InternalFSBinding {
178178
functionreadlink(path: StringOrBuffer,encoding: unknown,req: FSReqCallback<string|Buffer>): void;
179179
functionreadlink(path: StringOrBuffer,encoding: unknown,req: undefined,ctx: FSSyncContext): string|Buffer;
180180
functionreadlink(path: StringOrBuffer,encoding: unknown,usePromises: typeofkUsePromises): Promise<string|Buffer>;
181+
functionreadlink(path: StringOrBuffer,encoding: unknown): StringOrBuffer;
181182

182183
functionrealpath(path: StringOrBuffer,encoding: unknown,req: FSReqCallback<string|Buffer>): void;
183184
functionrealpath(path: StringOrBuffer,encoding: unknown,req: undefined,ctx: FSSyncContext): string|Buffer;

0 commit comments

Comments
 (0)