Skip to content

Commit

Permalink
fs: improve writevSync performance
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyasShabiCS committed Oct 4, 2023
1 parent 4b35a9c commit c522003
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 10 deletions.
40 changes: 40 additions & 0 deletions benchmark/fs/bench-writevSync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';

const common = require('../common');
const fs = require('fs');
const tmpdir = require('../../test/common/tmpdir');
tmpdir.refresh();

const path = tmpdir.resolve(`new-file-${process.pid}`);
fs.writeFileSync(path, 'Some content.');

const bench = common.createBenchmark(main, {
type: ['valid'],
n: [1e5],
});

const buffer = Buffer.from('Benchmark data.');

function main({ n, type }) {
let fd;

switch (type) {
case 'valid':
fd = fs.openSync(path, 'r+');
break;
default:
throw new Error('Invalid type');
}

bench.start();
for (let i = 0; i < n; i++) {
try {
fs.writevSync(fd, [buffer]);
} catch {
// do nothing
}
}
bench.end(n);

if (type === 'valid') fs.closeSync(fd);
}

Check failure on line 40 in benchmark/fs/bench-writevSync.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Newline required at end of file but not found
4 changes: 1 addition & 3 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -998,12 +998,10 @@ function writevSync(fd, buffers, position) {
return 0;
}

const ctx = {};

if (typeof position !== 'number')
position = null;

const result = binding.writeBuffers(fd, buffers, position, undefined, ctx);
const result = binding.writeBuffers(fd, buffers, position);

handleErrorFromBinding(ctx);

Check failure on line 1006 in lib/fs.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

'ctx' is not defined
return result;
Expand Down
17 changes: 10 additions & 7 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2205,18 +2205,21 @@ static void WriteBuffers(const FunctionCallbackInfo<Value>& args) {
iovs[i] = uv_buf_init(Buffer::Data(chunk), Buffer::Length(chunk));
}

FSReqBase* req_wrap_async = GetReqWrap(args, 3);
if (req_wrap_async != nullptr) { // writeBuffers(fd, chunks, pos, req)
FS_ASYNC_TRACE_BEGIN0(UV_FS_WRITE, req_wrap_async)
AsyncCall(env, req_wrap_async, args, "write", UTF8, AfterInteger,
if(argc > 3) {
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
FS_ASYNC_TRACE_BEGIN0(UV_FS_WRITE, req_wrap_async)
AsyncCall(env, req_wrap_async, args, "write", UTF8, AfterInteger,
uv_fs_write, fd, *iovs, iovs.length(), pos);
} else { // writeBuffers(fd, chunks, pos, undefined, ctx)
CHECK_EQ(argc, 5);
} else {
FSReqWrapSync req_wrap_sync;
FS_SYNC_TRACE_BEGIN(write);
int bytesWritten = SyncCall(env, args[4], &req_wrap_sync, "write",
int bytesWritten = SyncCallAndThrowOnError(env, &req_wrap_sync,
uv_fs_write, fd, *iovs, iovs.length(), pos);
FS_SYNC_TRACE_END(write, "bytesWritten", bytesWritten);

if (is_uv_error(bytesWritten)) {
return;
}
args.GetReturnValue().Set(bytesWritten);
}
}
Expand Down

0 comments on commit c522003

Please sign in to comment.