mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
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:
committed by
Node.js GitHub Bot
parent
dbd0ffa125
commit
a7e40f5ce5
49
benchmark/fs/bench-renameSync.js
Normal file
49
benchmark/fs/bench-renameSync.js
Normal 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');
|
||||
}
|
||||
}
|
||||
@@ -1034,10 +1034,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),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1432,7 +1432,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);
|
||||
@@ -1449,8 +1449,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",
|
||||
@@ -1460,12 +1460,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
typings/internalBinding/fs.d.ts
vendored
1
typings/internalBinding/fs.d.ts
vendored
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user