Skip to content

Commit 610036c

Browse files
anonrigtargos
authored andcommitted
fs: improve error performance of renameSync
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 a38d131 commit 610036c

4 files changed

Lines changed: 61 additions & 12 deletions

File tree

‎benchmark/fs/bench-renameSync.js‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'use strict';
2+
3+
constcommon=require('../common');
4+
constfs=require('fs');
5+
constassert=require('assert');
6+
consttmpdir=require('../../test/common/tmpdir');
7+
8+
constbench=common.createBenchmark(main,{
9+
type: ['invalid','valid'],
10+
n: [2e3],
11+
});
12+
13+
functionmain({ n, type }){
14+
switch(type){
15+
case'invalid': {
16+
lethasError=false;
17+
bench.start();
18+
for(leti=0;i<n;i++){
19+
try{
20+
fs.renameSync(tmpdir.resolve(`.non-existing-file-${i}`),tmpdir.resolve(`.new-file-${i}`));
21+
}catch{
22+
hasError=true;
23+
}
24+
}
25+
bench.end(n);
26+
assert(hasError);
27+
break;
28+
}
29+
case'valid': {
30+
tmpdir.refresh();
31+
for(leti=0;i<n;i++){
32+
fs.writeFileSync(tmpdir.resolve(`.existing-file-${i}`),'bench','utf8');
33+
}
34+
35+
bench.start();
36+
for(leti=0;i<n;i++){
37+
fs.renameSync(
38+
tmpdir.resolve(`.existing-file-${i}`),
39+
tmpdir.resolve(`.new-existing-file-${i}`),
40+
);
41+
}
42+
43+
bench.end(n);
44+
break;
45+
}
46+
default:
47+
thrownewError('Invalid type');
48+
}
49+
}

‎lib/fs.js‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,10 +1034,10 @@ function rename(oldPath, newPath, callback) {
10341034
functionrenameSync(oldPath,newPath){
10351035
oldPath=getValidatedPath(oldPath,'oldPath');
10361036
newPath=getValidatedPath(newPath,'newPath');
1037-
constctx={path: oldPath,dest: newPath};
1038-
binding.rename(pathModule.toNamespacedPath(oldPath),
1039-
pathModule.toNamespacedPath(newPath),undefined,ctx);
1040-
handleErrorFromBinding(ctx);
1037+
binding.rename(
1038+
pathModule.toNamespacedPath(oldPath),
1039+
pathModule.toNamespacedPath(newPath),
1040+
);
10411041
}
10421042

10431043
/**

‎src/node_file.cc‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,7 +1432,7 @@ static void Rename(const FunctionCallbackInfo<Value>& args) {
14321432
Isolate* isolate = env->isolate();
14331433

14341434
constint argc = args.Length();
1435-
CHECK_GE(argc, 3);
1435+
CHECK_GE(argc, 2);
14361436

14371437
BufferValue old_path(isolate, args[0]);
14381438
CHECK_NOT_NULL(*old_path);
@@ -1449,8 +1449,8 @@ static void Rename(const FunctionCallbackInfo<Value>& args) {
14491449
permission::PermissionScope::kFileSystemWrite,
14501450
new_path.ToStringView());
14511451

1452-
FSReqBase* req_wrap_async = GetReqWrap(args, 2);
1453-
if (req_wrap_async != nullptr) {
1452+
if (argc > 2) { // rename(old_path, new_path, req)
1453+
FSReqBase* req_wrap_async = GetReqWrap(args, 2);
14541454
FS_ASYNC_TRACE_BEGIN2(UV_FS_RENAME,
14551455
req_wrap_async,
14561456
"old_path",
@@ -1460,12 +1460,11 @@ static void Rename(const FunctionCallbackInfo<Value>& args) {
14601460
AsyncDestCall(env, req_wrap_async, args, "rename", *new_path,
14611461
new_path.length(), UTF8, AfterNoArgs, uv_fs_rename,
14621462
*old_path, *new_path);
1463-
} else {
1464-
CHECK_EQ(argc, 4);
1465-
FSReqWrapSync req_wrap_sync;
1463+
} else { // rename(old_path, new_path)
1464+
FSReqWrapSync req_wrap_sync("rename", *old_path, *new_path);
14661465
FS_SYNC_TRACE_BEGIN(rename);
1467-
SyncCall(env, args[3], &req_wrap_sync, "rename", uv_fs_rename,
1468-
*old_path, *new_path);
1466+
SyncCallAndThrowOnError(
1467+
env, &req_wrap_sync, uv_fs_rename, *old_path, *new_path);
14691468
FS_SYNC_TRACE_END(rename);
14701469
}
14711470
}

‎typings/internalBinding/fs.d.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ declare namespace InternalFSBinding {
183183
functionrename(oldPath: string,newPath: string,req: FSReqCallback): void;
184184
functionrename(oldPath: string,newPath: string,req: undefined,ctx: FSSyncContext): void;
185185
functionrename(oldPath: string,newPath: string,usePromises: typeofkUsePromises): Promise<void>;
186+
functionrename(oldPath: string,newPath: string): void;
186187

187188
functionrmdir(path: string,req: FSReqCallback): void;
188189
functionrmdir(path: string,req: undefined,ctx: FSSyncContext): void;

0 commit comments

Comments
 (0)