diff --git a/benchmark/fs/bench-writeSync.js b/benchmark/fs/bench-writeSync.js new file mode 100644 index 00000000000000..4edef0babca724 --- /dev/null +++ b/benchmark/fs/bench-writeSync.js @@ -0,0 +1,54 @@ +'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}`); +const parameters = [Buffer.from('Benchmark data'), + 0, + Buffer.byteLength('Benchmark data')]; +const bench = common.createBenchmark(main, { + type: ['valid', 'invalid'], + n: [1e5], +}); + +function main({ n, type }) { + let fd; + let result; + + switch (type) { + case 'valid': + fd = fs.openSync(path, 'w'); + + bench.start(); + for (let i = 0; i < n; i++) { + result = fs.writeSync(fd, ...parameters); + } + + bench.end(n); + assert(result); + fs.closeSync(fd); + break; + case 'invalid': { + fd = 1 << 30; + let hasError = false; + bench.start(); + for (let i = 0; i < n; i++) { + try { + result = fs.writeSync(fd, ...parameters); + } catch { + hasError = true; + } + } + + bench.end(n); + assert(hasError); + break; + } + default: + throw new Error('Invalid type'); + } +} diff --git a/lib/fs.js b/lib/fs.js index 34ad6ef6be835b..5f3f6a5113f6ed 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -896,7 +896,6 @@ ObjectDefineProperty(write, kCustomPromisifyArgsSymbol, */ function writeSync(fd, buffer, offsetOrOptions, length, position) { fd = getValidatedFd(fd); - const ctx = {}; let result; let offset = offsetOrOptions; @@ -918,18 +917,15 @@ function writeSync(fd, buffer, offsetOrOptions, length, position) { if (typeof length !== 'number') length = buffer.byteLength - offset; validateOffsetLengthWrite(offset, length, buffer.byteLength); - result = binding.writeBuffer(fd, buffer, offset, length, position, - undefined, ctx); + result = binding.writeBuffer(fd, buffer, offset, length, position); } else { validateStringAfterArrayBufferView(buffer, 'buffer'); validateEncoding(buffer, length); if (offset === undefined) offset = null; - result = binding.writeString(fd, buffer, offset, length, - undefined, ctx); + result = binding.writeString(fd, buffer, offset, length); } - handleErrorFromBinding(ctx); return result; } diff --git a/src/node_file.cc b/src/node_file.cc index e426319daf7856..dc7ae87dc2b8de 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -2063,13 +2063,13 @@ static void WriteBuffer(const FunctionCallbackInfo& args) { FS_ASYNC_TRACE_BEGIN0(UV_FS_WRITE, req_wrap_async) AsyncCall(env, req_wrap_async, args, "write", UTF8, AfterInteger, uv_fs_write, fd, &uvbuf, 1, pos); - } else { // write(fd, buffer, off, len, pos, undefined, ctx) - CHECK_EQ(argc, 7); - FSReqWrapSync req_wrap_sync; + } else { // write(fd, buffer, off, len, pos) + FSReqWrapSync req_wrap_sync("write"); FS_SYNC_TRACE_BEGIN(write); - int bytesWritten = SyncCall(env, args[6], &req_wrap_sync, "write", - uv_fs_write, fd, &uvbuf, 1, pos); + int bytesWritten = SyncCallAndThrowOnError( + env, &req_wrap_sync, uv_fs_write, fd, &uvbuf, 1, pos); FS_SYNC_TRACE_END(write, "bytesWritten", bytesWritten); + args.GetReturnValue().Set(bytesWritten); } } @@ -2206,9 +2206,8 @@ static void WriteString(const FunctionCallbackInfo& args) { } else { req_wrap_async->SetReturnValue(args); } - } else { // write(fd, string, pos, enc, undefined, ctx) - CHECK_EQ(argc, 6); - FSReqWrapSync req_wrap_sync; + } else { // write(fd, string, pos, enc) + FSReqWrapSync req_wrap_sync("write"); FSReqBase::FSReqBuffer stack_buffer; if (buf == nullptr) { if (!StringBytes::StorageSize(isolate, value, enc).To(&len)) @@ -2223,9 +2222,10 @@ static void WriteString(const FunctionCallbackInfo& args) { } uv_buf_t uvbuf = uv_buf_init(buf, len); FS_SYNC_TRACE_BEGIN(write); - int bytesWritten = SyncCall(env, args[5], &req_wrap_sync, "write", - uv_fs_write, fd, &uvbuf, 1, pos); + int bytesWritten = SyncCallAndThrowOnError( + env, &req_wrap_sync, uv_fs_write, fd, &uvbuf, 1, pos); FS_SYNC_TRACE_END(write, "bytesWritten", bytesWritten); + args.GetReturnValue().Set(bytesWritten); } }