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 5, 2023
1 parent 4b35a9c commit 3c2e4ad
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 14 deletions.
56 changes: 56 additions & 0 deletions benchmark/fs/bench-writevSync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
'use strict';

const common = require('../common');
const fs = require('fs');
const assert = require('assert');
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', 'invalid'],
n: [1e5],
});

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

function main({ n, type }) {
let fd;
let result;
switch (type) {
case 'valid':
fd = fs.openSync(path, 'r+');

bench.start();
for (let i = 0; i < n; i++) {
try {
result = fs.writevSync(fd, [buffer]);
} catch {
//
}
}
bench.end(n);
assert(result);
fs.closeSync(fd);
break;
case 'invalid':
fd = tmpdir.resolve(`.non-existing-file-${process.pid}`);
let hasError = false;
bench.start();
for (let i = 0; i < n; i++) {
try {
result = fs.writevSync(fd, [buffer]);
} catch {
hasError = true;
}
}
bench.end(n);
assert(hasError);

break;
default:
throw new Error('Invalid type');
}
}
7 changes: 1 addition & 6 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -998,15 +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);

handleErrorFromBinding(ctx);
return result;
return binding.writeBuffers(fd, buffers, position);
}

/**
Expand Down
18 changes: 10 additions & 8 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2205,18 +2205,20 @@ 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) { // writeBuffers(fd, chunks, pos, req)
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);
FSReqWrapSync req_wrap_sync;
} else { // writeBuffers(fd, chunks, pos)
FSReqWrapSync req_wrap_sync("write");
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 3c2e4ad

Please sign in to comment.