GeezerVison is showing github.com — fs: improve error performance for `fsyncSync` · nodejs/node@5942edb
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +42Lines changed: 42 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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.fsyncSync(fd); | | 34 | + } catch { | | 35 | + // do nothing | | 36 | + } | | 37 | + } | | 38 | + | | 39 | + bench.end(n); | | 40 | + | | 41 | + if (type === 'existing') fs.closeSync(fd); | | 42 | +} |
Collapse file Copy file name to clipboardExpand all lines: lib/fs.js +1-3Lines changed: 1 addition & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1323,9 +1323,7 @@ function fsync(fd, callback) { | 1323 | 1323 | */ | 1324 | 1324 | function fsyncSync(fd) { | 1325 | 1325 | fd = getValidatedFd(fd); | 1326 | | - const ctx = {}; | 1327 | | - binding.fsync(fd, undefined, ctx); | 1328 | | - handleErrorFromBinding(ctx); | | 1326 | + return binding.fsync(fd); | 1329 | 1327 | } | 1330 | 1328 |
| 1331 | 1329 | /** | |
Collapse file Copy file name to clipboardExpand all lines: src/node_file.cc +6-6Lines changed: 6 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1516,21 +1516,21 @@ static void Fsync(const FunctionCallbackInfo<Value>& args) { | 1516 | 1516 | Environment* env = Environment::GetCurrent(args); | 1517 | 1517 |
| 1518 | 1518 | const int argc = args.Length(); | 1519 | | - CHECK_GE(argc, 2); | | 1519 | + CHECK_GE(argc, 1); | 1520 | 1520 |
| 1521 | 1521 | CHECK(args[0]->IsInt32()); | 1522 | 1522 | const int fd = args[0].As<Int32>()->Value(); | 1523 | 1523 |
| 1524 | | - FSReqBase* req_wrap_async = GetReqWrap(args, 1); | 1525 | | - if (req_wrap_async != nullptr) { | | 1524 | + if (argc > 1) { | | 1525 | + FSReqBase* req_wrap_async = GetReqWrap(args, 1); | | 1526 | + CHECK_NOT_NULL(req_wrap_async); | 1526 | 1527 | FS_ASYNC_TRACE_BEGIN0(UV_FS_FSYNC, req_wrap_async) | 1527 | 1528 | AsyncCall(env, req_wrap_async, args, "fsync", UTF8, AfterNoArgs, | 1528 | 1529 | uv_fs_fsync, fd); | 1529 | 1530 | } else { | 1530 | | - CHECK_EQ(argc, 3); | 1531 | | - FSReqWrapSync req_wrap_sync; | | 1531 | + FSReqWrapSync req_wrap_sync("fsync"); | 1532 | 1532 | FS_SYNC_TRACE_BEGIN(fsync); | 1533 | | - SyncCall(env, args[2], &req_wrap_sync, "fsync", uv_fs_fsync, fd); | | 1533 | + SyncCallAndThrowOnError(env, &req_wrap_sync, uv_fs_fsync, fd); | 1534 | 1534 | FS_SYNC_TRACE_END(fsync); | 1535 | 1535 | } | 1536 | 1536 | } | |
Collapse file Copy file name to clipboardExpand all lines: typings/internalBinding/fs.d.ts +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -100,8 +100,8 @@ declare namespace InternalFSBinding { | 100 | 100 | function fstat(fd: number, useBigint: false, usePromises: typeof kUsePromises): Promise<Float64Array>; | 101 | 101 |
| 102 | 102 | function fsync(fd: number, req: FSReqCallback): void; | 103 | | - function fsync(fd: number, req: undefined, ctx: FSSyncContext): void; | 104 | 103 | function fsync(fd: number, usePromises: typeof kUsePromises): Promise<void>; | | 104 | + function fsync(fd: number): void; | 105 | 105 |
| 106 | 106 | function ftruncate(fd: number, len: number, req: FSReqCallback): void; | 107 | 107 | function ftruncate(fd: number, len: number, req: undefined, ctx: FSSyncContext): void; | |
|