Skip to content

Commit

Permalink
fs: improve error preformance fori fsyncSync
Browse files Browse the repository at this point in the history
  • Loading branch information
pluris committed Sep 26, 2023
1 parent f16f41c commit e6a80ee
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 4 deletions.
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 = 0;
break;
default:
new Error('Invalid type');
}

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

if (fd !== 0) fs.closeSync(fd);

bench.end(n);
}
5 changes: 1 addition & 4 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1309,10 +1309,7 @@ function fsync(fd, callback) {
* @returns {void}
*/
function fsyncSync(fd) {
fd = getValidatedFd(fd);
const ctx = {};
binding.fsync(fd, undefined, ctx);
handleErrorFromBinding(ctx);
syncFs.fsync(fd);
}

/**
Expand Down
6 changes: 6 additions & 0 deletions lib/internal/fs/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ function close(fd) {
return binding.closeSync(fd);
}

function fsync(fd) {
fd = getValidatedFd(fd);
return binding.fsyncSync(fd);
}

module.exports = {
readFileUtf8,
exists,
Expand All @@ -97,4 +102,5 @@ module.exports = {
statfs,
open,
close,
fsync,
};
20 changes: 20 additions & 0 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1641,6 +1641,24 @@ static void Fsync(const FunctionCallbackInfo<Value>& args) {
}
}

static void FsyncSync(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

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

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

uv_fs_t req;
FS_SYNC_TRACE_BEGIN(fsync);
int err = uv_fs_fsync(nullptr, &req, fd, nullptr);
FS_SYNC_TRACE_END(fsync);
if (err < 0) {
return env->ThrowUVException(err, "fsync", nullptr);
}
}

static void Unlink(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

Expand Down Expand Up @@ -3373,6 +3391,7 @@ static void CreatePerIsolateProperties(IsolateData* isolate_data,
SetMethod(isolate, target, "readBuffers", ReadBuffers);
SetMethod(isolate, target, "fdatasync", Fdatasync);
SetMethod(isolate, target, "fsync", Fsync);
SetMethod(isolate, target, "fsyncSync", FsyncSync);
SetMethod(isolate, target, "rename", Rename);
SetMethod(isolate, target, "ftruncate", FTruncate);
SetMethod(isolate, target, "rmdir", RMDir);
Expand Down Expand Up @@ -3498,6 +3517,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(ReadBuffers);
registry->Register(Fdatasync);
registry->Register(Fsync);
registry->Register(FsyncSync);
registry->Register(Rename);
registry->Register(FTruncate);
registry->Register(RMDir);
Expand Down

0 comments on commit e6a80ee

Please sign in to comment.