Skip to content

Commit 4b7fc22

Browse files
Jungku Leedebadree25
Jungku Lee
authored andcommitted
fs: improve error performance for fdatasyncSync
PR-URL: nodejs#49898 Refs: nodejs/performance#106 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
1 parent 98806b1 commit 4b7fc22

File tree

4 files changed

+51
-10
lines changed

4 files changed

+51
-10
lines changed

benchmark/fs/bench_fdatasyncSync.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const fs = require('fs');
5+
const tmpdir = require('../../test/common/tmpdir');
6+
tmpdir.refresh();
7+
8+
const tmpfile = tmpdir.resolve(`.existing-file-${process.pid}`);
9+
fs.writeFileSync(tmpfile, 'this-is-for-a-benchmark', 'utf8');
10+
11+
const bench = common.createBenchmark(main, {
12+
type: ['existing', 'non-existing'],
13+
n: [1e4],
14+
});
15+
16+
function main({ n, type }) {
17+
let fd;
18+
19+
switch (type) {
20+
case 'existing':
21+
fd = fs.openSync(tmpfile, 'r', 0o666);
22+
break;
23+
case 'non-existing':
24+
fd = 1 << 30;
25+
break;
26+
default:
27+
new Error('Invalid type');
28+
}
29+
30+
bench.start();
31+
for (let i = 0; i < n; i++) {
32+
try {
33+
fs.fdatasyncSync(fd);
34+
} catch {
35+
// do nothing
36+
}
37+
}
38+
39+
bench.end(n);
40+
41+
if (type === 'existing') fs.closeSync(fd);
42+
}

lib/fs.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1305,9 +1305,7 @@ function fdatasync(fd, callback) {
13051305
*/
13061306
function fdatasyncSync(fd) {
13071307
fd = getValidatedFd(fd);
1308-
const ctx = {};
1309-
binding.fdatasync(fd, undefined, ctx);
1310-
handleErrorFromBinding(ctx);
1308+
return binding.fdatasync(fd);
13111309
}
13121310

13131311
/**

src/node_file.cc

+7-7
Original file line numberDiff line numberDiff line change
@@ -1501,21 +1501,21 @@ static void Fdatasync(const FunctionCallbackInfo<Value>& args) {
15011501
Environment* env = Environment::GetCurrent(args);
15021502

15031503
const int argc = args.Length();
1504-
CHECK_GE(argc, 2);
1504+
CHECK_GE(argc, 1);
15051505

15061506
CHECK(args[0]->IsInt32());
15071507
const int fd = args[0].As<Int32>()->Value();
15081508

1509-
FSReqBase* req_wrap_async = GetReqWrap(args, 1);
1510-
if (req_wrap_async != nullptr) {
1509+
if (argc > 1) { // fdatasync(fd, req)
1510+
FSReqBase* req_wrap_async = GetReqWrap(args, 1);
1511+
CHECK_NOT_NULL(req_wrap_async);
15111512
FS_ASYNC_TRACE_BEGIN0(UV_FS_FDATASYNC, req_wrap_async)
15121513
AsyncCall(env, req_wrap_async, args, "fdatasync", UTF8, AfterNoArgs,
15131514
uv_fs_fdatasync, fd);
1514-
} else {
1515-
CHECK_EQ(argc, 3);
1516-
FSReqWrapSync req_wrap_sync;
1515+
} else { // fdatasync(fd)
1516+
FSReqWrapSync req_wrap_sync("fdatasync");
15171517
FS_SYNC_TRACE_BEGIN(fdatasync);
1518-
SyncCall(env, args[2], &req_wrap_sync, "fdatasync", uv_fs_fdatasync, fd);
1518+
SyncCallAndThrowOnError(env, &req_wrap_sync, uv_fs_fdatasync, fd);
15191519
FS_SYNC_TRACE_END(fdatasync);
15201520
}
15211521
}

typings/internalBinding/fs.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ declare namespace InternalFSBinding {
8686
function fdatasync(fd: number, req: FSReqCallback): void;
8787
function fdatasync(fd: number, req: undefined, ctx: FSSyncContext): void;
8888
function fdatasync(fd: number, usePromises: typeof kUsePromises): Promise<void>;
89+
function fdatasync(fd: number): void;
8990

9091
function fstat(fd: number, useBigint: boolean, req: FSReqCallback<Float64Array | BigUint64Array>): void;
9192
function fstat(fd: number, useBigint: true, req: FSReqCallback<BigUint64Array>): void;

0 commit comments

Comments
 (0)