Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: improve error performance for fsyncSync #49880

Merged
merged 9 commits into from
Oct 20, 2023
Merged
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions benchmark/fs/bench-fsyncSync.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.fsyncSync(fd);
} catch {
// do nothing
}
}

bench.end(n);

if (type === 'existing') fs.closeSync(fd);
}
5 changes: 1 addition & 4 deletions lib/fs.js
Original file line number Diff line number Diff line change
@@ -1330,10 +1330,7 @@ function fsync(fd, callback) {
* @returns {void}
*/
function fsyncSync(fd) {
fd = getValidatedFd(fd);
const ctx = {};
binding.fsync(fd, undefined, ctx);
handleErrorFromBinding(ctx);
return binding.fsync(fd);
}

/**
34 changes: 26 additions & 8 deletions src/node_file.cc
Original file line number Diff line number Diff line change
@@ -116,6 +116,24 @@ inline int64_t GetOffset(Local<Value> value) {
return IsSafeJsInt(value) ? value.As<Integer>()->Value() : -1;
}

inline int GetValidatedFd(Environment* env, Local<Value> value) {
if (!value->IsInt32()) {
env->isolate()->ThrowException(ERR_INVALID_ARG_TYPE(
Copy link
Member

@joyeecheung joyeecheung Oct 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be a ValidateInt32 if we want to start moving the validation to C++. Also this is not entirely on-par with the original implementation - the original implementation would print the value in a readable format in the case it's not a number, and this does not give any information about the invalid argument, which would make it harder for users to fix the error. We could also just don't move the error validation code now and leave it in JS if it's not ready to take on implementing proper argument validation + printing in C++.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joyeecheung Thank you for your comment.
You are right. As you said, I think we need to think more about the implementation of GetValidateFd(). First, I will remove this addition to C++ and change it to the original implementation.

env->isolate(), "Invalid argument. The fd must be int32."));
return 1 << 30;
}

const int fd = value.As<Int32>()->Value();

if (fd < 0 || fd > INT32_MAX) {
env->isolate()->ThrowException(ERR_OUT_OF_RANGE(
env->isolate(), "It must be >= 0 && <= INT32_MAX. Received %d", fd));
return 1 << 30;
}

return fd;
}

static const char* get_fs_func_name_by_type(uv_fs_type req_type) {
switch (req_type) {
#define FS_TYPE_TO_NAME(type, name) \
@@ -1524,21 +1542,21 @@ static void Fsync(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();
const int fd = GetValidatedFd(env, args[0]);
if (fd == (1 << 30)) return;

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

function ftruncate(fd: number, len: number, req: FSReqCallback): void;
function ftruncate(fd: number, len: number, req: undefined, ctx: FSSyncContext): void;
@@ -243,6 +244,7 @@ export interface FsBinding {
fdatasync: typeof InternalFSBinding.fdatasync;
fstat: typeof InternalFSBinding.fstat;
fsync: typeof InternalFSBinding.fsync;
fsyncSync: typeof InternalFSBinding.fsyncSync;
ftruncate: typeof InternalFSBinding.ftruncate;
futimes: typeof InternalFSBinding.futimes;
internalModuleReadJSON: typeof InternalFSBinding.internalModuleReadJSON;