fs: improve error performance for `readSync` · nodejs/node@0cc176e · GitHub
Skip to content

Commit 0cc176e

Browse files
Jungku Leetargos
authored andcommitted
fs: improve error performance for readSync
PR-URL: #50033 Refs: nodejs/performance#106 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
1 parent f050668 commit 0cc176e

4 files changed

Lines changed: 71 additions & 13 deletions

File tree

benchmark/fs/bench-readSync.js

Lines changed: 57 additions & 0 deletions

lib/fs.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -751,11 +751,7 @@ function readSync(fd, buffer, offsetOrOptions, length, position) {
751751
validatePosition(position, 'position', length);
752752
}
753753

754-
const ctx = {};
755-
const result = binding.read(fd, buffer, offset, length, position,
756-
undefined, ctx);
757-
handleErrorFromBinding(ctx);
758-
return result;
754+
return binding.read(fd, buffer, offset, length, position);
759755
}
760756

761757
/**

src/node_file.cc

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2341,18 +2341,23 @@ static void Read(const FunctionCallbackInfo<Value>& args) {
23412341
char* buf = buffer_data + off;
23422342
uv_buf_t uvbuf = uv_buf_init(buf, len);
23432343

2344-
FSReqBase* req_wrap_async = GetReqWrap(args, 5);
2345-
if (req_wrap_async != nullptr) { // read(fd, buffer, offset, len, pos, req)
2344+
if (argc > 5) { // read(fd, buffer, offset, len, pos, req)
2345+
FSReqBase* req_wrap_async = GetReqWrap(args, 5);
2346+
CHECK_NOT_NULL(req_wrap_async);
23462347
FS_ASYNC_TRACE_BEGIN0(UV_FS_READ, req_wrap_async)
23472348
AsyncCall(env, req_wrap_async, args, "read", UTF8, AfterInteger,
23482349
uv_fs_read, fd, &uvbuf, 1, pos);
2349-
} else { // read(fd, buffer, offset, len, pos, undefined, ctx)
2350-
CHECK_EQ(argc, 7);
2351-
FSReqWrapSync req_wrap_sync;
2350+
} else { // read(fd, buffer, offset, len, pos)
2351+
FSReqWrapSync req_wrap_sync("read");
23522352
FS_SYNC_TRACE_BEGIN(read);
2353-
const int bytesRead = SyncCall(env, args[6], &req_wrap_sync, "read",
2354-
uv_fs_read, fd, &uvbuf, 1, pos);
2353+
const int bytesRead = SyncCallAndThrowOnError(
2354+
env, &req_wrap_sync, uv_fs_read, fd, &uvbuf, 1, pos);
23552355
FS_SYNC_TRACE_END(read, "bytesRead", bytesRead);
2356+
2357+
if (is_uv_error(bytesRead)) {
2358+
return;
2359+
}
2360+
23562361
args.GetReturnValue().Set(bytesRead);
23572362
}
23582363
}

typings/internalBinding/fs.d.ts

Lines changed: 1 addition & 1 deletion

0 commit comments

Comments
 (0)