fs: improve error performance of `renameSync` · nodejs/node@610036c · GitHub
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

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
function renameSync(oldPath, newPath) {
10351035
oldPath = getValidatedPath(oldPath, 'oldPath');
10361036
newPath = getValidatedPath(newPath, 'newPath');
1037-
const ctx = { 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
const int 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

0 commit comments

Comments
 (0)