fs: improve error performance of renameSync

PR-URL: https://github.com/nodejs/node/pull/49962
Refs: https://github.com/nodejs/performance/issues/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>
This commit is contained in:
Yagiz Nizipli authored and Michaël Zasso committed 2023-11-11 20:29:13 +01:00
1 parent 228c87f329
commit 39f31a38cf
4 files changed
+61 -12

No files matched your search

+49
View File
@@ -0,0 +1,49 @@
'use strict';
const common = require('../common');
const fs = require('fs');
const assert = require('assert');
const tmpdir = require('../../test/common/tmpdir');
const bench = common.createBenchmark(main, {
type: ['invalid', 'valid'],
n: [2e3],
});
function main({ n, type }) {
switch (type) {
case 'invalid': {
let hasError = false;
bench.start();
for (let i = 0; i < n; i++) {
try {
fs.renameSync(tmpdir.resolve(`.non-existing-file-${i}`), tmpdir.resolve(`.new-file-${i}`));
} catch {
hasError = true;
}
}
bench.end(n);
assert(hasError);
break;
}
case 'valid': {
tmpdir.refresh();
for (let i = 0; i < n; i++) {
fs.writeFileSync(tmpdir.resolve(`.existing-file-${i}`), 'bench', 'utf8');
}
bench.start();
for (let i = 0; i < n; i++) {
fs.renameSync(
tmpdir.resolve(`.existing-file-${i}`),
tmpdir.resolve(`.new-existing-file-${i}`),
);
}
bench.end(n);
break;
}
default:
throw new Error('Invalid type');
}
}
+4 -4
View File
@@ -1032,10 +1032,10 @@ function rename(oldPath, newPath, callback) {
function renameSync(oldPath, newPath) {
oldPath = getValidatedPath(oldPath, 'oldPath');
newPath = getValidatedPath(newPath, 'newPath');
const ctx = { path: oldPath, dest: newPath };
binding.rename(pathModule.toNamespacedPath(oldPath),
pathModule.toNamespacedPath(newPath), undefined, ctx);
handleErrorFromBinding(ctx);
binding.rename(
pathModule.toNamespacedPath(oldPath),
pathModule.toNamespacedPath(newPath),
);
}
/**
+7 -8
View File
@@ -1467,7 +1467,7 @@ static void Rename(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = env->isolate();
const int argc = args.Length();
CHECK_GE(argc, 3);
CHECK_GE(argc, 2);
BufferValue old_path(isolate, args[0]);
CHECK_NOT_NULL(*old_path);
@@ -1484,8 +1484,8 @@ static void Rename(const FunctionCallbackInfo<Value>& args) {
permission::PermissionScope::kFileSystemWrite,
new_path.ToStringView());
FSReqBase* req_wrap_async = GetReqWrap(args, 2);
if (req_wrap_async != nullptr) {
if (argc > 2) { // rename(old_path, new_path, req)
FSReqBase* req_wrap_async = GetReqWrap(args, 2);
FS_ASYNC_TRACE_BEGIN2(UV_FS_RENAME,
req_wrap_async,
"old_path",
@@ -1495,12 +1495,11 @@ static void Rename(const FunctionCallbackInfo<Value>& args) {
AsyncDestCall(env, req_wrap_async, args, "rename", *new_path,
new_path.length(), UTF8, AfterNoArgs, uv_fs_rename,
*old_path, *new_path);
} else {
CHECK_EQ(argc, 4);
FSReqWrapSync req_wrap_sync;
} else { // rename(old_path, new_path)
FSReqWrapSync req_wrap_sync("rename", *old_path, *new_path);
FS_SYNC_TRACE_BEGIN(rename);
SyncCall(env, args[3], &req_wrap_sync, "rename", uv_fs_rename,
*old_path, *new_path);
SyncCallAndThrowOnError(
env, &req_wrap_sync, uv_fs_rename, *old_path, *new_path);
FS_SYNC_TRACE_END(rename);
}
}
+1
View File
@@ -183,6 +183,7 @@ declare namespace InternalFSBinding {
function rename(oldPath: string, newPath: string, req: FSReqCallback): void;
function rename(oldPath: string, newPath: string, req: undefined, ctx: FSSyncContext): void;
function rename(oldPath: string, newPath: string, usePromises: typeof kUsePromises): Promise<void>;
function rename(oldPath: string, newPath: string): void;
function rmdir(path: string, req: FSReqCallback): void;
function rmdir(path: string, req: undefined, ctx: FSSyncContext): void;