Skip to content

Commit f766c04

Browse files
anonrigtargos
authored andcommitted
fs: improve error performance of chownSync
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 610036c commit f766c04

4 files changed

Lines changed: 65 additions & 10 deletions

File tree

‎benchmark/fs/bench-chownSync.js‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
'use strict';
2+
3+
constcommon=require('../common');
4+
constfs=require('fs');
5+
constassert=require('assert');
6+
consttmpdir=require('../../test/common/tmpdir');
7+
8+
if(process.platform==='win32'){
9+
console.log('Skipping: Windows does not have `getuid` or `getgid`');
10+
process.exit(0);
11+
}
12+
13+
constbench=common.createBenchmark(main,{
14+
type: ['existing','non-existing'],
15+
method: ['chownSync','lchownSync'],
16+
n: [1e4],
17+
});
18+
19+
functionmain({ n, type, method }){
20+
constuid=process.getuid();
21+
constgid=process.getgid();
22+
constfsMethod=fs[method];
23+
24+
switch(type){
25+
case'existing': {
26+
tmpdir.refresh();
27+
consttmpfile=tmpdir.resolve(`.existing-file-${process.pid}`);
28+
fs.writeFileSync(tmpfile,'this-is-for-a-benchmark','utf8');
29+
bench.start();
30+
for(leti=0;i<n;i++){
31+
fsMethod(tmpfile,uid,gid);
32+
}
33+
bench.end(n);
34+
break;
35+
}
36+
case'non-existing': {
37+
constpath=tmpdir.resolve(`.non-existing-file-${Date.now()}`);
38+
lethasError=false;
39+
bench.start();
40+
for(leti=0;i<n;i++){
41+
try{
42+
fs[method](path,uid,gid);
43+
}catch{
44+
hasError=true;
45+
}
46+
}
47+
bench.end(n);
48+
assert(hasError);
49+
break;
50+
}
51+
default:
52+
newError('Invalid type');
53+
}
54+
}

‎lib/fs.js‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2089,9 +2089,11 @@ function chownSync(path, uid, gid) {
20892089
path=getValidatedPath(path);
20902090
validateInteger(uid,'uid',-1,kMaxUserId);
20912091
validateInteger(gid,'gid',-1,kMaxUserId);
2092-
constctx={ path };
2093-
binding.chown(pathModule.toNamespacedPath(path),uid,gid,undefined,ctx);
2094-
handleErrorFromBinding(ctx);
2092+
binding.chown(
2093+
pathModule.toNamespacedPath(path),
2094+
uid,
2095+
gid,
2096+
);
20952097
}
20962098

20972099
/**

‎src/node_file.cc‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2572,18 +2572,16 @@ static void Chown(const FunctionCallbackInfo<Value>& args) {
25722572
CHECK(IsSafeJsInt(args[2]));
25732573
constuv_gid_t gid = static_cast<uv_gid_t>(args[2].As<Integer>()->Value());
25742574

2575-
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
2576-
if (req_wrap_async != nullptr) { // chown(path, uid, gid, req)
2575+
if (argc > 3) { // chown(path, uid, gid, req)
2576+
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
25772577
FS_ASYNC_TRACE_BEGIN1(
25782578
UV_FS_CHOWN, req_wrap_async, "path", TRACE_STR_COPY(*path))
25792579
AsyncCall(env, req_wrap_async, args, "chown", UTF8, AfterNoArgs,
25802580
uv_fs_chown, *path, uid, gid);
2581-
} else { // chown(path, uid, gid, undefined, ctx)
2582-
CHECK_EQ(argc, 5);
2583-
FSReqWrapSync req_wrap_sync;
2581+
} else { // chown(path, uid, gid)
2582+
FSReqWrapSync req_wrap_sync("chown", *path);
25842583
FS_SYNC_TRACE_BEGIN(chown);
2585-
SyncCall(env, args[4], &req_wrap_sync, "chown",
2586-
uv_fs_chown, *path, uid, gid);
2584+
SyncCallAndThrowOnError(env, &req_wrap_sync, uv_fs_chown, *path, uid, gid);
25872585
FS_SYNC_TRACE_END(chown);
25882586
}
25892587
}

‎typings/internalBinding/fs.d.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ declare namespace InternalFSBinding {
6767
functionchown(path: string,uid: number,gid: number,req: FSReqCallback): void;
6868
functionchown(path: string,uid: number,gid: number,req: undefined,ctx: FSSyncContext): void;
6969
functionchown(path: string,uid: number,gid: number,usePromises: typeofkUsePromises): Promise<void>;
70+
functionchown(path: string,uid: number,gid: number): void;
7071

7172
functionclose(fd: number,req: FSReqCallback): void;
7273
functionclose(fd: number,req: undefined,ctx: FSSyncContext): void;

0 commit comments

Comments
 (0)