Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ed49722

Browse filesBrowse files
authored
fs: improve error performance for ftruncateSync
PR-URL: #50032 Refs: nodejs/performance#106 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
1 parent ceedb3a commit ed49722
Copy full SHA for ed49722

File tree

Expand file treeCollapse file tree

3 files changed

+46
-10
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+46
-10
lines changed
Open diff view settings
Collapse file
+40Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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

‎lib/fs.js‎

Copy file name to clipboardExpand all lines: lib/fs.js
+1-3Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,9 +1138,7 @@ function ftruncateSync(fd, len = 0) {
11381138
fd = getValidatedFd(fd);
11391139
validateInteger(len, 'len');
11401140
len = MathMax(0, len);
1141-
const ctx = {};
1142-
binding.ftruncate(fd, len, undefined, ctx);
1143-
handleErrorFromBinding(ctx);
1141+
binding.ftruncate(fd, len);
11441142
}
11451143

11461144
function lazyLoadCp() {
Collapse file

‎src/node_file.cc‎

Copy file name to clipboardExpand all lines: src/node_file.cc
+5-7Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,25 +1474,23 @@ static void FTruncate(const FunctionCallbackInfo<Value>& args) {
14741474
Environment* env = Environment::GetCurrent(args);
14751475

14761476
const int argc = args.Length();
1477-
CHECK_GE(argc, 3);
1477+
CHECK_GE(argc, 2);
14781478

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

14821482
CHECK(IsSafeJsInt(args[1]));
14831483
const int64_t len = args[1].As<Integer>()->Value();
14841484

1485-
FSReqBase* req_wrap_async = GetReqWrap(args, 2);
1486-
if (req_wrap_async != nullptr) {
1485+
if (argc > 2) {
1486+
FSReqBase* req_wrap_async = GetReqWrap(args, 2);
14871487
FS_ASYNC_TRACE_BEGIN0(UV_FS_FTRUNCATE, req_wrap_async)
14881488
AsyncCall(env, req_wrap_async, args, "ftruncate", UTF8, AfterNoArgs,
14891489
uv_fs_ftruncate, fd, len);
14901490
} else {
1491-
CHECK_EQ(argc, 4);
1492-
FSReqWrapSync req_wrap_sync;
1491+
FSReqWrapSync req_wrap_sync("ftruncate");
14931492
FS_SYNC_TRACE_BEGIN(ftruncate);
1494-
SyncCall(env, args[3], &req_wrap_sync, "ftruncate", uv_fs_ftruncate, fd,
1495-
len);
1493+
SyncCallAndThrowOnError(env, &req_wrap_sync, uv_fs_ftruncate, fd, len);
14961494
FS_SYNC_TRACE_END(ftruncate);
14971495
}
14981496
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.