GeezerVison is showing github.com — fs: improve error performance for `fdatasyncSync` · nodejs/node@d7132d9
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.fdatasyncSync(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 |
|---|
@@ -1303,9 +1303,7 @@ function fdatasync(fd, callback) { | 1303 | 1303 | */ | 1304 | 1304 | function fdatasyncSync(fd) { | 1305 | 1305 | fd = getValidatedFd(fd); | 1306 | | - const ctx = {}; | 1307 | | - binding.fdatasync(fd, undefined, ctx); | 1308 | | - handleErrorFromBinding(ctx); | | 1306 | + return binding.fdatasync(fd); | 1309 | 1307 | } | 1310 | 1308 |
| 1311 | 1309 | /** | |
Collapse file Copy file name to clipboardExpand all lines: src/node_file.cc +7-7Lines changed: 7 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1536,21 +1536,21 @@ static void Fdatasync(const FunctionCallbackInfo<Value>& args) { | 1536 | 1536 | Environment* env = Environment::GetCurrent(args); | 1537 | 1537 |
| 1538 | 1538 | const int argc = args.Length(); | 1539 | | - CHECK_GE(argc, 2); | | 1539 | + CHECK_GE(argc, 1); | 1540 | 1540 |
| 1541 | 1541 | CHECK(args[0]->IsInt32()); | 1542 | 1542 | const int fd = args[0].As<Int32>()->Value(); | 1543 | 1543 |
| 1544 | | - FSReqBase* req_wrap_async = GetReqWrap(args, 1); | 1545 | | - if (req_wrap_async != nullptr) { | | 1544 | + if (argc > 1) { // fdatasync(fd, req) | | 1545 | + FSReqBase* req_wrap_async = GetReqWrap(args, 1); | | 1546 | + CHECK_NOT_NULL(req_wrap_async); | 1546 | 1547 | FS_ASYNC_TRACE_BEGIN0(UV_FS_FDATASYNC, req_wrap_async) | 1547 | 1548 | AsyncCall(env, req_wrap_async, args, "fdatasync", UTF8, AfterNoArgs, | 1548 | 1549 | uv_fs_fdatasync, fd); | 1549 | | - } else { | 1550 | | - CHECK_EQ(argc, 3); | 1551 | | - FSReqWrapSync req_wrap_sync; | | 1550 | + } else { // fdatasync(fd) | | 1551 | + FSReqWrapSync req_wrap_sync("fdatasync"); | 1552 | 1552 | FS_SYNC_TRACE_BEGIN(fdatasync); | 1553 | | - SyncCall(env, args[2], &req_wrap_sync, "fdatasync", uv_fs_fdatasync, fd); | | 1553 | + SyncCallAndThrowOnError(env, &req_wrap_sync, uv_fs_fdatasync, fd); | 1554 | 1554 | FS_SYNC_TRACE_END(fdatasync); | 1555 | 1555 | } | 1556 | 1556 | } | |
Collapse file Copy file name to clipboardExpand all lines: typings/internalBinding/fs.d.ts +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -86,6 +86,7 @@ declare namespace InternalFSBinding { | 86 | 86 | function fdatasync(fd: number, req: FSReqCallback): void; | 87 | 87 | function fdatasync(fd: number, req: undefined, ctx: FSSyncContext): void; | 88 | 88 | function fdatasync(fd: number, usePromises: typeof kUsePromises): Promise<void>; | | 89 | + function fdatasync(fd: number): void; | 89 | 90 |
| 90 | 91 | function fstat(fd: number, useBigint: boolean, req: FSReqCallback<Float64Array | BigUint64Array>): void; | 91 | 92 | function fstat(fd: number, useBigint: true, req: FSReqCallback<BigUint64Array>): void; | |
|