Skip to content

Commit b0e34ed

Browse files
committed
fs: improve error performance of readvSync
1 parent 766198b commit b0e34ed

File tree

3 files changed

+67
-11
lines changed

3 files changed

+67
-11
lines changed

benchmark/fs/bench-readvSync.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const fs = require('fs');
5+
const assert = require('assert');
6+
const tmpdir = require('../../test/common/tmpdir');
7+
tmpdir.refresh();
8+
9+
const exptectedBuff = Buffer.from('Benchmark Data');
10+
const expectedLength = exptectedBuff.length;
11+
12+
const bufferArr = [Buffer.alloc(expectedLength)];
13+
14+
const filename = tmpdir.resolve('readv_sync.txt');
15+
fs.writeFileSync(filename, exptectedBuff);
16+
17+
const bench = common.createBenchmark(main, {
18+
type: ['valid', 'invalid'],
19+
n: [1e5],
20+
});
21+
22+
function main({ n, type }) {
23+
let fd;
24+
let result;
25+
26+
switch (type) {
27+
case 'valid':
28+
fd = fs.openSync(filename, 'r');
29+
30+
bench.start();
31+
for (let i = 0; i < n; i++) {
32+
result = fs.readvSync(fd, bufferArr, 0);
33+
}
34+
35+
bench.end(n);
36+
assert.strictEqual(result, expectedLength);
37+
fs.closeSync(fd);
38+
break;
39+
case 'invalid': {
40+
fd = 1 << 30;
41+
let hasError = false;
42+
bench.start();
43+
for (let i = 0; i < n; i++) {
44+
try {
45+
result = fs.readvSync(fd, bufferArr, 0);
46+
} catch {
47+
hasError = true;
48+
}
49+
}
50+
51+
bench.end(n);
52+
assert(hasError);
53+
break;
54+
}
55+
default:
56+
throw new Error('Invalid type');
57+
}
58+
}

lib/fs.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -805,14 +805,10 @@ function readvSync(fd, buffers, position) {
805805
fd = getValidatedFd(fd);
806806
validateBufferArray(buffers);
807807

808-
const ctx = {};
809-
810808
if (typeof position !== 'number')
811809
position = null;
812810

813-
const result = binding.readBuffers(fd, buffers, position, undefined, ctx);
814-
handleErrorFromBinding(ctx);
815-
return result;
811+
return binding.readBuffers(fd, buffers, position);
816812
}
817813

818814
/**

src/node_file.cc

+8-6
Original file line numberDiff line numberDiff line change
@@ -2483,18 +2483,20 @@ static void ReadBuffers(const FunctionCallbackInfo<Value>& args) {
24832483
iovs[i] = uv_buf_init(Buffer::Data(buffer), Buffer::Length(buffer));
24842484
}
24852485

2486-
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
2487-
if (req_wrap_async != nullptr) { // readBuffers(fd, buffers, pos, req)
2486+
if (argc > 3) { // readBuffers(fd, buffers, pos, req)
2487+
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
24882488
FS_ASYNC_TRACE_BEGIN0(UV_FS_READ, req_wrap_async)
24892489
AsyncCall(env, req_wrap_async, args, "read", UTF8, AfterInteger,
24902490
uv_fs_read, fd, *iovs, iovs.length(), pos);
24912491
} else { // readBuffers(fd, buffers, undefined, ctx)
2492-
CHECK_EQ(argc, 5);
2493-
FSReqWrapSync req_wrap_sync;
2492+
FSReqWrapSync req_wrap_sync("read");
24942493
FS_SYNC_TRACE_BEGIN(read);
2495-
int bytesRead = SyncCall(env, /* ctx */ args[4], &req_wrap_sync, "read",
2496-
uv_fs_read, fd, *iovs, iovs.length(), pos);
2494+
int bytesRead = SyncCallAndThrowOnError(
2495+
env, &req_wrap_sync, uv_fs_read, fd, *iovs, iovs.length(), pos);
24972496
FS_SYNC_TRACE_END(read, "bytesRead", bytesRead);
2497+
if (is_uv_error(bytesRead)) {
2498+
return;
2499+
}
24982500
args.GetReturnValue().Set(bytesRead);
24992501
}
25002502
}

0 commit comments

Comments
 (0)