fs: improve error performance of chownSync

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
2023-09-29 13:10:04 -04:00
committed by Node.js GitHub Bot
parent a7e40f5ce5
commit 1635366ad4
4 changed files with 65 additions and 10 deletions

View File

@@ -0,0 +1,54 @@
'use strict';
const common = require('../common');
const fs = require('fs');
const assert = require('assert');
const tmpdir = require('../../test/common/tmpdir');
if (process.platform === 'win32') {
console.log('Skipping: Windows does not have `getuid` or `getgid`');
process.exit(0);
}
const bench = common.createBenchmark(main, {
type: ['existing', 'non-existing'],
method: ['chownSync', 'lchownSync'],
n: [1e4],
});
function main({ n, type, method }) {
const uid = process.getuid();
const gid = process.getgid();
const fsMethod = fs[method];
switch (type) {
case 'existing': {
tmpdir.refresh();
const tmpfile = tmpdir.resolve(`.existing-file-${process.pid}`);
fs.writeFileSync(tmpfile, 'this-is-for-a-benchmark', 'utf8');
bench.start();
for (let i = 0; i < n; i++) {
fsMethod(tmpfile, uid, gid);
}
bench.end(n);
break;
}
case 'non-existing': {
const path = tmpdir.resolve(`.non-existing-file-${Date.now()}`);
let hasError = false;
bench.start();
for (let i = 0; i < n; i++) {
try {
fs[method](path, uid, gid);
} catch {
hasError = true;
}
}
bench.end(n);
assert(hasError);
break;
}
default:
new Error('Invalid type');
}
}

View File

@@ -2089,9 +2089,11 @@ function chownSync(path, uid, gid) {
path = getValidatedPath(path);
validateInteger(uid, 'uid', -1, kMaxUserId);
validateInteger(gid, 'gid', -1, kMaxUserId);
const ctx = { path };
binding.chown(pathModule.toNamespacedPath(path), uid, gid, undefined, ctx);
handleErrorFromBinding(ctx);
binding.chown(
pathModule.toNamespacedPath(path),
uid,
gid,
);
}
/**

View File

@@ -2572,18 +2572,16 @@ static void Chown(const FunctionCallbackInfo<Value>& args) {
CHECK(IsSafeJsInt(args[2]));
const uv_gid_t gid = static_cast<uv_gid_t>(args[2].As<Integer>()->Value());
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
if (req_wrap_async != nullptr) { // chown(path, uid, gid, req)
if (argc > 3) { // chown(path, uid, gid, req)
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
FS_ASYNC_TRACE_BEGIN1(
UV_FS_CHOWN, req_wrap_async, "path", TRACE_STR_COPY(*path))
AsyncCall(env, req_wrap_async, args, "chown", UTF8, AfterNoArgs,
uv_fs_chown, *path, uid, gid);
} else { // chown(path, uid, gid, undefined, ctx)
CHECK_EQ(argc, 5);
FSReqWrapSync req_wrap_sync;
} else { // chown(path, uid, gid)
FSReqWrapSync req_wrap_sync("chown", *path);
FS_SYNC_TRACE_BEGIN(chown);
SyncCall(env, args[4], &req_wrap_sync, "chown",
uv_fs_chown, *path, uid, gid);
SyncCallAndThrowOnError(env, &req_wrap_sync, uv_fs_chown, *path, uid, gid);
FS_SYNC_TRACE_END(chown);
}
}

View File

@@ -67,6 +67,7 @@ declare namespace InternalFSBinding {
function chown(path: string, uid: number, gid: number, req: FSReqCallback): void;
function chown(path: string, uid: number, gid: number, req: undefined, ctx: FSSyncContext): void;
function chown(path: string, uid: number, gid: number, usePromises: typeof kUsePromises): Promise<void>;
function chown(path: string, uid: number, gid: number): void;
function close(fd: number, req: FSReqCallback): void;
function close(fd: number, req: undefined, ctx: FSSyncContext): void;