GeezerVison is showing github.com — fs: improve error performance for `ftruncateSync` · nodejs/node@57eb06e
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 +40Lines changed: 40 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 path = tmpdir.resolve(`new-file-${process.pid}`); | | 9 | +fs.appendFileSync(path, 'Some content.'); | | 10 | + | | 11 | +const bench = common.createBenchmark(main, { | | 12 | + type: ['invalid', 'valid'], | | 13 | + n: [1e4], | | 14 | +}); | | 15 | + | | 16 | +function main({ n, type }) { | | 17 | + let fd; | | 18 | + | | 19 | + switch (type) { | | 20 | + case 'invalid': | | 21 | + fd = 1 << 30; | | 22 | + break; | | 23 | + case 'valid': | | 24 | + fd = fs.openSync(path, 'r+'); | | 25 | + break; | | 26 | + default: | | 27 | + throw new Error('Invalid type'); | | 28 | + } | | 29 | + | | 30 | + bench.start(); | | 31 | + for (let i = 0; i < n; i++) { | | 32 | + try { | | 33 | + fs.ftruncateSync(fd, 4); | | 34 | + } catch { | | 35 | + // do nothing | | 36 | + } | | 37 | + } | | 38 | + bench.end(n); | | 39 | + if (type === 'valid') fs.closeSync(fd); | | 40 | +} |
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 |
|---|
@@ -1136,9 +1136,7 @@ function ftruncateSync(fd, len = 0) { | 1136 | 1136 | fd = getValidatedFd(fd); | 1137 | 1137 | validateInteger(len, 'len'); | 1138 | 1138 | len = MathMax(0, len); | 1139 | | - const ctx = {}; | 1140 | | - binding.ftruncate(fd, len, undefined, ctx); | 1141 | | - handleErrorFromBinding(ctx); | | 1139 | + binding.ftruncate(fd, len); | 1142 | 1140 | } | 1143 | 1141 |
| 1144 | 1142 | function lazyLoadCp() { | |
Collapse file Copy file name to clipboardExpand all lines: src/node_file.cc +5-7Lines changed: 5 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1509,25 +1509,23 @@ static void FTruncate(const FunctionCallbackInfo<Value>& args) { | 1509 | 1509 | Environment* env = Environment::GetCurrent(args); | 1510 | 1510 |
| 1511 | 1511 | const int argc = args.Length(); | 1512 | | - CHECK_GE(argc, 3); | | 1512 | + CHECK_GE(argc, 2); | 1513 | 1513 |
| 1514 | 1514 | CHECK(args[0]->IsInt32()); | 1515 | 1515 | const int fd = args[0].As<Int32>()->Value(); | 1516 | 1516 |
| 1517 | 1517 | CHECK(IsSafeJsInt(args[1])); | 1518 | 1518 | const int64_t len = args[1].As<Integer>()->Value(); | 1519 | 1519 |
| 1520 | | - FSReqBase* req_wrap_async = GetReqWrap(args, 2); | 1521 | | - if (req_wrap_async != nullptr) { | | 1520 | + if (argc > 2) { | | 1521 | + FSReqBase* req_wrap_async = GetReqWrap(args, 2); | 1522 | 1522 | FS_ASYNC_TRACE_BEGIN0(UV_FS_FTRUNCATE, req_wrap_async) | 1523 | 1523 | AsyncCall(env, req_wrap_async, args, "ftruncate", UTF8, AfterNoArgs, | 1524 | 1524 | uv_fs_ftruncate, fd, len); | 1525 | 1525 | } else { | 1526 | | - CHECK_EQ(argc, 4); | 1527 | | - FSReqWrapSync req_wrap_sync; | | 1526 | + FSReqWrapSync req_wrap_sync("ftruncate"); | 1528 | 1527 | FS_SYNC_TRACE_BEGIN(ftruncate); | 1529 | | - SyncCall(env, args[3], &req_wrap_sync, "ftruncate", uv_fs_ftruncate, fd, | 1530 | | - len); | | 1528 | + SyncCallAndThrowOnError(env, &req_wrap_sync, uv_fs_ftruncate, fd, len); | 1531 | 1529 | FS_SYNC_TRACE_END(ftruncate); | 1532 | 1530 | } | 1533 | 1531 | } | |
|