Skip to content

Commit

Permalink
fs: improve error performance for fdatasyncSync
Browse files Browse the repository at this point in the history
PR-URL: nodejs#49898
Refs: nodejs/performance#106
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
  • Loading branch information
Jungku Lee authored and debadree25 committed Apr 15, 2024
1 parent 98806b1 commit 4b7fc22
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
42 changes: 42 additions & 0 deletions benchmark/fs/bench_fdatasyncSync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict';

const common = require('../common');
const fs = require('fs');
const tmpdir = require('../../test/common/tmpdir');
tmpdir.refresh();

const tmpfile = tmpdir.resolve(`.existing-file-${process.pid}`);
fs.writeFileSync(tmpfile, 'this-is-for-a-benchmark', 'utf8');

const bench = common.createBenchmark(main, {
type: ['existing', 'non-existing'],
n: [1e4],
});

function main({ n, type }) {
let fd;

switch (type) {
case 'existing':
fd = fs.openSync(tmpfile, 'r', 0o666);
break;
case 'non-existing':
fd = 1 << 30;
break;
default:
new Error('Invalid type');
}

bench.start();
for (let i = 0; i < n; i++) {
try {
fs.fdatasyncSync(fd);
} catch {
// do nothing
}
}

bench.end(n);

if (type === 'existing') fs.closeSync(fd);
}
4 changes: 1 addition & 3 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1305,9 +1305,7 @@ function fdatasync(fd, callback) {
*/
function fdatasyncSync(fd) {
fd = getValidatedFd(fd);
const ctx = {};
binding.fdatasync(fd, undefined, ctx);
handleErrorFromBinding(ctx);
return binding.fdatasync(fd);
}

/**
Expand Down
14 changes: 7 additions & 7 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1501,21 +1501,21 @@ static void Fdatasync(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

const int argc = args.Length();
CHECK_GE(argc, 2);
CHECK_GE(argc, 1);

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

FSReqBase* req_wrap_async = GetReqWrap(args, 1);
if (req_wrap_async != nullptr) {
if (argc > 1) { // fdatasync(fd, req)
FSReqBase* req_wrap_async = GetReqWrap(args, 1);
CHECK_NOT_NULL(req_wrap_async);
FS_ASYNC_TRACE_BEGIN0(UV_FS_FDATASYNC, req_wrap_async)
AsyncCall(env, req_wrap_async, args, "fdatasync", UTF8, AfterNoArgs,
uv_fs_fdatasync, fd);
} else {
CHECK_EQ(argc, 3);
FSReqWrapSync req_wrap_sync;
} else { // fdatasync(fd)
FSReqWrapSync req_wrap_sync("fdatasync");
FS_SYNC_TRACE_BEGIN(fdatasync);
SyncCall(env, args[2], &req_wrap_sync, "fdatasync", uv_fs_fdatasync, fd);
SyncCallAndThrowOnError(env, &req_wrap_sync, uv_fs_fdatasync, fd);
FS_SYNC_TRACE_END(fdatasync);
}
}
Expand Down
1 change: 1 addition & 0 deletions typings/internalBinding/fs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ declare namespace InternalFSBinding {
function fdatasync(fd: number, req: FSReqCallback): void;
function fdatasync(fd: number, req: undefined, ctx: FSSyncContext): void;
function fdatasync(fd: number, usePromises: typeof kUsePromises): Promise<void>;
function fdatasync(fd: number): void;

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

0 comments on commit 4b7fc22

Please sign in to comment.