Skip to content

Commit c522003

Browse files
committed
fs: improve writevSync performance
1 parent 4b35a9c commit c522003

File tree

3 files changed

+51
-10
lines changed

3 files changed

+51
-10
lines changed

benchmark/fs/bench-writevSync.js

Lines 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.writeFileSync(path, 'Some content.');
10+
11+
const bench = common.createBenchmark(main, {
12+
type: ['valid'],
13+
n: [1e5],
14+
});
15+
16+
const buffer = Buffer.from('Benchmark data.');
17+
18+
function main({ n, type }) {
19+
let fd;
20+
21+
switch (type) {
22+
case 'valid':
23+
fd = fs.openSync(path, 'r+');
24+
break;
25+
default:
26+
throw new Error('Invalid type');
27+
}
28+
29+
bench.start();
30+
for (let i = 0; i < n; i++) {
31+
try {
32+
fs.writevSync(fd, [buffer]);
33+
} catch {
34+
// do nothing
35+
}
36+
}
37+
bench.end(n);
38+
39+
if (type === 'valid') fs.closeSync(fd);
40+
}

lib/fs.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -998,12 +998,10 @@ function writevSync(fd, buffers, position) {
998998
return 0;
999999
}
10001000

1001-
const ctx = {};
1002-
10031001
if (typeof position !== 'number')
10041002
position = null;
10051003

1006-
const result = binding.writeBuffers(fd, buffers, position, undefined, ctx);
1004+
const result = binding.writeBuffers(fd, buffers, position);
10071005

10081006
handleErrorFromBinding(ctx);
10091007
return result;

src/node_file.cc

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2205,18 +2205,21 @@ static void WriteBuffers(const FunctionCallbackInfo<Value>& args) {
22052205
iovs[i] = uv_buf_init(Buffer::Data(chunk), Buffer::Length(chunk));
22062206
}
22072207

2208-
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
2209-
if (req_wrap_async != nullptr) { // writeBuffers(fd, chunks, pos, req)
2210-
FS_ASYNC_TRACE_BEGIN0(UV_FS_WRITE, req_wrap_async)
2211-
AsyncCall(env, req_wrap_async, args, "write", UTF8, AfterInteger,
2208+
if(argc > 3) {
2209+
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
2210+
FS_ASYNC_TRACE_BEGIN0(UV_FS_WRITE, req_wrap_async)
2211+
AsyncCall(env, req_wrap_async, args, "write", UTF8, AfterInteger,
22122212
uv_fs_write, fd, *iovs, iovs.length(), pos);
2213-
} else { // writeBuffers(fd, chunks, pos, undefined, ctx)
2214-
CHECK_EQ(argc, 5);
2213+
} else {
22152214
FSReqWrapSync req_wrap_sync;
22162215
FS_SYNC_TRACE_BEGIN(write);
2217-
int bytesWritten = SyncCall(env, args[4], &req_wrap_sync, "write",
2216+
int bytesWritten = SyncCallAndThrowOnError(env, &req_wrap_sync,
22182217
uv_fs_write, fd, *iovs, iovs.length(), pos);
22192218
FS_SYNC_TRACE_END(write, "bytesWritten", bytesWritten);
2219+
2220+
if (is_uv_error(bytesWritten)) {
2221+
return;
2222+
}
22202223
args.GetReturnValue().Set(bytesWritten);
22212224
}
22222225
}

0 commit comments

Comments
 (0)