Skip to content

Commit 39f31a3

Browse files
anonrigtargos
authored andcommitted
fs: improve error performance of renameSync
PR-URL: #49962 Refs: nodejs/performance#106 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Stephen Belanger <[email protected]> Reviewed-By: Vinícius Lourenço Claro Cardoso <[email protected]>
1 parent 228c87f commit 39f31a3

File tree

4 files changed

+61
-12
lines changed

4 files changed

+61
-12
lines changed

benchmark/fs/bench-renameSync.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const fs = require('fs');
5+
const assert = require('assert');
6+
const tmpdir = require('../../test/common/tmpdir');
7+
8+
const bench = common.createBenchmark(main, {
9+
type: ['invalid', 'valid'],
10+
n: [2e3],
11+
});
12+
13+
function main({ n, type }) {
14+
switch (type) {
15+
case 'invalid': {
16+
let hasError = false;
17+
bench.start();
18+
for (let i = 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 (let i = 0; i < n; i++) {
32+
fs.writeFileSync(tmpdir.resolve(`.existing-file-${i}`), 'bench', 'utf8');
33+
}
34+
35+
bench.start();
36+
for (let i = 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+
throw new Error('Invalid type');
48+
}
49+
}

lib/fs.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1032,10 +1032,10 @@ function rename(oldPath, newPath, callback) {
10321032
function renameSync(oldPath, newPath) {
10331033
oldPath = getValidatedPath(oldPath, 'oldPath');
10341034
newPath = getValidatedPath(newPath, 'newPath');
1035-
const ctx = { path: oldPath, dest: newPath };
1036-
binding.rename(pathModule.toNamespacedPath(oldPath),
1037-
pathModule.toNamespacedPath(newPath), undefined, ctx);
1038-
handleErrorFromBinding(ctx);
1035+
binding.rename(
1036+
pathModule.toNamespacedPath(oldPath),
1037+
pathModule.toNamespacedPath(newPath),
1038+
);
10391039
}
10401040

10411041
/**

src/node_file.cc

+7-8
Original file line numberDiff line numberDiff line change
@@ -1467,7 +1467,7 @@ static void Rename(const FunctionCallbackInfo<Value>& args) {
14671467
Isolate* isolate = env->isolate();
14681468

14691469
const int argc = args.Length();
1470-
CHECK_GE(argc, 3);
1470+
CHECK_GE(argc, 2);
14711471

14721472
BufferValue old_path(isolate, args[0]);
14731473
CHECK_NOT_NULL(*old_path);
@@ -1484,8 +1484,8 @@ static void Rename(const FunctionCallbackInfo<Value>& args) {
14841484
permission::PermissionScope::kFileSystemWrite,
14851485
new_path.ToStringView());
14861486

1487-
FSReqBase* req_wrap_async = GetReqWrap(args, 2);
1488-
if (req_wrap_async != nullptr) {
1487+
if (argc > 2) { // rename(old_path, new_path, req)
1488+
FSReqBase* req_wrap_async = GetReqWrap(args, 2);
14891489
FS_ASYNC_TRACE_BEGIN2(UV_FS_RENAME,
14901490
req_wrap_async,
14911491
"old_path",
@@ -1495,12 +1495,11 @@ static void Rename(const FunctionCallbackInfo<Value>& args) {
14951495
AsyncDestCall(env, req_wrap_async, args, "rename", *new_path,
14961496
new_path.length(), UTF8, AfterNoArgs, uv_fs_rename,
14971497
*old_path, *new_path);
1498-
} else {
1499-
CHECK_EQ(argc, 4);
1500-
FSReqWrapSync req_wrap_sync;
1498+
} else { // rename(old_path, new_path)
1499+
FSReqWrapSync req_wrap_sync("rename", *old_path, *new_path);
15011500
FS_SYNC_TRACE_BEGIN(rename);
1502-
SyncCall(env, args[3], &req_wrap_sync, "rename", uv_fs_rename,
1503-
*old_path, *new_path);
1501+
SyncCallAndThrowOnError(
1502+
env, &req_wrap_sync, uv_fs_rename, *old_path, *new_path);
15041503
FS_SYNC_TRACE_END(rename);
15051504
}
15061505
}

typings/internalBinding/fs.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ declare namespace InternalFSBinding {
183183
function rename(oldPath: string, newPath: string, req: FSReqCallback): void;
184184
function rename(oldPath: string, newPath: string, req: undefined, ctx: FSSyncContext): void;
185185
function rename(oldPath: string, newPath: string, usePromises: typeof kUsePromises): Promise<void>;
186+
function rename(oldPath: string, newPath: string): void;
186187

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

0 commit comments

Comments
 (0)