Skip to content

Commit e6a80ee

Browse files
author
pluris
committed
fs: improve error preformance fori fsyncSync
1 parent f16f41c commit e6a80ee

File tree

4 files changed

+69
-4
lines changed

4 files changed

+69
-4
lines changed

benchmark/fs/bench-fsyncSync.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 = 0;
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.fsyncSync(fd);
34+
} catch {
35+
// do nothing
36+
}
37+
}
38+
39+
if (fd !== 0) fs.closeSync(fd);
40+
41+
bench.end(n);
42+
}

lib/fs.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -1309,10 +1309,7 @@ function fsync(fd, callback) {
13091309
* @returns {void}
13101310
*/
13111311
function fsyncSync(fd) {
1312-
fd = getValidatedFd(fd);
1313-
const ctx = {};
1314-
binding.fsync(fd, undefined, ctx);
1315-
handleErrorFromBinding(ctx);
1312+
syncFs.fsync(fd);
13161313
}
13171314

13181315
/**

lib/internal/fs/sync.js

+6
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ function close(fd) {
8888
return binding.closeSync(fd);
8989
}
9090

91+
function fsync(fd) {
92+
fd = getValidatedFd(fd);
93+
return binding.fsyncSync(fd);
94+
}
95+
9196
module.exports = {
9297
readFileUtf8,
9398
exists,
@@ -97,4 +102,5 @@ module.exports = {
97102
statfs,
98103
open,
99104
close,
105+
fsync,
100106
};

src/node_file.cc

+20
Original file line numberDiff line numberDiff line change
@@ -1641,6 +1641,24 @@ static void Fsync(const FunctionCallbackInfo<Value>& args) {
16411641
}
16421642
}
16431643

1644+
static void FsyncSync(const FunctionCallbackInfo<Value>& args) {
1645+
Environment* env = Environment::GetCurrent(args);
1646+
1647+
const int argc = args.Length();
1648+
CHECK_GE(argc, 1);
1649+
1650+
CHECK(args[0]->IsInt32());
1651+
const int fd = args[0].As<Int32>()->Value();
1652+
1653+
uv_fs_t req;
1654+
FS_SYNC_TRACE_BEGIN(fsync);
1655+
int err = uv_fs_fsync(nullptr, &req, fd, nullptr);
1656+
FS_SYNC_TRACE_END(fsync);
1657+
if (err < 0) {
1658+
return env->ThrowUVException(err, "fsync", nullptr);
1659+
}
1660+
}
1661+
16441662
static void Unlink(const FunctionCallbackInfo<Value>& args) {
16451663
Environment* env = Environment::GetCurrent(args);
16461664

@@ -3373,6 +3391,7 @@ static void CreatePerIsolateProperties(IsolateData* isolate_data,
33733391
SetMethod(isolate, target, "readBuffers", ReadBuffers);
33743392
SetMethod(isolate, target, "fdatasync", Fdatasync);
33753393
SetMethod(isolate, target, "fsync", Fsync);
3394+
SetMethod(isolate, target, "fsyncSync", FsyncSync);
33763395
SetMethod(isolate, target, "rename", Rename);
33773396
SetMethod(isolate, target, "ftruncate", FTruncate);
33783397
SetMethod(isolate, target, "rmdir", RMDir);
@@ -3498,6 +3517,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
34983517
registry->Register(ReadBuffers);
34993518
registry->Register(Fdatasync);
35003519
registry->Register(Fsync);
3520+
registry->Register(FsyncSync);
35013521
registry->Register(Rename);
35023522
registry->Register(FTruncate);
35033523
registry->Register(RMDir);

0 commit comments

Comments
 (0)